+layout.svelte 1.72 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script>
2
	import { onMount, tick } 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';
Timothy J. Baek's avatar
Timothy J. Baek committed
14

15
16
17
	let loaded = false;

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

Timothy J. Baek's avatar
Timothy J. Baek committed
22
		if (backendConfig) {
Timothy J. Baek's avatar
Timothy J. Baek committed
23
			// Save Backend Status to Store
Timothy J. Baek's avatar
Timothy J. Baek committed
24
			await config.set(backendConfig);
25
26

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

29
			if ($config) {
30
				if (localStorage.token) {
31
					// Get Session User Info
Timothy J. Baek's avatar
Timothy J. Baek committed
32
33
34
35
					const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
						toast.error(error);
						return null;
					});
36

37
					if (sessionUser) {
Timothy J. Baek's avatar
Timothy J. Baek committed
38
						// Save Session User to Store
39
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
40
					} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
41
						// Redirect Invalid Session User to /auth Page
Timothy J. Baek's avatar
Timothy J. Baek committed
42
43
44
						localStorage.removeItem('token');
						await goto('/auth');
					}
45
				} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
46
					await goto('/auth');
47
48
				}
			}
49
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
50
			// Redirect to /error when Backend Not Detected
51
			await goto(`/error`);
52
53
54
55
56
		}

		await tick();
		loaded = true;
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
57
58
59
</script>

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

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

67
{#if loaded}
68
69
	<slot />
{/if}
70

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