diff options
| author | Kit La Touche <kit@transneptune.net> | 2025-02-17 12:50:33 -0500 |
|---|---|---|
| committer | Kit La Touche <kit@transneptune.net> | 2025-02-20 21:53:25 -0500 |
| commit | 098be35aade77e505d692558cfb554485b8563db (patch) | |
| tree | 82cfdfd8ea42cba8e7e3b426ee04790719101830 /ui/routes/+layout.svelte | |
| parent | daaf37a1ed3760f03fceb1123ebe80de3a2f280c (diff) | |
Remember last active channel and navigate there on root load
To facilitate PWA behaviour.
Diffstat (limited to 'ui/routes/+layout.svelte')
| -rw-r--r-- | ui/routes/+layout.svelte | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/ui/routes/+layout.svelte b/ui/routes/+layout.svelte index 750f1f8..18c0541 100644 --- a/ui/routes/+layout.svelte +++ b/ui/routes/+layout.svelte @@ -1,11 +1,15 @@ <script> import { setContext } from 'svelte'; - import { onNavigate } from '$app/navigation'; + import { browser } from '$app/environment'; + import { goto, onNavigate, afterNavigate } 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 }); @@ -16,6 +20,28 @@ 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; }); |
