From daaf37a1ed3760f03fceb1123ebe80de3a2f280c Mon Sep 17 00:00:00 2001
From: Kit La Touche <kit@transneptune.net>
Date: Sun, 16 Feb 2025 22:00:57 -0500
Subject: Separate channel metadata out into its own store

This is stored locally, and, while parallel to channel info, is not the
same as.

Eventually, this may hold info about moot/decayed channels, and grow
unbounded. That'll need to be addressed.
---
 ui/routes/(app)/+layout.svelte | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

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

diff --git a/ui/routes/(app)/+layout.svelte b/ui/routes/(app)/+layout.svelte
index cbfef54..9ade399 100644
--- a/ui/routes/(app)/+layout.svelte
+++ b/ui/routes/(app)/+layout.svelte
@@ -6,7 +6,7 @@
   import TinyGesture from 'tinygesture';
 
   import { boot, subscribeToEvents } from '$lib/apiServer';
-  import { currentUser, logins, channelsList, messages } from '$lib/store';
+  import { currentUser, logins, channelsList, channelsMetaList, messages } from '$lib/store';
 
   import ChannelList from '$lib/components/ChannelList.svelte';
   import CreateChannelForm from '$lib/components/CreateChannelForm.svelte';
@@ -23,6 +23,10 @@
   channelsList.subscribe((val) => {
     rawChannels = val.channels;
   });
