+layout.svelte 2.62 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script>
2
	import { onMount, tick, setContext } from 'svelte';
3
	import { config, user, theme, WEBUI_NAME, mobile } from '$lib/stores';
4
	import { goto } from '$app/navigation';
Jannik Streidl's avatar
Jannik Streidl committed
5
	import { Toaster, toast } from 'svelte-sonner';
Timothy J. Baek's avatar
Timothy J. Baek committed
6

Timothy J. Baek's avatar
Timothy J. Baek committed
7
8
9
	import { getBackendConfig } from '$lib/apis';
	import { getSessionUser } from '$lib/apis/auths';

Timothy J. Baek's avatar
Timothy J. Baek committed
10
	import '../tailwind.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
11
12
	import '../app.css';

Timothy J. Baek's avatar
Timothy J. Baek committed
13
	import 'tippy.js/dist/tippy.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
14

15
	import { WEBUI_BASE_URL } from '$lib/constants';
16
	import i18n, { initI18n } from '$lib/i18n';
17
18

	setContext('i18n', i18n);
Timothy J. Baek's avatar
Timothy J. Baek committed
19

20
	let loaded = false;
21
	const BREAKPOINT = 1024;
22
23

	onMount(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
24
		theme.set(localStorage.theme);
25
26
27
28
29
30
31
32
33
34
35
36

		mobile.set(window.innerWidth < BREAKPOINT);
		const onResize = () => {
			if (window.innerWidth < BREAKPOINT) {
				mobile.set(true);
			} else {
				mobile.set(false);
			}
		};

		window.addEventListener('resize', onResize);

Aarni Koskela's avatar
Aarni Koskela committed
37
38
39
		let backendConfig = null;
		try {
			backendConfig = await getBackendConfig();
40
			console.log('Backend config:', backendConfig);
Aarni Koskela's avatar
Aarni Koskela committed
41
		} catch (error) {
42
			console.error('Error loading backend config:', error);
Aarni Koskela's avatar
Aarni Koskela committed
43
44
45
46
		}
		// Initialize i18n even if we didn't get a backend config,
		// so `/error` can show something that's not `undefined`.
		initI18n(backendConfig?.default_locale);
47

Timothy J. Baek's avatar
Timothy J. Baek committed
48
		if (backendConfig) {
Timothy J. Baek's avatar
Timothy J. Baek committed
49
			// Save Backend Status to Store
Timothy J. Baek's avatar
Timothy J. Baek committed
50
			await config.set(backendConfig);
51
52

			await WEBUI_NAME.set(backendConfig.name);
53

54
			if ($config) {
55
				if (localStorage.token) {
56
					// Get Session User Info
Timothy J. Baek's avatar
Timothy J. Baek committed
57
58
59
60
					const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
						toast.error(error);
						return null;
					});
61

62
					if (sessionUser) {
Timothy J. Baek's avatar
Timothy J. Baek committed
63
						// Save Session User to Store
64
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
65
					} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
66
						// Redirect Invalid Session User to /auth Page
Timothy J. Baek's avatar
Timothy J. Baek committed
67
68
69
						localStorage.removeItem('token');
						await goto('/auth');
					}
70
				} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
71
					await goto('/auth');
72
73
				}
			}
74
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
75
			// Redirect to /error when Backend Not Detected
76
			await goto(`/error`);
77
78
79
		}

		await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
80
81

		document.getElementById('splash-screen')?.remove();
82
		loaded = true;
83
84
85
86

		return () => {
			window.removeEventListener('resize', onResize);
		};
87
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
88
89
90
</script>

<svelte:head>
91
92
	<title>{$WEBUI_NAME}</title>
	<link rel="icon" href="{WEBUI_BASE_URL}/static/favicon.png" />
Timothy J. Baek's avatar
Timothy J. Baek committed
93

Timothy J. Baek's avatar
Timothy J. Baek committed
94
95
	<!-- rosepine themes have been disabled as it's not up to date with our latest version. -->
	<!-- feel free to make a PR to fix if anyone wants to see it return -->
96
97
	<!-- <link rel="stylesheet" type="text/css" href="/themes/rosepine.css" />
	<link rel="stylesheet" type="text/css" href="/themes/rosepine-dawn.css" /> -->
Timothy J. Baek's avatar
Timothy J. Baek committed
98
</svelte:head>
99

100
{#if loaded}
101
102
	<slot />
{/if}
103

Timothy J. Baek's avatar
Timothy J. Baek committed
104
<Toaster richColors position="top-center" />