+layout.svelte 2.29 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 } 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
21
22
	let loaded = false;

	onMount(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
23
		theme.set(localStorage.theme);
Aarni Koskela's avatar
Aarni Koskela committed
24
25
26
27
28
29
30
31
32
33
		let backendConfig = null;
		try {
			backendConfig = await getBackendConfig();
			console.log("Backend config:", backendConfig);
		} catch (error) {
			console.error("Error loading backend config:", error);
		}
		// Initialize i18n even if we didn't get a backend config,
		// so `/error` can show something that's not `undefined`.
		initI18n(backendConfig?.default_locale);
34

Timothy J. Baek's avatar
Timothy J. Baek committed
35
		if (backendConfig) {
Timothy J. Baek's avatar
Timothy J. Baek committed
36
			// Save Backend Status to Store
Timothy J. Baek's avatar
Timothy J. Baek committed
37
			await config.set(backendConfig);
38
39

			await WEBUI_NAME.set(backendConfig.name);
40

41
			if ($config) {
42
				if (localStorage.token) {
43
					// Get Session User Info
Timothy J. Baek's avatar
Timothy J. Baek committed
44
45
46
47
					const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
						toast.error(error);
						return null;
					});
48

49
					if (sessionUser) {
Timothy J. Baek's avatar
Timothy J. Baek committed
50
						// Save Session User to Store
51
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
52
					} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
53
						// Redirect Invalid Session User to /auth Page
Timothy J. Baek's avatar
Timothy J. Baek committed
54
55
56
						localStorage.removeItem('token');
						await goto('/auth');
					}
57
				} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
58
					await goto('/auth');
59
60
				}
			}
61
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
62
			// Redirect to /error when Backend Not Detected
63
			await goto(`/error`);
64
65
66
		}

		await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
67
68

		document.getElementById('splash-screen')?.remove();
69
70
		loaded = true;
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
71
72
73
</script>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
77
78
	<!-- 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 -->
79
80
	<!-- <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
81
</svelte:head>
82

83
{#if loaded}
84
85
	<slot />
{/if}
86

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