Introduce discriminated FetchError union type to replace weak string error handling in API calls and hooks. Enables actionable error diagnostics. Changes: - Create types/api.ts with FetchError discriminated union (api_error, network_error, abort_error) - Export type guards: isAuthError, isAbortError, isNetworkError, isApiError - Update useListData and usePolledData to expose typed FetchError instead of string - Add getErrorMessage() helper to extract displayable messages from FetchError - Add createStringErrorAdapter() for backward compatibility with string error state - Update handleFetchError() to work with both FetchError and string setters - Update all consumer hooks to expose typed errors - Update components to use getErrorMessage() when displaying errors - Update tests to mock FetchError instead of strings - Add comprehensive typed error model documentation to Web-Development.md This enables better error handling patterns: - Check error.type to distinguish between API, network, and abort errors - Extract status codes for specific handling (401/403 auth, 50x server errors) - Maintain backward compatibility with existing string-based error states All TypeScript compilation passes with no errors. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
/**
|
|
* React hook for fetching and updating the blocklist import schedule.
|
|
*/
|
|
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import { fetchSchedule, updateSchedule } from "../api/blocklist";
|
|
import { handleFetchError, createStringErrorAdapter } from "../utils/fetchError";
|
|
import type { ScheduleConfig, ScheduleInfo } from "../types/blocklist";
|
|
|
|
export interface UseScheduleReturn {
|
|
info: ScheduleInfo | null;
|
|
loading: boolean;
|
|
error: string | null;
|
|
saveSchedule: (config: ScheduleConfig) => Promise<void>;
|
|
refresh: () => void;
|
|
}
|
|
|
|
/**
|
|
* Fetch and update the blocklist import schedule.
|
|
*/
|
|
export function useSchedule(): UseScheduleReturn {
|
|
const [info, setInfo] = useState<ScheduleInfo | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const abortRef = useRef<AbortController | null>(null);
|
|
|
|
const refresh = useCallback((): void => {
|
|
abortRef.current?.abort();
|
|
const controller = new AbortController();
|
|
abortRef.current = controller;
|
|
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
fetchSchedule(controller.signal)
|
|
.then((data) => {
|
|
if (controller.signal.aborted) return;
|
|
setInfo(data);
|
|
})
|
|
.catch((err: unknown) => {
|
|
if (controller.signal.aborted) return;
|
|
handleFetchError(err, createStringErrorAdapter(setError), "Failed to load schedule");
|
|
})
|
|
.finally(() => {
|
|
if (!controller.signal.aborted) {
|
|
setLoading(false);
|
|
}
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
refresh();
|
|
|
|
return (): void => {
|
|
abortRef.current?.abort();
|
|
};
|
|
}, [refresh]);
|
|
|
|
const saveSchedule = useCallback(async (config: ScheduleConfig): Promise<void> => {
|
|
const updated = await updateSchedule(config);
|
|
setInfo(updated);
|
|
}, []);
|
|
|
|
return { info, loading, error, saveSchedule, refresh };
|
|
}
|