import { useEffect, useRef } from "react"; /** * Calls `tick` every `ms` while the tab is visible; hidden ticks are skipped and one catch-up tick fires * when the tab becomes visible again. The latest `tick` is always the one called. (Same hook as the * customer portal's use-visible-polling.) */ export function useVisiblePolling(tick: () => void, ms: number, enabled = true): void { const latest = useRef(tick); useEffect(() => { latest.current = tick; }); useEffect(() => { if (!enabled) return; const run = () => { if (document.visibilityState === "visible") latest.current(); }; const id = setInterval(run, ms); document.addEventListener("visibilitychange", run); return () => { clearInterval(id); document.removeEventListener("visibilitychange", run); }; }, [ms, enabled]); }