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.
This commit is contained in:
Jure
2026-03-25 19:10:40 +01:00
parent bb084182d7
commit 0189433269
2 changed files with 12 additions and 3 deletions

View File

@@ -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);
}

View File

@@ -74,11 +74,18 @@ export const useNotificationsStore = create<NotificationsState>((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 });
}