From bee20bccda598b833ced376856351727f385108b Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Sat, 24 Sep 2022 15:37:46 +0800
Subject: feat: Add LocaleSwitcher globally

---
 apps/kit/src/routes/+layout.svelte | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

(limited to 'apps/kit/src/routes/+layout.svelte')

diff --git a/apps/kit/src/routes/+layout.svelte b/apps/kit/src/routes/+layout.svelte
index 688673b..cbb4856 100644
--- a/apps/kit/src/routes/+layout.svelte
+++ b/apps/kit/src/routes/+layout.svelte
@@ -1,18 +1,27 @@
 <script lang="ts">
     import "../app.pcss";
-    import {afterNavigate, beforeNavigate, goto} from "$app/navigation";
-    import {is_active} from "$lib/session";
-    import type {Navigation} from "@sveltejs/kit";
-    import {setLocale} from "$lib/i18n/i18n-svelte";
-    import {onMount} from "svelte";
-    import type {LayoutData} from "./$types";
+    import { afterNavigate, beforeNavigate, goto } from "$app/navigation";
+    import { is_active } from "$lib/session";
+    import type { Navigation } from "@sveltejs/kit";
+    import { setLocale } from "$lib/i18n/i18n-svelte";
+    import { onMount } from "svelte";
+    import type { LayoutData } from "./$types";
+    import LocaleSwitcher from "$lib/components/locale-switcher.svelte";
 
     export let data: LayoutData;
     onMount(() => setLocale(data.locale));
 
     async function redirect_if_necessary(ticket: Navigation) {
         const sessionIsValid = await is_active();
-        const isPublicRoute = ticket.to?.routeId?.startsWith("(public)");
+        // TODO: ticket.to can be empty while navigating, so coalesce could probably cause non-public routes to cause a redir to /login...
+        const isPublicRoute =
+            ticket.to?.routeId?.startsWith("(public)") ?? true;
+
+        console.log("redir: ", {
+            isPublicRoute,
+            sessionIsValid,
+        });
+
         if (sessionIsValid && isPublicRoute) {
             await goto("/home");
         } else if (!sessionIsValid && !isPublicRoute) {
@@ -27,4 +36,5 @@
     beforeNavigate(redirect_if_necessary);
 </script>
 
-<slot></slot>
\ No newline at end of file
+<LocaleSwitcher />
+<slot />
-- 
cgit v1.3

