"src/vscode:/vscode.git/clone" did not exist on "11e79f52ad37097cbfbcc103823f308c6949c906"
+layout.svelte 5.35 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script>
Timothy J. Baek's avatar
Timothy J. Baek committed
2
	import { io } from 'socket.io-client';
Timothy J. Baek's avatar
Timothy J. Baek committed
3
4
	import { spring } from 'svelte/motion';

Timothy J. Baek's avatar
Timothy J. Baek committed
5
6
7
	let loadingProgress = spring(0, {
		stiffness: 0.05
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
8

9
	import { onMount, tick, setContext } from 'svelte';
10
11
12
13
14
15
16
17
18
19
	import {
		config,
		user,
		theme,
		WEBUI_NAME,
		mobile,
		socket,
		activeUserCount,
		USAGE_POOL
	} from '$lib/stores';
20
	import { goto } from '$app/navigation';
21
	import { page } from '$app/stores';
Jannik Streidl's avatar
Jannik Streidl committed
22
	import { Toaster, toast } from 'svelte-sonner';
Timothy J. Baek's avatar
Timothy J. Baek committed
23

Timothy J. Baek's avatar
Timothy J. Baek committed
24
25
26
	import { getBackendConfig } from '$lib/apis';
	import { getSessionUser } from '$lib/apis/auths';

Timothy J. Baek's avatar
Timothy J. Baek committed
27
	import '../tailwind.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
28
29
	import '../app.css';

Timothy J. Baek's avatar
Timothy J. Baek committed
30
	import 'tippy.js/dist/tippy.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
31

Timothy J. Baek's avatar
Timothy J. Baek committed
32
	import { WEBUI_BASE_URL, WEBUI_HOSTNAME } from '$lib/constants';
33
	import i18n, { initI18n, getLanguages } from '$lib/i18n';
34
	import { bestMatchingLanguage } from '$lib/utils';
35
36

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

38
	let loaded = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
39
	const BREAKPOINT = 768;
40

Timothy J. Baek's avatar
Timothy J. Baek committed
41
42
	let wakeLock = null;

43
	onMount(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
44
		theme.set(localStorage.theme);
45
46
47
48
49
50
51
52
53
54
55
56

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

		window.addEventListener('resize', onResize);

Timothy J. Baek's avatar
Timothy J. Baek committed
57
58
59
60
61
62
63
64
		const setWakeLock = async () => {
			try {
				wakeLock = await navigator.wakeLock.request('screen');
			} catch (err) {
				// The Wake Lock request has failed - usually system related, such as battery.
				console.log(err);
			}

Timothy J. Baek's avatar
Timothy J. Baek committed
65
66
67
68
69
70
71
			if (wakeLock) {
				// Add a listener to release the wake lock when the page is unloaded
				wakeLock.addEventListener('release', () => {
					// the wake lock has been released
					console.log('Wake Lock released');
				});
			}
Timothy J. Baek's avatar
Timothy J. Baek committed
72
73
74
75
76
77
78
79
80
81
82
83
84
		};

		if ('wakeLock' in navigator) {
			await setWakeLock();

			document.addEventListener('visibilitychange', async () => {
				// Re-request the wake lock if the document becomes visible
				if (wakeLock !== null && document.visibilityState === 'visible') {
					await setWakeLock();
				}
			});
		}

Aarni Koskela's avatar
Aarni Koskela committed
85
86
87
		let backendConfig = null;
		try {
			backendConfig = await getBackendConfig();
88
			console.log('Backend config:', backendConfig);
Aarni Koskela's avatar
Aarni Koskela committed
89
		} catch (error) {
90
			console.error('Error loading backend config:', error);
Aarni Koskela's avatar
Aarni Koskela committed
91
92
93
		}
		// Initialize i18n even if we didn't get a backend config,
		// so `/error` can show something that's not `undefined`.
94

Timothy J. Baek's avatar
Timothy J. Baek committed
95
96
		initI18n();
		if (!localStorage.locale) {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
97
98
99
100
101
102
103
			const languages = await getLanguages();
			const browserLanguages = navigator.languages
				? navigator.languages
				: [navigator.language || navigator.userLanguage];
			const lang = backendConfig.default_locale
				? backendConfig.default_locale
				: bestMatchingLanguage(languages, browserLanguages, 'en-US');
Timothy J. Baek's avatar
Timothy J. Baek committed
104
105
			$i18n.changeLanguage(lang);
		}
106

Timothy J. Baek's avatar
Timothy J. Baek committed
107
		if (backendConfig) {
Timothy J. Baek's avatar
Timothy J. Baek committed
108
			// Save Backend Status to Store
Timothy J. Baek's avatar
Timothy J. Baek committed
109
			await config.set(backendConfig);
110
			await WEBUI_NAME.set(backendConfig.name);
111

112
			if ($config) {
ther0bster's avatar
ther0bster committed
113
				const _socket = io(`${WEBUI_BASE_URL}` || undefined, {
Timothy J. Baek's avatar
Timothy J. Baek committed
114
115
116
117
118
119
120
121
					path: '/ws/socket.io',
					auth: { token: localStorage.token }
				});

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

Timothy J. Baek's avatar
Timothy J. Baek committed
122
123
124
125
126
127
				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
128

129
130
131
132
133
				_socket.on('usage', (data) => {
					console.log('usage', data);
					USAGE_POOL.set(data['models']);
				});

134
				if (localStorage.token) {
135
					// Get Session User Info
Timothy J. Baek's avatar
Timothy J. Baek committed
136
137
138
139
					const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
						toast.error(error);
						return null;
					});
140

141
					if (sessionUser) {
Timothy J. Baek's avatar
Timothy J. Baek committed
142
						// Save Session User to Store
143
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
144
					} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
145
						// Redirect Invalid Session User to /auth Page
Timothy J. Baek's avatar
Timothy J. Baek committed
146
147
148
						localStorage.removeItem('token');
						await goto('/auth');
					}
149
				} else {
150
151
152
153
154
					// 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');
					}
155
156
				}
			}
157
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
158
			// Redirect to /error when Backend Not Detected
159
			await goto(`/error`);
160
161
162
		}

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

Timothy J. Baek's avatar
Timothy J. Baek committed
164
165
166
167
168
		if (
			document.documentElement.classList.contains('her') &&
			document.getElementById('progress-bar')
		) {
			loadingProgress.subscribe((value) => {
Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
169
170
171
				const progressBar = document.getElementById('progress-bar');

				if (progressBar) {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
172
					progressBar.style.width = `${value}%`;
Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
173
				}
Timothy J. Baek's avatar
Timothy J. Baek committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
			});

			await loadingProgress.set(100);

			document.getElementById('splash-screen')?.remove();

			const audio = new Audio(`/audio/greeting.mp3`);
			const playAudio = () => {
				audio.play();
				document.removeEventListener('click', playAudio);
			};

			document.addEventListener('click', playAudio);

			loaded = true;
		} else {
			document.getElementById('splash-screen')?.remove();
			loaded = true;
		}
193
194
195
196

		return () => {
			window.removeEventListener('resize', onResize);
		};
197
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
198
199
200
</script>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
204
205
	<!-- 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 -->
206
207
	<!-- <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
208
</svelte:head>
209

210
{#if loaded}
211
212
	<slot />
{/if}
213

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