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
2026-08-16 17:33:48 +05:00
parent d6d5dd785d
commit d2196398cc

View File

@@ -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 () => {