+layout.svelte 3.66 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';
16
	import { page } from '$app/stores';
Jannik Streidl's avatar
Jannik Streidl committed
17
	import { Toaster, toast } from 'svelte-sonner';
Timothy J. Baek's avatar
Timothy J. Baek committed
18

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

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

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

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

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

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

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

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

		const languages = await getLanguages();

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

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

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

72
			if ($config) {
Timothy J. Baek's avatar
Timothy J. Baek committed
73
74
75
76
77
78
79
80
81
				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
82
83
84
85
86
87
				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
88

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

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

101
					if (sessionUser) {
Timothy J. Baek's avatar
Timothy J. Baek committed
102
						// Save Session User to Store
103
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
104
					} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
105
						// Redirect Invalid Session User to /auth Page
Timothy J. Baek's avatar
Timothy J. Baek committed
106
107
108
						localStorage.removeItem('token');
						await goto('/auth');
					}
109
				} else {
110
111
112
113
114
					// Don't redirect if we're already on the auth page
					// Needed because we pass in tokens from OAuth logins via URL fragments
					if ($page.url.pathname !== '/auth') {
						await goto('/auth');
					}
115
116
				}
			}
117
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
118
			// Redirect to /error when Backend Not Detected
119
			await goto(`/error`);
120
121
122
		}

		await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
123
124

		document.getElementById('splash-screen')?.remove();
125
		loaded = true;
126
127
128
129

		return () => {
			window.removeEventListener('resize', onResize);
		};
130
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
131
132
133
</script>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
137
138
	<!-- 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 -->
139
140
	<!-- <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
141
</svelte:head>
142

143
{#if loaded}
144
145
	<slot />
{/if}
146

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