Watch
0
0
Fork
You've already forked hyperhive
0

swarm-ui: one hook for a read that runs once per set of inputs

Part of the unification order in the UI sweep issue (step d, first of three).

Twenty files hand-rolled the same effect: a cancelled flag, a fetch, the
problem body on a bad status, a catch that wraps the error. useResource does
it once on top of the poller's request discipline: it aborts the request in
flight when the inputs change or the page goes away, keeps the last good
answer under a failed read and clears the error on the next good one. The
agent details move to it (limits, linked accounts, ssh CA, permission
catalogue, allowed and denied domains). Hint drops its HintTone alias for the
shared Tone.
This commit is contained in:
iris 2026-11-25 22:48:29 +01:00
commit 7f8d6abc58

View file

@ -7,11 +7,14 @@
// entry, each answering the whole list. `refetch` reads the list again, for a
// caller that added an entry through its own form. The deny list also reports
// `behind`, the hives that have not taken its current revision yet.
import { useEffect, useState } from "preact/hooks";
import { useState } from "preact/hooks";
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
import { fetchJson } from "../../ui/polled/usePolled.js";
import { useResource } from "../../ui/polled/useResource.js";
import {
ALLOWED_DOMAINS_URL,
DENIED_DOMAINS_URL,
shownError,
type AllowedDomain,
} from "./allowed-domains.js";
@ -21,33 +24,15 @@ interface Listing {
}
function useDomainList(url: string) {
const [entries, setEntries] = useState<AllowedDomain[] | null>(null);
const [behind, setBehind] = useState<string[]>([]);
const [error, setError] = useState<ProblemDetails | null>(null);
// Bumped after an add or a removal, so the list is fetched again.
const [reload, setReload] = useState(0);
// The error of a removal; a read that fails shows its own.
const [removeError, setRemoveError] = useState<ProblemDetails | null>(null);
const [busy, setBusy] = useState(false);
useEffect(() => {
let cancelled = false;
(async () => {
const r = await fetch(url);
if (!r.ok) {
if (!cancelled) setError(await readApiError(r));
return;
}
const body = (await r.json()) as Listing;
if (cancelled) return;
setEntries(body.entries);
setBehind(body.behind ?? []);
setError(null);
})().catch((e: unknown) => {
if (!cancelled) setError({ detail: String(e) });
});
return () => {
cancelled = true;
};
}, [url, reload]);
const {
data,
error: readError,
reload,
} = useResource((signal) => fetchJson<Listing>(url, signal), [url]);
async function remove(entry: AllowedDomain) {
if (busy) return;
@ -59,24 +44,28 @@ function useDomainList(url: string) {
body: JSON.stringify(entry),
});
if (!r.ok) {
setError(await readApiError(r));
setRemoveError(await readApiError(r));
return;
}
setReload((n) => n + 1);
setRemoveError(null);
reload();
} catch (e: unknown) {
setError({ detail: String(e) });
setRemoveError({ detail: String(e) });
} finally {
setBusy(false);
}
}
return {
entries,
behind,
error,
entries: data?.entries ?? null,
behind: data?.behind ?? [],
error: shownError(removeError, readError),
busy,
remove,
refetch: () => setReload((n) => n + 1),
refetch: () => {
setRemoveError(null);
reload();
},
};
}