From 2978d9197740ecf5740ef20ff60161abea68eb80 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Thu, 14 Nov 2024 12:16:44 -0800 Subject: [PATCH] Add mobile nav notification badges --- src/app/components/MenuSpace.svelte | 81 ++++++++---------------- src/app/components/MenuSpaces.svelte | 22 +------ src/app/components/MenuSpacesItem.svelte | 27 ++++++++ src/app/components/PrimaryNav.svelte | 6 +- src/app/notifications.ts | 23 ++++++- src/lib/components/Delay.svelte | 11 ---- src/lib/components/PrimaryNavItem.svelte | 12 ++-- src/routes/+layout.svelte | 5 +- src/routes/chat/+layout.svelte | 28 ++++---- src/routes/chat/+page.svelte | 7 ++ src/routes/spaces/[relay]/+layout.svelte | 37 ++++++----- 11 files changed, 128 insertions(+), 131 deletions(-) create mode 100644 src/app/components/MenuSpacesItem.svelte delete mode 100644 src/lib/components/Delay.svelte diff --git a/src/app/components/MenuSpace.svelte b/src/app/components/MenuSpace.svelte index 96bdb330..11263682 100644 --- a/src/app/components/MenuSpace.svelte +++ b/src/app/components/MenuSpace.svelte @@ -1,7 +1,7 @@ + + + +
+
+ + {#if $notification} +
+ {/if} +
+
+ + diff --git a/src/app/components/PrimaryNav.svelte b/src/app/components/PrimaryNav.svelte index 0ea7e1fa..eecfcd34 100644 --- a/src/app/components/PrimaryNav.svelte +++ b/src/app/components/PrimaryNav.svelte @@ -9,7 +9,7 @@ import PrimaryNavItemSpace from "@app/components/PrimaryNavItemSpace.svelte" import {userMembership, getMembershipUrls, PLATFORM_RELAY, PLATFORM_LOGO} from "@app/state" import {pushModal} from "@app/modal" - import {deriveNotification, CHAT_FILTERS} from "@app/notifications" + import {deriveNotification, spacesNotification, CHAT_FILTERS} from "@app/notifications" const chatNotification = deriveNotification("/chat", CHAT_FILTERS) @@ -76,10 +76,10 @@ - + - +
diff --git a/src/app/notifications.ts b/src/app/notifications.ts index e559f8eb..87f49ab0 100644 --- a/src/app/notifications.ts +++ b/src/app/notifications.ts @@ -4,7 +4,15 @@ import {repository, pubkey} from "@welshman/app" import {prop, max, sortBy, assoc, lt, now} from "@welshman/lib" import type {Filter} from "@welshman/util" import {DIRECT_MESSAGE} from "@welshman/util" -import {MESSAGE, THREAD, COMMENT, deriveEventsForUrl} from "@app/state" +import {makeSpacePath} from "@app/routes" +import { + MESSAGE, + THREAD, + COMMENT, + deriveEventsForUrl, + getMembershipUrls, + userMembership, +} from "@app/state" // Checked state @@ -49,3 +57,16 @@ export const deriveNotification = (path: string, filters: Filter[], url?: string }, ) } + +export const spacesNotification = derived( + [pubkey, checked, userMembership, deriveEvents(repository, {filters: SPACE_FILTERS})], + ([$pubkey, $checked, $userMembership, $events]) => { + return getMembershipUrls($userMembership).some(url => { + const path = makeSpacePath(url) + const lastChecked = max([$checked["*"], $checked[path]]) + const [latestEvent] = sortBy($e => -$e.created_at, $events) + + return latestEvent?.pubkey !== $pubkey && lt(lastChecked, latestEvent?.created_at) + }) + }, +) diff --git a/src/lib/components/Delay.svelte b/src/lib/components/Delay.svelte deleted file mode 100644 index 19f8f42f..00000000 --- a/src/lib/components/Delay.svelte +++ /dev/null @@ -1,11 +0,0 @@ - - -{#await sleep(delay)} - -{:then} - -{/await} diff --git a/src/lib/components/PrimaryNavItem.svelte b/src/lib/components/PrimaryNavItem.svelte index fa791b2c..c3c64db1 100644 --- a/src/lib/components/PrimaryNavItem.svelte +++ b/src/lib/components/PrimaryNavItem.svelte @@ -17,10 +17,10 @@ class:bg-base-300={active} class:tooltip={title} data-tip={title}> - {#if !active && notification} -
- {/if} + {#if !active && notification} +
+ {/if}
{:else} @@ -30,10 +30,10 @@ class:bg-base-300={active} class:tooltip={title} data-tip={title}> - {#if !active && notification} -
- {/if} + {#if !active && notification} +
+ {/if}
{/if} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 2e2b2136..f25003a7 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -2,6 +2,7 @@ import "@src/app.css" import {onMount} from "svelte" import {get, derived} from "svelte/store" + import {page} from "$app/stores" import {dev} from "$app/environment" import {identity, uniq, sleep, take, sortBy, ago, now, HOUR, WEEK, Worker} from "@welshman/lib" import type {TrustedEvent} from "@welshman/util" @@ -229,7 +230,9 @@ {:then}
- + {#key $page.url.pathname} + + {/key}
diff --git a/src/routes/chat/+layout.svelte b/src/routes/chat/+layout.svelte index 7e433b9a..5b0e00ba 100644 --- a/src/routes/chat/+layout.svelte +++ b/src/routes/chat/+layout.svelte @@ -1,6 +1,5 @@