19 lines
673 B
JavaScript
19 lines
673 B
JavaScript
import { useCallback } from "react";
|
|
import { useStompClient, parseStompMessage } from "./useStompClient";
|
|
import { useUnread } from "../context/UnreadContext";
|
|
import { getActiveChatId } from "../stores/activeChatStore";
|
|
|
|
export function useUnreadSocket(userId) {
|
|
const { setChatUnread } = useUnread();
|
|
|
|
const handleConnect = useCallback((client) => {
|
|
client.subscribe(`/topic/user/${userId}/unread`, (msg) => {
|
|
parseStompMessage(msg, (body) => {
|
|
if (String(body.chatId) === getActiveChatId()) return;
|
|
setChatUnread(body.chatId, body.unreadCount);
|
|
});
|
|
});
|
|
}, [userId, setChatUnread]);
|
|
|
|
useStompClient(!!userId, handleConnect);
|
|
} |