From 018943326921a64ae4df62111fbb1ddc90d6b588 Mon Sep 17 00:00:00 2001 From: Jure <44338+hoornet@users.noreply.github.com> Date: Wed, 25 Mar 2026 19:10:40 +0100 Subject: [PATCH] Fix notification badges disappearing on relay timeout Don't overwrite existing notifications with empty results when relay times out or disconnects. Also fetch notification counts immediately on startup instead of waiting for the first poll cycle. --- src/lib/notificationPoller.ts | 4 +++- src/stores/notifications.ts | 11 +++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/lib/notificationPoller.ts b/src/lib/notificationPoller.ts index 7af3948..d3d6efc 100644 --- a/src/lib/notificationPoller.ts +++ b/src/lib/notificationPoller.ts @@ -109,7 +109,9 @@ async function pollOnce(pubkey: string) { export function startNotificationPoller(pubkey: string) { stopNotificationPoller(); - // Run first poll after a short delay (let relays connect) + // Fetch notification counts immediately (before full poll) + useNotificationsStore.getState().fetchNotifications(pubkey).catch(() => {}); + // Run first full poll after a short delay (let relays connect) setTimeout(() => pollOnce(pubkey).catch(() => {}), 5000); intervalId = setInterval(() => pollOnce(pubkey).catch(() => {}), POLL_INTERVAL); } diff --git a/src/stores/notifications.ts b/src/stores/notifications.ts index 2e99f88..599aa05 100644 --- a/src/stores/notifications.ts +++ b/src/stores/notifications.ts @@ -74,11 +74,18 @@ export const useNotificationsStore = create((set, get) => ({ // Filter out own events — your replies shouldn't be notifications const others = events.filter((e) => e.pubkey !== pubkey); const sorted = others.sort((a, b) => (b.created_at ?? 0) - (a.created_at ?? 0)).slice(0, MAX_NOTIFICATIONS); - const { readIds } = get(); + + // Don't overwrite existing notifications with empty results (relay timeout/disconnect) + const { readIds, notifications: existing } = get(); + if (sorted.length === 0 && existing.length > 0) { + // Keep existing notifications — relay probably timed out + return; + } + const unreadCount = sorted.filter((e) => !readIds.has(e.id!)).length; set({ notifications: sorted, unreadCount }); } catch { - // Non-critical + // Non-critical — keep existing notifications on error } finally { set({ loading: false }); }