|
<script> |
|
import { io } from 'socket.io-client'; |
|
import { spring } from 'svelte/motion'; |
|
|
|
let loadingProgress = spring(0, { |
|
stiffness: 0.05 |
|
}); |
|
|
|
import { onMount, tick, setContext } from 'svelte'; |
|
import { |
|
config, |
|
user, |
|
theme, |
|
WEBUI_NAME, |
|
mobile, |
|
socket, |
|
activeUserCount, |
|
USAGE_POOL |
|
} from '$lib/stores'; |
|
import { goto } from '$app/navigation'; |
|
import { page } from '$app/stores'; |
|
import { Toaster, toast } from 'svelte-sonner'; |
|
|
|
import { getBackendConfig } from '$lib/apis'; |
|
import { getSessionUser } from '$lib/apis/auths'; |
|
|
|
import '../tailwind.css'; |
|
import '../app.css'; |
|
|
|
import 'tippy.js/dist/tippy.css'; |
|
|
|
import { WEBUI_BASE_URL, WEBUI_HOSTNAME } from '$lib/constants'; |
|
import i18n, { initI18n, getLanguages } from '$lib/i18n'; |
|
import { bestMatchingLanguage } from '$lib/utils'; |
|
|
|
setContext('i18n', i18n); |
|
|
|
let loaded = false; |
|
const BREAKPOINT = 768; |
|
|
|
let wakeLock = null; |
|
|
|
onMount(async () => { |
|
theme.set(localStorage.theme); |
|
|
|
mobile.set(window.innerWidth < BREAKPOINT); |
|
const onResize = () => { |
|
if (window.innerWidth < BREAKPOINT) { |
|
mobile.set(true); |
|
} else { |
|
mobile.set(false); |
|
} |
|
}; |
|
|
|
window.addEventListener('resize', onResize); |
|
|
|
const setWakeLock = async () => { |
|
try { |
|
wakeLock = await navigator.wakeLock.request('screen'); |
|
} catch (err) { |
|
|
|
console.log(err); |
|
} |
|
|
|
if (wakeLock) { |
|
|
|
wakeLock.addEventListener('release', () => { |
|
|
|
console.log('Wake Lock released'); |
|
}); |
|
} |
|
}; |
|
|
|
if ('wakeLock' in navigator) { |
|
await setWakeLock(); |
|
|
|
document.addEventListener('visibilitychange', async () => { |
|
|
|
if (wakeLock !== null && document.visibilityState === 'visible') { |
|
await setWakeLock(); |
|
} |
|
}); |
|
} |
|
|
|
let backendConfig = null; |
|
try { |
|
backendConfig = await getBackendConfig(); |
|
console.log('Backend config:', backendConfig); |
|
} catch (error) { |
|
console.error('Error loading backend config:', error); |
|
} |
|
|
|
|
|
|
|
initI18n(); |
|
if (!localStorage.locale) { |
|
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'); |
|
$i18n.changeLanguage(lang); |
|
} |
|
|
|
if (backendConfig) { |
|
|
|
await config.set(backendConfig); |
|
await WEBUI_NAME.set(backendConfig.name); |
|
|
|
if ($config) { |
|
const _socket = io(`${WEBUI_BASE_URL}` || undefined, { |
|
path: '/ws/socket.io', |
|
auth: { token: localStorage.token } |
|
}); |
|
|
|
_socket.on('connect', () => { |
|
console.log('connected'); |
|
}); |
|
|
|
await socket.set(_socket); |
|
|
|
_socket.on('user-count', (data) => { |
|
console.log('user-count', data); |
|
activeUserCount.set(data.count); |
|
}); |
|
|
|
_socket.on('usage', (data) => { |
|
console.log('usage', data); |
|
USAGE_POOL.set(data['models']); |
|
}); |
|
|
|
if (localStorage.token) { |
|
|
|
const sessionUser = await getSessionUser(localStorage.token).catch((error) => { |
|
toast.error(error); |
|
return null; |
|
}); |
|
|
|
if (sessionUser) { |
|
|
|
await user.set(sessionUser); |
|
} else { |
|
|
|
localStorage.removeItem('token'); |
|
await goto('/auth'); |
|
} |
|
} else { |
|
|
|
|
|
if ($page.url.pathname !== '/auth') { |
|
await goto('/auth'); |
|
} |
|
} |
|
} |
|
} else { |
|
|
|
await goto(`/error`); |
|
} |
|
|
|
await tick(); |
|
|
|
if ( |
|
document.documentElement.classList.contains('her') && |
|
document.getElementById('progress-bar') |
|
) { |
|
loadingProgress.subscribe((value) => { |
|
const progressBar = document.getElementById('progress-bar'); |
|
|
|
if (progressBar) { |
|
progressBar.style.width = `${value}%`; |
|
} |
|
}); |
|
|
|
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; |
|
} |
|
|
|
return () => { |
|
window.removeEventListener('resize', onResize); |
|
}; |
|
}); |
|
</script> |
|
|
|
<svelte:head> |
|
<title>{$WEBUI_NAME}</title> |
|
<link crossorigin="anonymous" rel="icon" href="{WEBUI_BASE_URL}/static/favicon.png" /> |
|
|
|
|
|
|
|
|
|
|
|
</svelte:head> |
|
|
|
{#if loaded} |
|
<slot /> |
|
{/if} |
|
|
|
<Toaster richColors position="top-center" /> |
|
|