"vscode:/vscode.git/clone" did not exist on "b7d7041190f41cddfc4b6b9776afcc9a4734cea4"
+layout.svelte 1.9 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
11
	import '../app.css';
	import '../tailwind.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
12
	import 'tippy.js/dist/tippy.css';
13
	import { WEBUI_BASE_URL } from '$lib/constants';
14
	import i18n, { initI18n } from '$lib/i18n';
15
16

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

18
19
20
	let loaded = false;

	onMount(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
21
		theme.set(localStorage.theme);
22
		// Check Backend Status
Timothy J. Baek's avatar
Timothy J. Baek committed
23
		const backendConfig = await getBackendConfig();
24

Timothy J. Baek's avatar
Timothy J. Baek committed
25
		if (backendConfig) {
Timothy J. Baek's avatar
Timothy J. Baek committed
26
			// Save Backend Status to Store
Timothy J. Baek's avatar
Timothy J. Baek committed
27
			await config.set(backendConfig);
28
29
			if ($config.default_locale) {
				initI18n($config.default_locale);
30
31
32
			} else {
				initI18n();
			}
33
34

			await WEBUI_NAME.set(backendConfig.name);
Timothy J. Baek's avatar
Timothy J. Baek committed
35
			console.log(backendConfig);
36

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

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

		await tick();
		loaded = true;
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
65
66
67
</script>

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

	<link rel="stylesheet" type="text/css" href="/themes/rosepine.css" />
ThatOneCalculator's avatar
ThatOneCalculator committed
72
	<link rel="stylesheet" type="text/css" href="/themes/rosepine-dawn.css" />
Timothy J. Baek's avatar
Timothy J. Baek committed
73
</svelte:head>
74

75
{#if loaded}
76
77
	<slot />
{/if}
78

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