From d2196398cc5aa243c282d91714b372f83cfb2fc5 Mon Sep 17 00:00:00 2001 From: Mekan1206 Date: Sun, 16 Aug 2026 17:33:48 +0500 Subject: [PATCH] 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. --- src/components/ChatWidget/index.jsx | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/components/ChatWidget/index.jsx b/src/components/ChatWidget/index.jsx index 34bca39..d805bed 100644 --- a/src/components/ChatWidget/index.jsx +++ b/src/components/ChatWidget/index.jsx @@ -59,7 +59,7 @@ function ChatWidget() { const { data: inboxData, error: inboxError } = useGetChatConversationsQuery(identity, { skip: !ready || hidden, - pollingInterval: 15000, + pollingInterval: open ? 4000 : 15000, }); const inbox = Array.isArray(inboxData) ? inboxData : []; const [fetchTargets] = useLazyGetChatTargetsQuery(); @@ -250,13 +250,14 @@ function ChatWidget() { }, [appendMessage, conversation?.id, hidden, markRead, screen]); useEffect(() => { - if (!open || screen !== "thread" || !conversation?.id || getChatEcho()) { + if (!open || screen !== "thread" || !conversation?.id) { return undefined; } const conversationId = conversation.id; - const timer = setInterval(async () => { - if (requestGen.current && conversationRef.current?.id !== conversationId) { + + const refresh = async () => { + if (conversationRef.current?.id !== conversationId) { return; } try { @@ -277,9 +278,21 @@ function ChatWidget() { } catch { // 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]); const handleScroll = async () => {