|
<script> |
|
import { onMount, tick, setContext } from 'svelte'; |
|
import { config, user, theme, WEBUI_NAME, mobile } from '$lib/stores'; |
|
import { goto } from '$app/navigation'; |
|
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 } from '$lib/constants'; |
|
import i18n, { initI18n } from '$lib/i18n'; |
|
|
|
setContext('i18n', i18n); |
|
|
|
let loaded = false; |
|
const BREAKPOINT = 768; |
|
|
|
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); |
|
|
|
let backendConfig = null; |
|
try { |
|
backendConfig = await getBackendConfig(); |
|
console.log('Backend config:', backendConfig); |
|
} catch (error) { |
|
console.error('Error loading backend config:', error); |
|
} |
|
|
|
|
|
initI18n(backendConfig?.default_locale); |
|
|
|
if (backendConfig) { |
|
|
|
await config.set(backendConfig); |
|
|
|
await WEBUI_NAME.set(backendConfig.name); |
|
|
|
if ($config) { |
|
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 { |
|
await goto('/auth'); |
|
} |
|
} |
|
} else { |
|
|
|
await goto(`/error`); |
|
} |
|
|
|
await tick(); |
|
|
|
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" /> |
|
|