+layout.svelte 3.45 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';
5
6
7
8
9
10
11
12
13
14
	import {
		config,
		user,
		theme,
		WEBUI_NAME,
		mobile,
		socket,
		activeUserCount,
		USAGE_POOL
	} from '$lib/stores';
15
	import { goto } from '$app/navigation';
Jannik Streidl's avatar
Jannik Streidl committed
16
	import { Toaster, toast } from 'svelte-sonner';
Timothy J. Baek's avatar
Timothy J. Baek committed
17

Timothy J. Baek's avatar
Timothy J. Baek committed
18
19
20
	import { getBackendConfig } from '$lib/apis';
	import { getSessionUser } from '$lib/apis/auths';

Timothy J. Baek's avatar
Timothy J. Baek committed
21
	import '../tailwind.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
	import '../app.css';

Timothy J. Baek's avatar
Timothy J. Baek committed
24
	import 'tippy.js/dist/tippy.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
25

Timothy J. Baek's avatar
Timothy J. Baek committed
26
	import { WEBUI_BASE_URL, WEBUI_HOSTNAME } from '$lib/constants';
27
	import i18n, { initI18n, getLanguages } from '$lib/i18n';
28
29

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

31
	let loaded = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
32
	const BREAKPOINT = 768;
33
34

	onMount(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
35
		theme.set(localStorage.theme);
36
37
38
39
40
41
42
43
44
45
46
47

		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
48
49
50
		let backendConfig = null;
		try {
			backendConfig = await getBackendConfig();
51
			console.log('Backend config:', backendConfig);
Aarni Koskela's avatar
Aarni Koskela committed
52
		} catch (error) {
53
			console.error('Error loading backend config:', error);
Aarni Koskela's avatar
Aarni Koskela committed
54
55
56
		}
		// Initialize i18n even if we didn't get a backend config,
		// so `/error` can show something that's not `undefined`.
57
58
59
60
61
62
63
64

		const languages = await getLanguages();

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

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

Timothy J. Baek's avatar
Timothy J. Baek committed
66
		if (backendConfig) {
Timothy J. Baek's avatar
Timothy J. Baek committed
67
			// Save Backend Status to Store
Timothy J. Baek's avatar
Timothy J. Baek committed
68
			await config.set(backendConfig);
69
			await WEBUI_NAME.set(backendConfig.name);
70

71
			if ($config) {
Timothy J. Baek's avatar
Timothy J. Baek committed
72
73
74
75
76
77
78
79
80
				const _socket = io(`${WEBUI_BASE_URL}`, {
					path: '/ws/socket.io',
					auth: { token: localStorage.token }
				});

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

Timothy J. Baek's avatar
Timothy J. Baek committed
81
82
83
84
85
86
				await socket.set(_socket);

				_socket.on('user-count', (data) => {
					console.log('user-count', data);
					activeUserCount.set(data.count);
				});
Timothy J. Baek's avatar
Timothy J. Baek committed
87

88
89
90
91
92
				_socket.on('usage', (data) => {
					console.log('usage', data);
					USAGE_POOL.set(data['models']);
				});

93
				if (localStorage.token) {
94
					// Get Session User Info
Timothy J. Baek's avatar
Timothy J. Baek committed
95
96
97
98
					const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
						toast.error(error);
						return null;
					});
99

100
					if (sessionUser) {
Timothy J. Baek's avatar
Timothy J. Baek committed
101
						// Save Session User to Store
102
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
103
					} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
104
						// Redirect Invalid Session User to /auth Page
Timothy J. Baek's avatar
Timothy J. Baek committed
105
106
107
						localStorage.removeItem('token');
						await goto('/auth');
					}
108
				} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
109
					await goto('/auth');
110
111
				}
			}
112
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
113
			// Redirect to /error when Backend Not Detected
114
			await goto(`/error`);
115
116
117
		}

		await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
118
119

		document.getElementById('splash-screen')?.remove();
120
		loaded = true;
121
122
123
124

		return () => {
			window.removeEventListener('resize', onResize);
		};
125
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
126
127
128
</script>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
132
133
	<!-- 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 -->
134
135
	<!-- <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
136
</svelte:head>
137

138
{#if loaded}
139
140
	<slot />
{/if}
141

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