fix(panel): dedupe notifications by id in the store

addNotification prepended every delivery and re-incremented unreadCount /
pendingAckCount, so a re-fetched or replayed notification stacked duplicates
and re-inflated the counts — already-acknowledged notifications re-surfaced to
the CEO and the pending badge kept climbing. Dedupe by id: update in place on
re-delivery; only add + count a genuinely new notification.
This commit is contained in:
Renn F
2026-06-04 06:47:30 +02:00
parent 2db9741ff6
commit d25994f87b
+20 -5
View File
@@ -21,11 +21,26 @@ export const useNotificationStore = create<NotificationState>((set) => ({
pendingAckCount: 0,
addNotification: (notification) =>
set((state) => ({
notifications: [notification, ...state.notifications].slice(0, 50),
unreadCount: state.unreadCount + (notification.is_read ? 0 : 1),
pendingAckCount: state.pendingAckCount + (notification.requires_ack && !notification.is_acknowledged ? 1 : 0),
})),
set((state) => {
// Dedupe by id. A notification re-delivered (re-fetch / WebSocket replay)
// must update in place, not stack a duplicate or re-increment the counts —
// otherwise already-acknowledged notifications re-surfaced to the CEO and
// the pending badge kept climbing.
if (state.notifications.some((n) => n.id === notification.id)) {
return {
notifications: state.notifications.map((n) =>
n.id === notification.id ? notification : n
),
};
}
return {
notifications: [notification, ...state.notifications].slice(0, 50),
unreadCount: state.unreadCount + (notification.is_read ? 0 : 1),
pendingAckCount:
state.pendingAckCount +
(notification.requires_ack && !notification.is_acknowledged ? 1 : 0),
};
}),
markAsRead: (id) =>
set((state) => ({