aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/+layout.ts
blob: 0fcf5ec802c2bea5fe782e1a20dc1e2a039869e2 (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
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);
    }
  }
};