Add AbortSignal support to dashboard/blocklist APIs and hooks

This commit is contained in:
2026-04-21 17:29:05 +02:00
parent 51e340fa33
commit cf5a000bf5
7 changed files with 122 additions and 16 deletions

View File

@@ -2,7 +2,7 @@
* React hook for polling blocklist schedule error state.
*/
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { fetchSchedule } from "../api/blocklist";
const BLOCKLIST_POLL_INTERVAL_MS = 60_000;
@@ -17,16 +17,20 @@ export interface UseBlocklistStatusReturn {
*/
export function useBlocklistStatus(): UseBlocklistStatusReturn {
const [hasErrors, setHasErrors] = useState(false);
const abortRef = useRef<AbortController | null>(null);
useEffect(() => {
let cancelled = false;
const poll = (): void => {
fetchSchedule()
abortRef.current?.abort();
const controller = new AbortController();
abortRef.current = controller;
fetchSchedule(controller.signal)
.then((info) => {
if (!cancelled) {
setHasErrors(info.last_run_errors === true);
if (controller.signal.aborted) {
return;
}
setHasErrors(info.last_run_errors === true);
})
.catch(() => {
// Silently swallow network errors — do not change indicator state.
@@ -36,7 +40,7 @@ export function useBlocklistStatus(): UseBlocklistStatusReturn {
poll();
const id = window.setInterval(poll, BLOCKLIST_POLL_INTERVAL_MS);
return (): void => {
cancelled = true;
abortRef.current?.abort();
window.clearInterval(id);
};
}, []);