+layout.svelte 1.57 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script>
2
	import { onMount, tick } from 'svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
3
	import { config, user, theme } from '$lib/stores';
4
5
	import { goto } from '$app/navigation';
	import toast, { Toaster } from 'svelte-french-toast';
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';
Timothy J. Baek's avatar
Timothy J. Baek committed
13
	import { WEBUI_NAME } 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
25
			await config.set(backendConfig);
			console.log(backendConfig);
26

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

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

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

<svelte:head>
Timothy J. Baek's avatar
Timothy J. Baek committed
58
	<title>{WEBUI_NAME}</title>
Timothy J. Baek's avatar
Timothy J. Baek committed
59
60

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

64
{#if loaded}
65
66
	<slot />
{/if}
67
68

<Toaster />