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:
parent
6f73bf0c55
commit
7f8d6abc58
6 changed files with 279 additions and 134 deletions
|
|
@ -12,10 +12,12 @@
|
|||
//
|
||||
// Its state belongs to one agent: callers key it by agent name, so switching
|
||||
// agents remounts it instead of showing the previous agent's caps.
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { useState } from "preact/hooks";
|
||||
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
||||
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
||||
import { Badge } from "@hive/shared/badge.js";
|
||||
import { fetchJson } from "../../ui/polled/usePolled.js";
|
||||
import { useResource } from "../../ui/polled/useResource.js";
|
||||
import { ActionRow } from "../../ui/action-row/ActionRow.js";
|
||||
import { Button } from "../../ui/button/Button.js";
|
||||
import { FormStack } from "../../ui/form-stack/FormStack.js";
|
||||
|
|
@ -93,31 +95,17 @@ export function LimitsFields({
|
|||
}
|
||||
|
||||
export function AgentLimits({ agent }: { agent: string }) {
|
||||
const [caps, setCaps] = useState<Caps | null>(null);
|
||||
const [error, setError] = useState<ProblemDetails | null>(null);
|
||||
// Bumped after a save or a clear, so the caps are fetched again.
|
||||
const [reload, setReload] = useState(0);
|
||||
const [editing, setEditing] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
const r = await fetch(limitsUrl(agent));
|
||||
if (!r.ok) {
|
||||
if (!cancelled) setError(await readApiError(r));
|
||||
return;
|
||||
}
|
||||
const body = (await r.json()) as Caps;
|
||||
if (cancelled) return;
|
||||
setCaps(body);
|
||||
setError(null);
|
||||
})().catch((e: unknown) => {
|
||||
if (!cancelled) setError({ detail: String(e) });
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [agent, reload]);
|
||||
// Read again after a save or a clear.
|
||||
const {
|
||||
data: caps,
|
||||
error,
|
||||
reload,
|
||||
} = useResource(
|
||||
(signal) => fetchJson<Caps>(limitsUrl(agent), signal),
|
||||
[agent],
|
||||
);
|
||||
|
||||
// Shown as text, not a hover title. Caps already loaded stay under a
|
||||
// failed refetch, so a save that worked is not hidden by a read that did not.
|
||||
|
|
@ -127,7 +115,7 @@ export function AgentLimits({ agent }: { agent: string }) {
|
|||
context="reading the resource caps failed"
|
||||
problem={error}
|
||||
/>
|
||||
<Button onClick={() => setReload((n) => n + 1)}>retry</Button>
|
||||
<Button onClick={() => reload()}>retry</Button>
|
||||
</>
|
||||
) : null;
|
||||
if (caps === null) {
|
||||
|
|
@ -168,7 +156,7 @@ export function AgentLimits({ agent }: { agent: string }) {
|
|||
onClose={() => setEditing(false)}
|
||||
onSaved={() => {
|
||||
setEditing(false);
|
||||
setReload((n) => n + 1);
|
||||
reload();
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ import { useEffect, useState } from "preact/hooks";
|
|||
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
||||
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
||||
import { Badge } from "@hive/shared/badge.js";
|
||||
import { fetchJson } from "../../ui/polled/usePolled.js";
|
||||
import { useResource } from "../../ui/polled/useResource.js";
|
||||
import { BadgePopover } from "../../ui/badge-popover/BadgePopover.js";
|
||||
import { Button } from "../../ui/button/Button.js";
|
||||
import { useConfirm } from "../../ui/confirm-dialog/useConfirm.js";
|
||||
|
|
@ -157,31 +159,13 @@ export function AgentPermissions({
|
|||
g: GrantState;
|
||||
}) {
|
||||
const { grant, busy, saveError, loading, send } = g;
|
||||
const [catalogue, setCatalogue] = useState<Catalogue | null>(null);
|
||||
const [catalogueError, setCatalogueError] = useState<ProblemDetails | null>(
|
||||
null,
|
||||
const { data: catalogue, error: catalogueError } = useResource(
|
||||
(signal) => fetchJson<Catalogue>("/api/permissions", signal),
|
||||
[],
|
||||
);
|
||||
// The boxes as ticked while a change is being saved; null shows the stored grant.
|
||||
const [selected, setSelected] = useState<string[] | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
const r = await fetch("/api/permissions");
|
||||
if (!r.ok) {
|
||||
if (!cancelled) setCatalogueError(await readApiError(r));
|
||||
return;
|
||||
}
|
||||
const data = (await r.json()) as Catalogue;
|
||||
if (!cancelled) setCatalogue(data);
|
||||
})().catch((e: unknown) => {
|
||||
if (!cancelled) setCatalogueError({ detail: String(e) });
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, []);
|
||||
|
||||
if (grant === null) return loading;
|
||||
|
||||
const stored = grant.permissions;
|
||||
|
|
|
|||
|
|
@ -10,10 +10,11 @@
|
|||
// built in `ssh.ts`, prefilled with the key and this agent's name.
|
||||
//
|
||||
// Its state belongs to one agent: callers key it by agent name.
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { useState } from "preact/hooks";
|
||||
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
||||
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
||||
import { Badge } from "@hive/shared/badge.js";
|
||||
import { fetchJson } from "../../ui/polled/usePolled.js";
|
||||
import { useResource } from "../../ui/polled/useResource.js";
|
||||
import { BadgePopover } from "../../ui/badge-popover/BadgePopover.js";
|
||||
import { Button } from "../../ui/button/Button.js";
|
||||
import { CheckboxField } from "../../ui/checkbox-field/CheckboxField.js";
|
||||
|
|
@ -35,26 +36,12 @@ export function AgentSsh({ agent, g }: { agent: string; g: GrantState }) {
|
|||
const [form, setForm] = useState<TargetForm>("nixos");
|
||||
const [copied, setCopied] = useState(false);
|
||||
// The CA key, or why there is none: a store without a CA yet answers 404.
|
||||
const [ca, setCa] = useState<string | null>(null);
|
||||
const [caError, setCaError] = useState<ProblemDetails | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
const r = await fetch("/api/ssh/ca");
|
||||
if (!r.ok) {
|
||||
if (!cancelled) setCaError(await readApiError(r));
|
||||
return;
|
||||
}
|
||||
const body = (await r.json()) as { public_key: string };
|
||||
if (!cancelled) setCa(body.public_key);
|
||||
})().catch((e: unknown) => {
|
||||
if (!cancelled) setCaError({ detail: String(e) });
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, []);
|
||||
const { data: caKey, error: caError } = useResource(
|
||||
(signal) => fetchJson<{ public_key: string }>("/api/ssh/ca", signal),
|
||||
[],
|
||||
);
|
||||
const ca = caKey?.public_key ?? null;
|
||||
|
||||
if (grant === null) return loading;
|
||||
|
||||
|
|
|
|||
|
|
@ -9,13 +9,15 @@
|
|||
//
|
||||
// Its state belongs to one agent: callers key it by hive and agent, so
|
||||
// switching agents remounts it instead of showing the previous agent's rows.
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { useState } from "preact/hooks";
|
||||
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
||||
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
||||
import { Badge } from "@hive/shared/badge.js";
|
||||
import { CheckboxField } from "../../ui/checkbox-field/CheckboxField.js";
|
||||
import { ConfirmDialog } from "../../ui/confirm-dialog/ConfirmDialog.js";
|
||||
import { Hint } from "../../ui/hint/Hint.js";
|
||||
import { fetchJson } from "../../ui/polled/usePolled.js";
|
||||
import { useResource } from "../../ui/polled/useResource.js";
|
||||
import { accountKindLabel } from "./account-kind.js";
|
||||
import { deleteUrl, listUrl } from "./account-api.js";
|
||||
import "./LinkedAccounts.css";
|
||||
|
|
@ -39,10 +41,6 @@ export function LinkedAccounts({
|
|||
agent: string;
|
||||
version: number;
|
||||
}) {
|
||||
const [accounts, setAccounts] = useState<LinkedAccount[] | null>(null);
|
||||
const [error, setError] = useState<ProblemDetails | null>(null);
|
||||
// Bumped after a delete, so the list is fetched again.
|
||||
const [reload, setReload] = useState(0);
|
||||
// The row whose delete confirmation is open; null means closed.
|
||||
const [deleteTarget, setDeleteTarget] = useState<LinkedAccount | null>(null);
|
||||
// Matrix only: log the token out at its homeserver before deleting.
|
||||
|
|
@ -50,25 +48,14 @@ export function LinkedAccounts({
|
|||
const [deleting, setDeleting] = useState(false);
|
||||
const [deleteError, setDeleteError] = useState<ProblemDetails | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
const r = await fetch(listUrl(hive, agent));
|
||||
if (!r.ok) {
|
||||
if (!cancelled) setError(await readApiError(r));
|
||||
return;
|
||||
}
|
||||
const data = (await r.json()) as LinkedAccount[];
|
||||
if (cancelled) return;
|
||||
setAccounts(data);
|
||||
setError(null);
|
||||
})().catch((e: unknown) => {
|
||||
if (!cancelled) setError({ detail: String(e) });
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [hive, agent, version, reload]);
|
||||
const {
|
||||
data: accounts,
|
||||
error,
|
||||
reload,
|
||||
} = useResource(
|
||||
(signal) => fetchJson<LinkedAccount[]>(listUrl(hive, agent), signal),
|
||||
[hive, agent, version],
|
||||
);
|
||||
|
||||
function openDelete(a: LinkedAccount) {
|
||||
setDeleteTarget(a);
|
||||
|
|
@ -92,7 +79,7 @@ export function LinkedAccounts({
|
|||
return;
|
||||
}
|
||||
setDeleteTarget(null);
|
||||
setReload((n) => n + 1);
|
||||
reload();
|
||||
} catch (e: unknown) {
|
||||
setDeleteError({ detail: String(e) });
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -106,3 +106,14 @@ export function behindNote(behind: string[]): string | null {
|
|||
? `${names} has not applied this list yet, so its proxy may still let these names through.`
|
||||
: `${names} have not applied this list yet, so their proxies may still let these names through.`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The error the list shows: a failed removal first, since it is the newer
|
||||
* answer to what the operator just did; otherwise a failed read.
|
||||
*/
|
||||
export function shownError<P>(
|
||||
removeError: P | null,
|
||||
readError: P | null,
|
||||
): P | null {
|
||||
return removeError ?? readError;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue