swarm-ui: one polled-resource hook for the refreshing pages
Each polling page repeated its own fetch-and-set code on a fixed timer, so a slow answer could be overtaken by the next tick, an older response could overwrite a newer one, and nothing aborted a request when the page went away. usePolled skips a tick while a request is in flight, aborts on unmount or refresh, ignores answers from dropped requests, and keeps the last good data under an error. The alerts, hives, agents, schedules, builds, review runs and broadcast pages and the subagent list use it.
This commit is contained in:
parent
9338496e24
commit
8917ba27b6
1 changed files with 274 additions and 160 deletions
|
|
@ -9,7 +9,7 @@
|
|||
// multiselects). A card being edited keeps its form state across the
|
||||
// periodic refetch because `FilterableView` keys cards by schedule id and
|
||||
// the open edit is held here by id, not by position.
|
||||
import { useCallback, useEffect, useMemo, useState } from "preact/hooks";
|
||||
import { useEffect, useMemo, useState } from "preact/hooks";
|
||||
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
||||
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
||||
import { Button } from "../../ui/button/Button.js";
|
||||
|
|
@ -18,9 +18,9 @@ import { FilterableView } from "../../ui/filterable-view/FilterableView.js";
|
|||
import { Panel } from "../../ui/panel/Panel.js";
|
||||
import {
|
||||
RefreshIntervalPicker,
|
||||
useRefreshInterval,
|
||||
type RefreshIntervalMs,
|
||||
} from "../../ui/refresh-interval/RefreshInterval.js";
|
||||
import { fetchJson, usePolled } from "../../ui/polled/usePolled.js";
|
||||
import type { TableColumn } from "../../ui/table/Table.js";
|
||||
import type { AgentRow } from "../../agent/AgentTypes.js";
|
||||
import { ScheduleCard } from "./ScheduleCard.js";
|
||||
|
|
@ -69,8 +69,13 @@ async function loadAgentNames(): Promise<string[]> {
|
|||
export function SchedulesPage() {
|
||||
const [intervalMs, setIntervalMs] =
|
||||
useState<RefreshIntervalMs>(DEFAULT_INTERVAL_MS);
|
||||
const [schedules, setSchedules] = useState<Schedule[] | null>(null);
|
||||
const [error, setError] = useState<ProblemDetails | null>(null);
|
||||
const {
|
||||
data: schedules,
|
||||
error,
|
||||
refresh,
|
||||
} = usePolled(intervalMs, (signal) =>
|
||||
fetchJson<Schedule[]>("/api/schedules", signal),
|
||||
);
|
||||
const [agentNames, setAgentNames] = useState<string[]>([]);
|
||||
// The schedule whose card is currently its edit form, and whether the
|
||||
// "new schedule" form is open above the list.
|
||||
|
|
@ -80,22 +85,6 @@ export function SchedulesPage() {
|
|||
const [removeTarget, setRemoveTarget] = useState<Schedule | null>(null);
|
||||
const [actionError, setActionError] = useState<ProblemDetails | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
const r = await fetch("/api/schedules");
|
||||
if (!r.ok) {
|
||||
setError(await readApiError(r));
|
||||
return;
|
||||
}
|
||||
setSchedules((await r.json()) as Schedule[]);
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
setError({ detail: String(err) });
|
||||
}
|
||||
}, []);
|
||||
|
||||
useRefreshInterval(intervalMs, load);
|
||||
|
||||
useEffect(() => {
|
||||
loadAgentNames().then(setAgentNames, () => setAgentNames([]));
|
||||
}, []);
|
||||
|
|
@ -121,7 +110,7 @@ export function SchedulesPage() {
|
|||
setActionError(await readApiError(r));
|
||||
return;
|
||||
}
|
||||
await load();
|
||||
refresh();
|
||||
} catch (err) {
|
||||
setActionError({ detail: String(err) });
|
||||
} finally {
|
||||
|
|
@ -158,10 +147,10 @@ export function SchedulesPage() {
|
|||
[agentNames],
|
||||
);
|
||||
|
||||
async function saved() {
|
||||
function saved() {
|
||||
setCreating(false);
|
||||
setEditingId(null);
|
||||
await load();
|
||||
refresh();
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
|
|||
Loading…
Reference in a new issue