+layout.svelte 1.46 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script>
2
3
4
5
6
	import { onMount, tick } from 'svelte';
	import { config, user } from '$lib/stores';
	import { goto } from '$app/navigation';
	import { WEBUI_API_BASE_URL } from '$lib/constants';
	import toast, { Toaster } from 'svelte-french-toast';
Timothy J. Baek's avatar
Timothy J. Baek committed
7
8
9

	import '../app.css';
	import '../tailwind.css';
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

	let loaded = false;

	onMount(async () => {
		const webBackendStatus = await fetch(`${WEBUI_API_BASE_URL}/`, {
			method: 'GET',
			headers: {
				'Content-Type': 'application/json'
			}
		})
			.then(async (res) => {
				if (!res.ok) throw await res.json();
				return res.json();
			})
			.catch((error) => {
				console.log(error);
				return null;
			});

		console.log(webBackendStatus);
		await config.set(webBackendStatus);

		if (webBackendStatus) {
			if (webBackendStatus.auth) {
				if (localStorage.token) {
					const res = await fetch(`${WEBUI_API_BASE_URL}/auths`, {
						method: 'GET',
						headers: {
							'Content-Type': 'application/json',
							Authorization: `Bearer ${localStorage.token}`
						}
					})
						.then(async (res) => {
							if (!res.ok) throw await res.json();
							return res.json();
						})
						.catch((error) => {
							console.log(error);
							toast.error(error.detail);
							return null;
						});

					await user.set(res);
				} else {
					goto('/auth');
				}
			}
		}

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

<svelte:head>
	<title>Ollama</title>
</svelte:head>
<Toaster />
68
69
70
71

{#if $config !== undefined && loaded}
	<slot />
{/if}