update
This commit is contained in:
@@ -56,19 +56,43 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({ children }) => {
|
||||
const connected = data?.connected ?? false;
|
||||
const loginName = data?.loginName ?? null;
|
||||
|
||||
const prevUnreadRef = useRef<Map<string, number>>(new Map());
|
||||
const isInitializedRef = useRef(false);
|
||||
|
||||
// Reset initialization flag when disconnected
|
||||
useEffect(() => {
|
||||
if (!isConnected) {
|
||||
isInitializedRef.current = false;
|
||||
}
|
||||
}, [isConnected]);
|
||||
|
||||
// Detect new unread messages while panel is closed and show toast
|
||||
useEffect(() => {
|
||||
if (!rooms.length) return;
|
||||
const prev = prevUnreadRef.current;
|
||||
const isFirstLoad = prev.size === 0;
|
||||
|
||||
if (!isInitializedRef.current) {
|
||||
// First load (or after reconnect) — initialize without toasting
|
||||
for (const room of rooms) {
|
||||
prev.set(room.token, room.unreadMessages);
|
||||
}
|
||||
isInitializedRef.current = true;
|
||||
return;
|
||||
}
|
||||
|
||||
for (const room of rooms) {
|
||||
const prevCount = prev.get(room.token) ?? 0;
|
||||
if (!isFirstLoad && !chatPanelOpen && room.unreadMessages > prevCount) {
|
||||
if (!chatPanelOpen && room.unreadMessages > prevCount) {
|
||||
showNotificationToast(room.displayName, 'info');
|
||||
}
|
||||
prev.set(room.token, room.unreadMessages);
|
||||
}
|
||||
|
||||
// Prune entries for rooms no longer in the list
|
||||
const currentTokens = new Set(rooms.map((r) => r.token));
|
||||
for (const key of prev.keys()) {
|
||||
if (!currentTokens.has(key)) prev.delete(key);
|
||||
}
|
||||
}, [rooms, chatPanelOpen, showNotificationToast]);
|
||||
|
||||
const selectRoom = useCallback((token: string | null) => {
|
||||
|
||||
Reference in New Issue
Block a user