- Add usePageVisibility hook to track page visibility state - Add pauseWhenHidden option to usePolledData (defaults to false for backward compatibility) - When enabled, polling pauses when page is hidden and resumes with immediate refresh when visible - Refactor useBlocklistStatus to use usePolledData with pauseWhenHidden=true - Add comprehensive tests for usePageVisibility hook - Add polling lifecycle documentation to Web-Development.md Fixes #36: Polling continues when tab is not visible Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
31 lines
881 B
TypeScript
31 lines
881 B
TypeScript
/**
|
|
* React hook for polling blocklist schedule error state.
|
|
*/
|
|
|
|
import { usePolledData } from "./usePolledData";
|
|
import { fetchSchedule } from "../api/blocklist";
|
|
|
|
const BLOCKLIST_POLL_INTERVAL_MS = 60_000;
|
|
|
|
export interface UseBlocklistStatusReturn {
|
|
hasErrors: boolean;
|
|
}
|
|
|
|
/**
|
|
* Poll `GET /api/blocklists/schedule` every 60 seconds to detect whether
|
|
* the most recent blocklist import had errors.
|
|
*
|
|
* Polling pauses when the page is hidden and resumes immediately when visible.
|
|
*/
|
|
export function useBlocklistStatus(): UseBlocklistStatusReturn {
|
|
const { data } = usePolledData({
|
|
fetcher: (signal) => fetchSchedule(signal),
|
|
selector: (response) => response.last_run_errors === true,
|
|
errorMessage: "Failed to fetch blocklist schedule",
|
|
pollInterval: BLOCKLIST_POLL_INTERVAL_MS,
|
|
pauseWhenHidden: true,
|
|
});
|
|
|
|
return { hasErrors: data ?? false };
|
|
}
|