+layout.svelte 3.09 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';
5
	import { page } from '$app/stores';
Jannik Streidl's avatar
Jannik Streidl committed
6
	import { Toaster, toast } from 'svelte-sonner';
Timothy J. Baek's avatar
Timothy J. Baek committed
7

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

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

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

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

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

21
	let loaded = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
22
	const BREAKPOINT = 768;
23
24

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

		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
38
39
40
		let backendConfig = null;
		try {
			backendConfig = await getBackendConfig();
41
			console.log('Backend config:', backendConfig);
Aarni Koskela's avatar
Aarni Koskela committed
42
		} catch (error) {
43
			console.error('Error loading backend config:', error);
Aarni Koskela's avatar
Aarni Koskela committed
44
45
46
		}
		// Initialize i18n even if we didn't get a backend config,
		// so `/error` can show something that's not `undefined`.
47
48
49
50
51
52
53
54

		const languages = await getLanguages();

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

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

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

			await WEBUI_NAME.set(backendConfig.name);
61

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

70
					if (sessionUser) {
Timothy J. Baek's avatar
Timothy J. Baek committed
71
						// Save Session User to Store
72
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
73
					} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
74
						// Redirect Invalid Session User to /auth Page
Timothy J. Baek's avatar
Timothy J. Baek committed
75
76
77
						localStorage.removeItem('token');
						await goto('/auth');
					}
78
				} else {
79
80
81
82
83
					// Don't redirect if we're already on the auth page
					// Needed because we pass in tokens from OAuth logins via URL fragments
					if ($page.url.pathname !== '/auth') {
						await goto('/auth');
					}
84
85
				}
			}
86
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
87
			// Redirect to /error when Backend Not Detected
88
			await goto(`/error`);
89
90
91
		}

		await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
92
93

		document.getElementById('splash-screen')?.remove();
94
		loaded = true;
95
96
97
98

		return () => {
			window.removeEventListener('resize', onResize);
		};
99
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
100
101
102
</script>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
106
107
	<!-- 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 -->
108
109
	<!-- <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
110
</svelte:head>
111

112
{#if loaded}
113
114
	<slot />
{/if}
115

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