+layout.svelte 2.38 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
24
		document.getElementById('splash-screen')?.remove();

Timothy J. Baek's avatar
Timothy J. Baek committed
25
		theme.set(localStorage.theme);
26
		// Check Backend Status
Timothy J. Baek's avatar
Timothy J. Baek committed
27
		const backendConfig = await getBackendConfig();
28

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

			await WEBUI_NAME.set(backendConfig.name);
Timothy J. Baek's avatar
Timothy J. Baek committed
39
			console.log(backendConfig);
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
67
68
		}

		await tick();
		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
	<slot />
Timothy J. Baek's avatar
Timothy J. Baek committed
83
84
85
86
87
88
89
90
91
92
93
{:else}
	<div class=" min-h-screen h-[100dvh] flex">
		<div class="m-auto">
			<img
				src="/logo.svg"
				alt="logo"
				class=" size-24 rounded-full border-[1px] border-gray-200 dark:border-none mx-auto mb-8"
				draggable="false"
			/>
		</div>
	</div>
94
{/if}
95

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