+layout.svelte 1.52 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

14
15
16
	let loaded = false;

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

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

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

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

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

<svelte:head>
	<title>Ollama</title>
Timothy J. Baek's avatar
Timothy J. Baek committed
58
59

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

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

<Toaster />