// The swarm's allowed-domain and denied-domain entries and the one change that // is not a form: removing an entry. Shared by an agent card's allowed domains // and the swarm page's egress card, which differ only in which entries they // show. // // Reads `GET ` (every entry) and removes one with a `DELETE` carrying the // 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 { 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"; interface Listing { entries: AllowedDomain[]; behind?: string[]; } function useDomainList(url: string) { // The error of a removal; a read that fails shows its own. const [removeError, setRemoveError] = useState(null); const [busy, setBusy] = useState(false); const { data, error: readError, reload, } = useResource((signal) => fetchJson(url, signal), [url]); /** True when the entry was removed. */ async function remove(entry: AllowedDomain): Promise { if (busy) return false; setBusy(true); try { const r = await fetch(url, { method: "DELETE", headers: { "content-type": "application/json" }, body: JSON.stringify(entry), }); if (!r.ok) { setRemoveError(await readApiError(r)); return false; } setRemoveError(null); reload(); return true; } catch (e: unknown) { setRemoveError({ detail: String(e) }); return false; } finally { setBusy(false); } } return { entries: data?.entries ?? null, behind: data?.behind ?? [], error: shownError(removeError, readError), busy, remove, refetch: () => { setRemoveError(null); reload(); }, }; } export const useAllowedDomains = () => useDomainList(ALLOWED_DOMAINS_URL); export const useDeniedDomains = () => useDomainList(DENIED_DOMAINS_URL);