From 1dae207b6ea11f07cf6fee9479b020bb314dad62 Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 16 Jan 2026 01:39:42 +0100 Subject: [PATCH] refactor Notifications rc --- hooks/use-notifications.ts | 5 ++++- hooks/use-rocketchat-calls.ts | 6 ++++-- .../notifications/notification-registry.ts | 20 +++++++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/hooks/use-notifications.ts b/hooks/use-notifications.ts index 7278e5e..7d9b9c6 100644 --- a/hooks/use-notifications.ts +++ b/hooks/use-notifications.ts @@ -208,7 +208,10 @@ export function useNotifications() { const handleNotificationUpdate = (event: CustomEvent) => { console.log('[useNotifications] Received notification update event', event.detail); // Refresh count immediately when widget updates - fetchNotificationCount(false); // Use cache, widget already updated it + // Use a small delay to ensure the registry has been updated + setTimeout(() => { + fetchNotificationCount(false); // Use cache, widget already updated it + }, 100); }; // Listen for custom event from widgets diff --git a/hooks/use-rocketchat-calls.ts b/hooks/use-rocketchat-calls.ts index 2e5f863..8efa258 100644 --- a/hooks/use-rocketchat-calls.ts +++ b/hooks/use-rocketchat-calls.ts @@ -116,13 +116,15 @@ export function useRocketChatCalls() { }); // Trigger notification + // For calls, we want to increment the existing count, not replace it + // So we fetch current count first, then increment triggerNotification({ source: 'rocketchat', - count: 1, // Increment count for call + count: 1, // This will be added to existing count in the registry items: [ { id: `call-${callEvent.roomId}-${Date.now()}`, - title: `Appel entrant de ${callEvent.from.name || callEvent.from.username}`, + title: `📞 Appel entrant de ${callEvent.from.name || callEvent.from.username}`, message: `Appel vidéo/audio dans ${callEvent.roomName || 'chat'}`, link: `/parole?room=${callEvent.roomId}`, timestamp: callEvent.timestamp, diff --git a/lib/services/notifications/notification-registry.ts b/lib/services/notifications/notification-registry.ts index 08d0a40..ca7feac 100644 --- a/lib/services/notifications/notification-registry.ts +++ b/lib/services/notifications/notification-registry.ts @@ -62,11 +62,23 @@ export class NotificationRegistry { } // Update count for this source + // If count is 1 and it's a call, increment. Otherwise, set the count. const previousSourceCount = currentCount.sources[source]?.unread || 0; - currentCount.sources[source] = { - total: count, - unread: count, - }; + const isCallIncrement = count === 1 && items && items.length > 0 && items[0].metadata?.type === 'call'; + + if (isCallIncrement) { + // For calls, increment the existing count + currentCount.sources[source] = { + total: previousSourceCount + count, + unread: previousSourceCount + count, + }; + } else { + // For regular updates, set the count (widgets send their total) + currentCount.sources[source] = { + total: count, + unread: count, + }; + } // Recalculate total currentCount.unread = Object.values(currentCount.sources).reduce(