+layout.svelte 1.79 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script>
2
	import { onMount, tick, setContext } from 'svelte';
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
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
15
16
	import i18n from '$lib/i18n';

	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
refac  
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

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

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

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

		await tick();
		loaded = true;
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
60
61
62
</script>

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

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

70
{#if loaded}
71
72
	<slot />
{/if}
73

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