+layout.svelte 2.13 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);
24
		// Check Backend Status
Timothy J. Baek's avatar
Timothy J. Baek committed
25
		const backendConfig = await getBackendConfig();
26

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

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

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

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

		await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
65
66

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

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

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

81
{#if loaded}
82
83
	<slot />
{/if}
84

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