From e561ff74b81209b8e0f0c7cc63861a2ef70d19bf Mon Sep 17 00:00:00 2001
From: Kit La Touche <kit@transneptune.net>
Date: Fri, 21 Feb 2025 23:19:30 -0500
Subject: Only redirect to last active channel off of /

---
 ui/routes/(app)/+layout.svelte | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

(limited to 'ui/routes/(app)/+layout.svelte')

diff --git a/ui/routes/(app)/+layout.svelte b/ui/routes/(app)/+layout.svelte
index 888a185..20620d4 100644
--- a/ui/routes/(app)/+layout.svelte
+++ b/ui/routes/(app)/+layout.svelte
@@ -1,13 +1,15 @@
 <script>
-  import { page } from '$app/state';
-  import { goto } from '$app/navigation';
-  import { browser } from '$app/environment';
   import { getContext, onDestroy, onMount } from 'svelte';
+
+  import { browser } from '$app/environment';
+  import { goto, afterNavigate } from '$app/navigation';
+  import { page } from '$app/state';
+
   import TinyGesture from 'tinygesture';
 
   import { boot, subscribeToEvents } from '$lib/apiServer';
+  import { STORE_KEY_LAST_ACTIVE } from '$lib/constants';
   import { channelsList, channelsMetaList, currentUser, logins, messages, onEvent } from '$lib/store';
-
   import ChannelList from '$lib/components/ChannelList.svelte';
   import CreateChannelForm from '$lib/components/CreateChannelForm.svelte';
 
@@ -112,6 +114,29 @@
     event.returnValue = '';
     return '';
   }
+
+  function getLastActiveChannel() {
+    return (
+      browser && JSON.parse(localStorage.getItem(STORE_KEY_LAST_ACTIVE))
+    );
+  }
+
+  function setLastActiveChannel(channelId) {
+    browser && localStorage.setItem(
+      STORE_KEY_LAST_ACTIVE,
+      JSON.stringify(channelId)
+    );
+  }
+
+  afterNavigate(() => {
+    const lastActiveChannel = getLastActiveChannel();
+    const inRoot = page.url.pathname === '/';
+    if (inRoot && lastActiveChannel) {
+      goto(`/ch/${lastActiveChannel}`);
+    } else if (channel) {
+      setLastActiveChannel(channel || null);
+    }
+  });
 </script>
 
 <svelte:window {onbeforeunload} />
-- 
cgit v1.2.3

