Update polling behavior in ChatWidget to adjust interval based on chat visibility. Introduced a visibility change listener to refresh chat data when the document becomes visible, enhancing user experience during active conversations.

This commit is contained in:
Mekan1206 committed 2026-08-16 17:33:48 +05:00
1 parent d6d5dd785d
commit d2196398cc
1 file changed
+19 -6
+19 -6
View File
@@ -59,7 +59,7 @@ function ChatWidget() {
const { data: inboxData, error: inboxError } = useGetChatConversationsQuery(identity, { const { data: inboxData, error: inboxError } = useGetChatConversationsQuery(identity, {
skip: !ready || hidden, skip: !ready || hidden,
pollingInterval: 15000, pollingInterval: open ? 4000 : 15000,
}); });
const inbox = Array.isArray(inboxData) ? inboxData : []; const inbox = Array.isArray(inboxData) ? inboxData : [];
const [fetchTargets] = useLazyGetChatTargetsQuery(); const [fetchTargets] = useLazyGetChatTargetsQuery();
@@ -250,13 +250,14 @@ function ChatWidget() {
}, [appendMessage, conversation?.id, hidden, markRead, screen]); }, [appendMessage, conversation?.id, hidden, markRead, screen]);
useEffect(() => { useEffect(() => {
if (!open || screen !== "thread" || !conversation?.id || getChatEcho()) { if (!open || screen !== "thread" || !conversation?.id) {
return undefined; return undefined;
} }
const conversationId = conversation.id; const conversationId = conversation.id;
const timer = setInterval(async () => {
if (requestGen.current && conversationRef.current?.id !== conversationId) { const refresh = async () => {
if (conversationRef.current?.id !== conversationId) {
return; return;
} }
try { try {
@@ -277,9 +278,21 @@ function ChatWidget() {
} catch { } catch {
// polling is best-effort // polling is best-effort
} }
}, 4000); };
return () => clearInterval(timer); const onVisible = () => {
if (document.visibilityState === "visible") {
refresh();
}
};
document.addEventListener("visibilitychange", onVisible);
const timer = setInterval(refresh, 4000);
return () => {
clearInterval(timer);
document.removeEventListener("visibilitychange", onVisible);
};
}, [conversation?.id, fetchMessages, open, screen]); }, [conversation?.id, fetchMessages, open, screen]);
const handleScroll = async () => { const handleScroll = async () => {