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
12 changed files with 274 additions and 160 deletions
|
|
@ -19,9 +19,9 @@ import { Button } from "../../ui/button/Button.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 { TextAreaField } from "../../ui/text-area-field/TextAreaField.js";
|
||||
import {
|
||||
failedAgents,
|
||||
|
|
@ -58,10 +58,11 @@ function storedFilter(): AgentFilter {
|
|||
}
|
||||
|
||||
export function BroadcastPage() {
|
||||
const [agents, setAgents] = useState<BroadcastAgent[] | null>(null);
|
||||
const [loadError, setLoadError] = useState<ProblemDetails | null>(null);
|
||||
const [intervalMs, setIntervalMs] =
|
||||
useState<RefreshIntervalMs>(DEFAULT_INTERVAL_MS);
|
||||
const { data: agents, error: loadError } = usePolled(intervalMs, (signal) =>
|
||||
fetchJson<BroadcastAgent[]>("/api/agents/status", signal),
|
||||
);
|
||||
const [filter, setFilterState] = useState<AgentFilter>(storedFilter);
|
||||
const [ticked, setTicked] = useState<string[]>([]);
|
||||
const [body, setBody] = useState("");
|
||||
|
|
@ -76,20 +77,6 @@ export function BroadcastPage() {
|
|||
}
|
||||
}
|
||||
|
||||
async function refresh() {
|
||||
const res = await fetch("/api/agents/status");
|
||||
if (!res.ok) {
|
||||
setLoadError(await readApiError(res));
|
||||
return;
|
||||
}
|
||||
setAgents((await res.json()) as BroadcastAgent[]);
|
||||
setLoadError(null);
|
||||
}
|
||||
|
||||
useRefreshInterval(intervalMs, () => {
|
||||
refresh().catch((e: unknown) => setLoadError({ detail: String(e) }));
|
||||
});
|
||||
|
||||
const all = agents ?? [];
|
||||
const shown = visibleAgents(all, filter);
|
||||
const targets = selectedVisible(ticked, shown);
|
||||
|
|
|
|||
Loading…
Reference in a new issue