+layout.svelte 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <script>
  2. import { onMount, tick, setContext } from 'svelte';
  3. import { config, user, theme, WEBUI_NAME, mobile } from '$lib/stores';
  4. import { goto } from '$app/navigation';
  5. import { Toaster, toast } from 'svelte-sonner';
  6. import { getBackendConfig } from '$lib/apis';
  7. import { getSessionUser } from '$lib/apis/auths';
  8. import '../tailwind.css';
  9. import '../app.css';
  10. import 'tippy.js/dist/tippy.css';
  11. import { WEBUI_BASE_URL } from '$lib/constants';
  12. import i18n, { initI18n } from '$lib/i18n';
  13. setContext('i18n', i18n);
  14. let loaded = false;
  15. const BREAKPOINT = 768;
  16. onMount(async () => {
  17. theme.set(localStorage.theme);
  18. mobile.set(window.innerWidth < BREAKPOINT);
  19. const onResize = () => {
  20. if (window.innerWidth < BREAKPOINT) {
  21. mobile.set(true);
  22. } else {
  23. mobile.set(false);
  24. }
  25. };
  26. window.addEventListener('resize', onResize);
  27. let backendConfig = null;
  28. try {
  29. backendConfig = await getBackendConfig();
  30. console.log('Backend config:', backendConfig);
  31. } catch (error) {
  32. console.error('Error loading backend config:', error);
  33. }
  34. // Initialize i18n even if we didn't get a backend config,
  35. // so `/error` can show something that's not `undefined`.
  36. initI18n(backendConfig?.default_locale);
  37. if (backendConfig) {
  38. // Save Backend Status to Store
  39. await config.set(backendConfig);
  40. await WEBUI_NAME.set(backendConfig.name);
  41. if ($config) {
  42. if (localStorage.token) {
  43. // Get Session User Info
  44. const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
  45. toast.error(error);
  46. return null;
  47. });
  48. if (sessionUser) {
  49. // Save Session User to Store
  50. await user.set(sessionUser);
  51. } else {
  52. // Redirect Invalid Session User to /auth Page
  53. localStorage.removeItem('token');
  54. await goto('/auth');
  55. }
  56. } else {
  57. await goto('/auth');
  58. }
  59. }
  60. } else {
  61. // Redirect to /error when Backend Not Detected
  62. await goto(`/error`);
  63. }
  64. await tick();
  65. document.getElementById('splash-screen')?.remove();
  66. loaded = true;
  67. return () => {
  68. window.removeEventListener('resize', onResize);
  69. };
  70. });
  71. </script>
  72. <svelte:head>
  73. <title>{$WEBUI_NAME}</title>
  74. <link crossorigin="anonymous" rel="icon" href="{WEBUI_BASE_URL}/static/favicon.png" />
  75. <!-- rosepine themes have been disabled as it's not up to date with our latest version. -->
  76. <!-- feel free to make a PR to fix if anyone wants to see it return -->
  77. <!-- <link rel="stylesheet" type="text/css" href="/themes/rosepine.css" />
  78. <link rel="stylesheet" type="text/css" href="/themes/rosepine-dawn.css" /> -->
  79. </svelte:head>
  80. {#if loaded}
  81. <slot />
  82. {/if}
  83. <Toaster richColors position="top-center" />