2023-10-09 00:38:42 +02:00
|
|
|
<script>
|
2023-11-19 01:47:12 +01:00
|
|
|
import { onMount, tick } from 'svelte';
|
2024-01-02 01:05:05 +01:00
|
|
|
import { config, user, theme } from '$lib/stores';
|
2023-11-19 01:47:12 +01:00
|
|
|
import { goto } from '$app/navigation';
|
|
|
|
import toast, { Toaster } from 'svelte-french-toast';
|
2023-10-09 00:38:42 +02:00
|
|
|
|
2023-12-26 20:32:22 +01:00
|
|
|
import { getBackendConfig } from '$lib/apis';
|
|
|
|
import { getSessionUser } from '$lib/apis/auths';
|
|
|
|
|
2023-10-09 00:38:42 +02:00
|
|
|
import '../app.css';
|
|
|
|
import '../tailwind.css';
|
2023-12-14 23:24:56 +01:00
|
|
|
import 'tippy.js/dist/tippy.css';
|
2024-02-10 04:14:26 +01:00
|
|
|
import { WEBUI_NAME } from '$lib/constants';
|
2023-12-26 20:32:22 +01:00
|
|
|
|
2023-11-19 01:47:12 +01:00
|
|
|
let loaded = false;
|
|
|
|
|
|
|
|
onMount(async () => {
|
2024-01-02 01:05:05 +01:00
|
|
|
theme.set(localStorage.theme);
|
2023-12-26 07:14:06 +01:00
|
|
|
// Check Backend Status
|
2023-12-26 20:32:22 +01:00
|
|
|
const backendConfig = await getBackendConfig();
|
2023-11-19 01:47:12 +01:00
|
|
|
|
2023-12-26 20:32:22 +01:00
|
|
|
if (backendConfig) {
|
2023-12-26 20:34:14 +01:00
|
|
|
// Save Backend Status to Store
|
2023-12-26 20:32:22 +01:00
|
|
|
await config.set(backendConfig);
|
|
|
|
console.log(backendConfig);
|
2023-11-19 01:47:12 +01:00
|
|
|
|
2023-12-26 07:14:06 +01:00
|
|
|
if ($config) {
|
2023-11-19 01:47:12 +01:00
|
|
|
if (localStorage.token) {
|
2023-12-26 07:14:06 +01:00
|
|
|
// Get Session User Info
|
2023-12-26 20:32:22 +01:00
|
|
|
const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
|
|
|
|
toast.error(error);
|
|
|
|
return null;
|
|
|
|
});
|
2023-11-19 01:47:12 +01:00
|
|
|
|
2023-12-26 07:14:06 +01:00
|
|
|
if (sessionUser) {
|
2023-12-26 20:34:14 +01:00
|
|
|
// Save Session User to Store
|
2023-12-26 07:14:06 +01:00
|
|
|
await user.set(sessionUser);
|
2023-11-19 06:41:43 +01:00
|
|
|
} else {
|
2023-12-26 20:34:14 +01:00
|
|
|
// Redirect Invalid Session User to /auth Page
|
2023-11-19 06:41:43 +01:00
|
|
|
localStorage.removeItem('token');
|
|
|
|
await goto('/auth');
|
|
|
|
}
|
2023-11-19 01:47:12 +01:00
|
|
|
} else {
|
2023-11-19 06:41:43 +01:00
|
|
|
await goto('/auth');
|
2023-11-19 01:47:12 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-26 07:14:06 +01:00
|
|
|
} else {
|
2023-12-26 20:34:14 +01:00
|
|
|
// Redirect to /error when Backend Not Detected
|
2023-12-26 07:14:06 +01:00
|
|
|
await goto(`/error`);
|
2023-11-19 01:47:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
await tick();
|
|
|
|
loaded = true;
|
|
|
|
});
|
2023-10-09 00:38:42 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<svelte:head>
|
2024-02-10 04:14:26 +01:00
|
|
|
<title>{WEBUI_NAME}</title>
|
2024-01-02 01:05:05 +01:00
|
|
|
|
|
|
|
<link rel="stylesheet" type="text/css" href="/themes/rosepine.css" />
|
2024-01-02 01:39:02 +01:00
|
|
|
<link rel="stylesheet" type="text/css" href="/themes/rosepine-dawn.css" />
|
2023-10-09 00:38:42 +02:00
|
|
|
</svelte:head>
|
2023-11-19 01:47:12 +01:00
|
|
|
|
2023-12-26 07:14:06 +01:00
|
|
|
{#if loaded}
|
2023-11-19 01:47:12 +01:00
|
|
|
<slot />
|
|
|
|
{/if}
|
2023-12-26 07:14:06 +01:00
|
|
|
|
|
|
|
<Toaster />
|