+layout.svelte 3.16 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script>
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
	import { io } from 'socket.io-client';

4
	import { onMount, tick, setContext } from 'svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
5
	import { config, user, theme, WEBUI_NAME, mobile, socket } from '$lib/stores';
6
	import { goto } from '$app/navigation';
Jannik Streidl's avatar
Jannik Streidl committed
7
	import { Toaster, toast } from 'svelte-sonner';
Timothy J. Baek's avatar
Timothy J. Baek committed
8

Timothy J. Baek's avatar
Timothy J. Baek committed
9
10
11
	import { getBackendConfig } from '$lib/apis';
	import { getSessionUser } from '$lib/apis/auths';

Timothy J. Baek's avatar
Timothy J. Baek committed
12
	import '../tailwind.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
13
14
	import '../app.css';

Timothy J. Baek's avatar
Timothy J. Baek committed
15
	import 'tippy.js/dist/tippy.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
16

Timothy J. Baek's avatar
Timothy J. Baek committed
17
	import { WEBUI_BASE_URL, WEBUI_HOSTNAME } from '$lib/constants';
18
	import i18n, { initI18n, getLanguages } from '$lib/i18n';
19
20

	setContext('i18n', i18n);
Timothy J. Baek's avatar
Timothy J. Baek committed
21

22
	let loaded = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
23
	const BREAKPOINT = 768;
24
25

	onMount(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
26
		theme.set(localStorage.theme);
27
28
29
30
31
32
33
34
35
36
37
38

		mobile.set(window.innerWidth < BREAKPOINT);
		const onResize = () => {
			if (window.innerWidth < BREAKPOINT) {
				mobile.set(true);
			} else {
				mobile.set(false);
			}
		};

		window.addEventListener('resize', onResize);

Aarni Koskela's avatar
Aarni Koskela committed
39
40
41
		let backendConfig = null;
		try {
			backendConfig = await getBackendConfig();
42
			console.log('Backend config:', backendConfig);
Aarni Koskela's avatar
Aarni Koskela committed
43
		} catch (error) {
44
			console.error('Error loading backend config:', error);
Aarni Koskela's avatar
Aarni Koskela committed
45
46
47
		}
		// Initialize i18n even if we didn't get a backend config,
		// so `/error` can show something that's not `undefined`.
48
49
50
51
52
53
54
55

		const languages = await getLanguages();

		const browserLanguage = navigator.languages
			? navigator.languages[0]
			: navigator.language || navigator.userLanguage;

		initI18n(languages.includes(browserLanguage) ? browserLanguage : backendConfig?.default_locale);
56

Timothy J. Baek's avatar
Timothy J. Baek committed
57
		if (backendConfig) {
Timothy J. Baek's avatar
Timothy J. Baek committed
58
			// Save Backend Status to Store
Timothy J. Baek's avatar
Timothy J. Baek committed
59
			await config.set(backendConfig);
60
			await WEBUI_NAME.set(backendConfig.name);
61

62
			if ($config) {
Timothy J. Baek's avatar
Timothy J. Baek committed
63
64
65
66
67
68
69
70
71
72
73
				const _socket = io(`${WEBUI_BASE_URL}`, {
					path: '/ws/socket.io',
					auth: { token: localStorage.token }
				});

				_socket.on('connect', () => {
					console.log('connected');
				});

				socket.set(_socket);

74
				if (localStorage.token) {
75
					// Get Session User Info
Timothy J. Baek's avatar
Timothy J. Baek committed
76
77
78
79
					const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
						toast.error(error);
						return null;
					});
80

81
					if (sessionUser) {
Timothy J. Baek's avatar
Timothy J. Baek committed
82
						// Save Session User to Store
83
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
84
					} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
85
						// Redirect Invalid Session User to /auth Page
Timothy J. Baek's avatar
Timothy J. Baek committed
86
87
88
						localStorage.removeItem('token');
						await goto('/auth');
					}
89
				} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
90
					await goto('/auth');
91
92
				}
			}
93
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
94
			// Redirect to /error when Backend Not Detected
95
			await goto(`/error`);
96
97
98
		}

		await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
99
100

		document.getElementById('splash-screen')?.remove();
101
		loaded = true;
102
103
104
105

		return () => {
			window.removeEventListener('resize', onResize);
		};
106
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
107
108
109
</script>

<svelte:head>
110
	<title>{$WEBUI_NAME}</title>
Timothy J. Baek's avatar
Timothy J. Baek committed
111
	<link crossorigin="anonymous" rel="icon" href="{WEBUI_BASE_URL}/static/favicon.png" />
Timothy J. Baek's avatar
Timothy J. Baek committed
112

Timothy J. Baek's avatar
Timothy J. Baek committed
113
114
	<!-- rosepine themes have been disabled as it's not up to date with our latest version. -->
	<!-- feel free to make a PR to fix if anyone wants to see it return -->
115
116
	<!-- <link rel="stylesheet" type="text/css" href="/themes/rosepine.css" />
	<link rel="stylesheet" type="text/css" href="/themes/rosepine-dawn.css" /> -->
Timothy J. Baek's avatar
Timothy J. Baek committed
117
</svelte:head>
118

119
{#if loaded}
120
121
	<slot />
{/if}
122

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