Files
BanGUI/frontend/src/hooks/useBlocklistStatus.ts
Lukas 336242ad06 Implement visibility-aware polling to reduce background tab resource usage
- 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>
2026-04-29 20:01:25 +02:00

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 };
}