aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/+layout.ts
blob: b2b71c26432c342cc8050dcc0c8f9d14166df1b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { browser } from "$app/environment";
import { theme as themeStore } from "$lib/store";
import { addTranslations, setLocale, setRoute } from "$lib/translations/index";
import { getCookie, setCookie } from "$lib/utils";

/** @type {import('@sveltejs/kit').Load} */
export const load = async ({ data }) => {
  const { i18n, translations } = data as any;
  const { locale, route } = i18n;

  addTranslations(translations);

  await setRoute(route);
  await setLocale(locale);

  if (browser) {
    let theme = getCookie("theme");

    if (!theme) {
      theme = matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";

      setCookie("theme", theme, 21000000);

      if (theme === "dark") setTimeout(() => themeStore.set(theme!), 100);
    }
  }

  return data;
};