open-webui/src/routes/+layout.svelte

215 lines
5.3 KiB
Svelte
Raw Normal View History

2023-10-08 22:38:42 +00:00
<script>
2024-06-04 06:39:52 +00:00
import { io } from 'socket.io-client';
2024-06-12 07:05:20 +00:00
import { spring } from 'svelte/motion';
2024-06-12 07:13:35 +00:00
let loadingProgress = spring(0, {
stiffness: 0.05
});
2024-06-04 06:39:52 +00:00
2024-03-01 04:40:36 +00:00
import { onMount, tick, setContext } from 'svelte';
2024-06-04 18:13:43 +00:00
import {
config,
user,
theme,
WEBUI_NAME,
mobile,
socket,
activeUserCount,
USAGE_POOL
} from '$lib/stores';
2023-11-19 00:47:12 +00:00
import { goto } from '$app/navigation';
import { page } from '$app/stores';
2024-03-01 09:18:07 +00:00
import { Toaster, toast } from 'svelte-sonner';
2023-10-08 22:38:42 +00:00
2023-12-26 19:32:22 +00:00
import { getBackendConfig } from '$lib/apis';
import { getSessionUser } from '$lib/apis/auths';
2023-10-08 22:38:42 +00:00
import '../tailwind.css';
2024-04-27 22:50:26 +00:00
import '../app.css';
2023-12-14 22:24:56 +00:00
import 'tippy.js/dist/tippy.css';
2024-04-27 22:50:26 +00:00
2024-06-04 06:39:52 +00:00
import { WEBUI_BASE_URL, WEBUI_HOSTNAME } from '$lib/constants';
2024-05-26 07:17:20 +00:00
import i18n, { initI18n, getLanguages } from '$lib/i18n';
2024-06-30 03:04:04 +00:00
import { bestMatchingLanguage } from '$lib/utils';
2024-03-01 04:40:36 +00:00
setContext('i18n', i18n);
2023-12-26 19:32:22 +00:00
2023-11-19 00:47:12 +00:00
let loaded = false;
2024-05-15 06:48:46 +00:00
const BREAKPOINT = 768;
2023-11-19 00:47:12 +00:00
2024-06-22 20:35:18 +00:00
let wakeLock = null;
2023-11-19 00:47:12 +00:00
onMount(async () => {
2024-01-02 00:05:05 +00:00
theme.set(localStorage.theme);
2024-05-14 22:26:53 +00:00
mobile.set(window.innerWidth < BREAKPOINT);
const onResize = () => {
if (window.innerWidth < BREAKPOINT) {
mobile.set(true);
} else {
mobile.set(false);
}
};
window.addEventListener('resize', onResize);
2024-06-22 20:35:18 +00:00
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);
}
2024-06-24 18:29:21 +00:00
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');
});
}
2024-06-22 20:35:18 +00:00
};
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();
}
});
}
2024-05-13 15:00:30 +00:00
let backendConfig = null;
try {
backendConfig = await getBackendConfig();
console.log('Backend config:', backendConfig);
2024-05-13 15:00:30 +00:00
} catch (error) {
console.error('Error loading backend config:', error);
2024-05-13 15:00:30 +00:00
}
// Initialize i18n even if we didn't get a backend config,
// so `/error` can show something that's not `undefined`.
2024-05-26 07:17:20 +00:00
2024-06-30 21:48:05 +00:00
initI18n();
if (!localStorage.locale) {
2024-06-30 21:52:18 +00:00
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');
2024-06-30 21:48:05 +00:00
$i18n.changeLanguage(lang);
}
2023-11-19 00:47:12 +00:00
2023-12-26 19:32:22 +00:00
if (backendConfig) {
2023-12-26 19:34:14 +00:00
// Save Backend Status to Store
2023-12-26 19:32:22 +00:00
await config.set(backendConfig);
2024-02-24 01:12:19 +00:00
await WEBUI_NAME.set(backendConfig.name);
2023-11-19 00:47:12 +00:00
2023-12-26 06:14:06 +00:00
if ($config) {
const _socket = io({
2024-06-04 06:39:52 +00:00
path: '/ws/socket.io',
auth: { token: localStorage.token }
});
_socket.on('connect', () => {
console.log('connected');
});
2024-06-04 08:10:31 +00:00
await socket.set(_socket);
_socket.on('user-count', (data) => {
console.log('user-count', data);
activeUserCount.set(data.count);
});
2024-06-04 06:39:52 +00:00
2024-06-04 18:13:43 +00:00
_socket.on('usage', (data) => {
console.log('usage', data);
USAGE_POOL.set(data['models']);
});
2023-11-19 00:47:12 +00:00
if (localStorage.token) {
2023-12-26 06:14:06 +00:00
// Get Session User Info
2023-12-26 19:32:22 +00:00
const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
toast.error(error);
return null;
});
2023-11-19 00:47:12 +00:00
2023-12-26 06:14:06 +00:00
if (sessionUser) {
2023-12-26 19:34:14 +00:00
// Save Session User to Store
2023-12-26 06:14:06 +00:00
await user.set(sessionUser);
2023-11-19 05:41:43 +00:00
} else {
2023-12-26 19:34:14 +00:00
// Redirect Invalid Session User to /auth Page
2023-11-19 05:41:43 +00:00
localStorage.removeItem('token');
await goto('/auth');
}
2023-11-19 00:47:12 +00:00
} else {
// 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');
}
2023-11-19 00:47:12 +00:00
}
}
2023-12-26 06:14:06 +00:00
} else {
2023-12-26 19:34:14 +00:00
// Redirect to /error when Backend Not Detected
2023-12-26 06:14:06 +00:00
await goto(`/error`);
2023-11-19 00:47:12 +00:00
}
await tick();
2024-04-27 22:59:20 +00:00
2024-06-12 07:05:20 +00:00
if (
document.documentElement.classList.contains('her') &&
document.getElementById('progress-bar')
) {
loadingProgress.subscribe((value) => {
2024-06-12 07:16:54 +00:00
const progressBar = document.getElementById('progress-bar');
if (progressBar) {
2024-06-12 10:13:59 +00:00
progressBar.style.width = `${value}%`;
2024-06-12 07:16:54 +00:00
}
2024-06-12 07:05:20 +00:00
});
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;
}
2024-05-14 22:26:53 +00:00
return () => {
window.removeEventListener('resize', onResize);
};
2023-11-19 00:47:12 +00:00
});
2023-10-08 22:38:42 +00:00
</script>
<svelte:head>
2024-02-24 01:12:19 +00:00
<title>{$WEBUI_NAME}</title>
2024-05-17 03:49:28 +00:00
<link crossorigin="anonymous" rel="icon" href="{WEBUI_BASE_URL}/static/favicon.png" />
2024-01-02 00:05:05 +00:00
2024-04-27 02:44:10 +00:00
<!-- 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 -->
2024-04-27 02:33:39 +00:00
<!-- <link rel="stylesheet" type="text/css" href="/themes/rosepine.css" />
<link rel="stylesheet" type="text/css" href="/themes/rosepine-dawn.css" /> -->
2023-10-08 22:38:42 +00:00
</svelte:head>
2023-11-19 00:47:12 +00:00
2023-12-26 06:14:06 +00:00
{#if loaded}
2023-11-19 00:47:12 +00:00
<slot />
{/if}
2023-12-26 06:14:06 +00:00
2024-03-02 06:40:55 +00:00
<Toaster richColors position="top-center" />