feat: implement real-time message and notification count synchronization via custom events and socket listeners
This commit is contained in:
@@ -170,6 +170,9 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
|||||||
setConversations((prev) =>
|
setConversations((prev) =>
|
||||||
prev.map((c) => (c.id === conversationId ? { ...c, unreadCount: 0 } : c))
|
prev.map((c) => (c.id === conversationId ? { ...c, unreadCount: 0 } : c))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Notify header to instantly refresh message + notification counts
|
||||||
|
window.dispatchEvent(new Event('messages-read'));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to load conversation:', error);
|
console.error('Failed to load conversation:', error);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -331,6 +334,7 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
|||||||
} else {
|
} else {
|
||||||
await messagesService.markAsRead(currentConversationIdRef.current);
|
await messagesService.markAsRead(currentConversationIdRef.current);
|
||||||
}
|
}
|
||||||
|
window.dispatchEvent(new Event('messages-read'));
|
||||||
}, [isConnected]);
|
}, [isConnected]);
|
||||||
|
|
||||||
// Mark all conversations as read
|
// Mark all conversations as read
|
||||||
@@ -342,6 +346,7 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
|||||||
setConversations((prev) =>
|
setConversations((prev) =>
|
||||||
prev.map((c) => ({ ...c, unreadCount: 0 }))
|
prev.map((c) => ({ ...c, unreadCount: 0 }))
|
||||||
);
|
);
|
||||||
|
window.dispatchEvent(new Event('messages-read'));
|
||||||
}, [conversations]);
|
}, [conversations]);
|
||||||
|
|
||||||
// Delete a conversation
|
// Delete a conversation
|
||||||
@@ -497,6 +502,8 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
|||||||
} else {
|
} else {
|
||||||
messagesService.markAsRead(message.conversationId).catch(() => {});
|
messagesService.markAsRead(message.conversationId).catch(() => {});
|
||||||
}
|
}
|
||||||
|
// Notify header to refresh counts since we auto-read the message
|
||||||
|
window.dispatchEvent(new Event('messages-read'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user