+  let rawChannelsMeta;
+  channelsMetaList.subscribe((val) => {
+    rawChannelsMeta = val.channelsMeta;
+  });
   let rawMessages;
   messages.subscribe((val) => {
     rawMessages = val;
@@ -30,7 +34,9 @@
 
   let enrichedChannels = $derived.by(() => {
     const channels = rawChannels;
+    const channelsMeta = rawChannelsMeta;
     const messages = rawMessages;
+
     const enrichedChannels = [];
     if (channels && messages) {
       for (let ch of channels) {
@@ -38,7 +44,7 @@
         let lastRun = runs?.slice(-1)[0];
         let lastMessage = lastRun?.messages.slice(-1)[0];
         let lastMessageAt = lastMessage?.at;
-        let hasUnreads = lastMessageAt > ch.lastReadAt;
+        let hasUnreads = lastMessageAt > channelsMeta[ch.id]?.lastReadAt;
         enrichedChannels.push({
           ...ch,
           hasUnreads
-- 
cgit v1.2.3


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 +++++++++++++++++++++++++++++----
 ui/routes/+layout.svelte       | 27 +--------------------------
 2 files changed, 30 insertions(+), 30 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} />
diff --git a/ui/routes/+layout.svelte b/ui/routes/+layout.svelte
index 18c0541..5bccd2f 100644
--- a/ui/routes/+layout.svelte
+++ b/ui/routes/+layout.svelte
@@ -1,14 +1,11 @@
 <script>
   import { setContext } from 'svelte';
-  import { browser } from '$app/environment';
-  import { goto, onNavigate, afterNavigate } from '$app/navigation';
+  import { onNavigate } from '$app/navigation';
   import { page } from '$app/stores';
   import '../app.css';
   import logo from '$lib/assets/logo.png';
-  import { STORE_KEY_LAST_ACTIVE } from '$lib/constants';
   import { currentUser } from '$lib/store';
 
-  let activeChannel = $derived($page.params.channel);
 
   let pageContext = $state({
     showMenu: false
@@ -20,28 +17,6 @@
     pageContext.showMenu = !pageContext.showMenu;
   }
 
-  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();
-    if (!activeChannel && lastActiveChannel) {
-      goto(`/ch/${lastActiveChannel}`);
-    } else {
-      setLastActiveChannel(activeChannel || null);
-    }
-  });
-
   onNavigate(() => {
     pageContext.showMenu = false;
   });
-- 
cgit v1.2.3


From 770400bcb5953dcb762b5c80d90e8e26e778b504 Mon Sep 17 00:00:00 2001
From: Owen Jacobson <owen@grimoire.ca>
Date: Mon, 24 Feb 2025 12:26:20 -0500
Subject: Move constant closer to usage

---
 ui/lib/constants.js            | 1 -
 ui/routes/(app)/+layout.svelte | 3 ++-
 2 files changed, 2 insertions(+), 2 deletions(-)

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

diff --git a/ui/lib/constants.js b/ui/lib/constants.js
index 5456414..f854f5d 100644
--- a/ui/lib/constants.js
+++ b/ui/lib/constants.js
@@ -1,4 +1,3 @@
 export const EPOCH_STRING = '1970-01-01T00:00:00Z';
 
 export const STORE_KEY_CHANNELS_DATA = 'pilcrow:channelsData';
-export const STORE_KEY_LAST_ACTIVE = 'pilcrow:lastActiveChannel';
diff --git a/ui/routes/(app)/+layout.svelte b/ui/routes/(app)/+layout.svelte
index 20620d4..572a551 100644
--- a/ui/routes/(app)/+layout.svelte
+++ b/ui/routes/(app)/+layout.svelte
@@ -8,7 +8,6 @@
   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';
@@ -115,6 +114,8 @@
     return '';
   }
 
+  const STORE_KEY_LAST_ACTIVE = 'pilcrow:lastActiveChannel';
+
   function getLastActiveChannel() {
     return (
       browser && JSON.parse(localStorage.getItem(STORE_KEY_LAST_ACTIVE))
-- 
cgit v1.2.3


From f2c415dd7eb1cb68e18e96dfd70460f8972ee9df Mon Sep 17 00:00:00 2001
From: Owen Jacobson <owen@grimoire.ca>
Date: Mon, 24 Feb 2025 12:27:24 -0500
Subject: npm run fmt

---
 ui/lib/store.js                 |  5 ++---
 ui/lib/store/channels.svelte.js | 18 +++++++-----------
 ui/routes/(app)/+layout.svelte  | 18 ++++++++++--------
 ui/routes/+layout.svelte        |  1 -
 4 files changed, 19 insertions(+), 23 deletions(-)

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

diff --git a/ui/lib/store.js b/ui/lib/store.js
index afced4c..57b5cce 100644
--- a/ui/lib/store.js
+++ b/ui/lib/store.js
@@ -6,9 +6,8 @@ import { Logins } from '$lib/store/logins';
 import { STORE_KEY_CHANNELS_DATA } from '$lib/constants';
 
 // Get channelsList content from the local storage
-const channelsMetaData = (
-  browser && JSON.parse(localStorage.getItem(STORE_KEY_CHANNELS_DATA))
-) || {};
+const channelsMetaData =
+  (browser && JSON.parse(localStorage.getItem(STORE_KEY_CHANNELS_DATA))) || {};
 
 export const currentUser = writable(null);
 export const logins = writable(new Logins());
diff --git a/ui/lib/store/channels.svelte.js b/ui/lib/store/channels.svelte.js
index 31e9098..9058d86 100644
--- a/ui/lib/store/channels.svelte.js
+++ b/ui/lib/store/channels.svelte.js
@@ -1,8 +1,7 @@
 import { DateTime } from 'luxon';
-import { get } from 'svelte/store'
+import { get } from 'svelte/store';
 import { STORE_KEY_CHANNELS_DATA, EPOCH_STRING } from '$lib/constants';
 
-
 export class Channels {
   channels = $state([]);
 
@@ -24,7 +23,7 @@ export class Channels {
   }
 
   addChannel(id, name) {
-    const newChannel = { id, name }
+    const newChannel = { id, name };
     this.channels = [...this.channels, newChannel];
     get(this.channelsMetaList).initializeChannel(newChannel);
     this.sort();
@@ -66,17 +65,14 @@ export class ChannelsMeta {
       }
       return {
         ...ch,
-        lastReadAt,
+        lastReadAt
       };
     });
     this.channelsMeta = channelsMeta;
   }
 
   writeOutToLocalStorage() {
-    localStorage.setItem(
-      STORE_KEY_CHANNELS_DATA,
-      JSON.stringify(this.channelsMeta)
-    );
+    localStorage.setItem(STORE_KEY_CHANNELS_DATA, JSON.stringify(this.channelsMeta));
   }
 
   updateLastReadAt(channelId, at) {
@@ -100,7 +96,7 @@ export class ChannelsMeta {
       const channelData = {
         lastReadAt: null,
         draft: '',
-        scrollPosition: null,
+        scrollPosition: null
       };
       this.channelsMeta[channelId] = channelData;
     }
@@ -113,7 +109,7 @@ export class ChannelsMeta {
 
 function objectMap(object, mapFn) {
   return Object.keys(object).reduce((result, key) => {
-    result[key] = mapFn(object[key])
-    return result
+    result[key] = mapFn(object[key]);
+    return result;
   }, {});
 }
diff --git a/ui/routes/(app)/+layout.svelte b/ui/routes/(app)/+layout.svelte
index 572a551..67fea64 100644
--- a/ui/routes/(app)/+layout.svelte
+++ b/ui/routes/(app)/+layout.svelte
@@ -8,7 +8,14 @@
   import TinyGesture from 'tinygesture';
 
   import { boot, subscribeToEvents } from '$lib/apiServer';
-  import { channelsList, channelsMetaList, currentUser, logins, messages, onEvent } from '$lib/store';
+  import {
+    channelsList,
+    channelsMetaList,
+    currentUser,
+    logins,
+    messages,
+    onEvent
+  } from '$lib/store';
   import ChannelList from '$lib/components/ChannelList.svelte';
   import CreateChannelForm from '$lib/components/CreateChannelForm.svelte';
 
@@ -117,16 +124,11 @@
   const STORE_KEY_LAST_ACTIVE = 'pilcrow:lastActiveChannel';
 
   function getLastActiveChannel() {
-    return (
-      browser && JSON.parse(localStorage.getItem(STORE_KEY_LAST_ACTIVE))
-    );
+    return browser && JSON.parse(localStorage.getItem(STORE_KEY_LAST_ACTIVE));
   }
 
   function setLastActiveChannel(channelId) {
-    browser && localStorage.setItem(
-      STORE_KEY_LAST_ACTIVE,
-      JSON.stringify(channelId)
-    );
+    browser && localStorage.setItem(STORE_KEY_LAST_ACTIVE, JSON.stringify(channelId));
   }
 
   afterNavigate(() => {
diff --git a/ui/routes/+layout.svelte b/ui/routes/+layout.svelte
index 5bccd2f..6c19a95 100644
--- a/ui/routes/+layout.svelte
+++ b/ui/routes/+layout.svelte
@@ -6,7 +6,6 @@
   import logo from '$lib/assets/logo.png';
   import { currentUser } from '$lib/store';
 
-
   let pageContext = $state({
     showMenu: false
   });
-- 
cgit v1.2.3

