"src/fastertransformer/layers/BaseLayer.h" did not exist on "720fc533da804ac3f46ee938864403e51fcd9fa7"
+layout.svelte 3.3 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, activeUserCount } 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
				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
72
73
74
75
76
77
				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
78

79
				if (localStorage.token) {
80
					// Get Session User Info
Timothy J. Baek's avatar
Timothy J. Baek committed
81
82
83
84
					const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
						toast.error(error);
						return null;
					});
85

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

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

		document.getElementById('splash-screen')?.remove();
106
		loaded = true;
107
108
109
110

		return () => {
			window.removeEventListener('resize', onResize);
		};
111
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
112
113
114
</script>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
118
119
	<!-- 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 -->
120
121
	<!-- <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
122
</svelte:head>
123

124
{#if loaded}
125
126
	<slot />
{/if}
127

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