+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
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';
Timothy J. Baek's avatar
Timothy J. Baek committed
15
	import Spinner from '$lib/components/common/Spinner.svelte';
16
17

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

19
20
21
	let loaded = false;

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

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

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

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

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

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

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

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

78
{#if loaded}
79
	<slot />
Timothy J. Baek's avatar
Timothy J. Baek committed
80
81
82
83
84
85
86
87
88
89
90
{: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>
91
{/if}
92

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