// Copyright: © 2026 TWWIM UG. All rights reserved. (www.twwim.com) /** * Visibility-aware refetch toggle for TanStack Query polls. * Pauses polling when the tab is hidden; triggers an immediate refetch * when it becomes visible again so the operator catches up to the latest * messages without waiting for the next poll cadence. */ import { useEffect, useState } from 'react'; export function useVisibilityRefetch(onVisible?: () => void) { const [isVisible, setIsVisible] = useState( typeof document !== 'undefined' ? document.visibilityState === 'visible' : true, ); useEffect(() => { if (typeof document === 'undefined') return; const handler = () => { const visible = document.visibilityState === 'visible'; setIsVisible(visible); if (visible && onVisible) onVisible(); }; document.addEventListener('visibilitychange', handler); return () => document.removeEventListener('visibilitychange', handler); }, [onVisible]); return isVisible; }