+layout.svelte 2.88 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, getLanguages } from '$lib/i18n';
17
18

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

20
	let loaded = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
21
	const BREAKPOINT = 768;
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
		}
		// Initialize i18n even if we didn't get a backend config,
		// so `/error` can show something that's not `undefined`.
46
47
48
49
50
51
52
53

		const languages = await getLanguages();

		const browserLanguage = navigator.languages
			? navigator.languages[0]
			: navigator.language || navigator.userLanguage;

		initI18n(languages.includes(browserLanguage) ? browserLanguage : backendConfig?.default_locale);
54

Timothy J. Baek's avatar
Timothy J. Baek committed
55
		if (backendConfig) {
Timothy J. Baek's avatar
Timothy J. Baek committed
56
			// Save Backend Status to Store
Timothy J. Baek's avatar
Timothy J. Baek committed
57
			await config.set(backendConfig);
58
59

			await WEBUI_NAME.set(backendConfig.name);
60

61
			if ($config) {
62
				if (localStorage.token) {
63
					// Get Session User Info
Timothy J. Baek's avatar
Timothy J. Baek committed
64
65
66
67
					const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
						toast.error(error);
						return null;
					});
68

69
					if (sessionUser) {
Timothy J. Baek's avatar
Timothy J. Baek committed
70
						// Save Session User to Store
71
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
72
					} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
73
						// Redirect Invalid Session User to /auth Page
Timothy J. Baek's avatar
Timothy J. Baek committed
74
75
76
						localStorage.removeItem('token');
						await goto('/auth');
					}
77
				} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
78
					await goto('/auth');
79
80
				}
			}
81
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
82
			// Redirect to /error when Backend Not Detected
83
			await goto(`/error`);
84
85
86
		}

		await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
87
88

		document.getElementById('splash-screen')?.remove();
89
		loaded = true;
90
91
92
93

		return () => {
			window.removeEventListener('resize', onResize);
		};
94
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
95
96
97
</script>

<svelte:head>
98
	<title>{$WEBUI_NAME}</title>
Timothy J. Baek's avatar
Timothy J. Baek committed
99
	<link crossorigin="anonymous" rel="icon" href="{WEBUI_BASE_URL}/static/favicon.png" />
Timothy J. Baek's avatar
Timothy J. Baek committed
100

Timothy J. Baek's avatar
Timothy J. Baek committed
101
102
	<!-- 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 -->
103
104
	<!-- <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
105
</svelte:head>
106

107
{#if loaded}
108
109
	<slot />
{/if}
110

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