Part of the unification order in the UI sweep issue (steps e and f). The terminal, turn-state and message feeds each opened an EventSource, tracked connecting/open/reconnecting/closed, probed the session when the browser gave up, and reconnected through an attempt counter. openStream (no Preact, source injected, tested with a fake) and useEventStream now own that; each feed keeps only what it does with a message. A change confirmed itself three ways: a toast, inline, or nothing. The design guide now says forms confirm inline, actions on a card in a refreshing list confirm with a toast, and a change the page already shows says nothing. Fire now, saving a schedule and removing an egress entry gain the toast they lacked.
76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
// 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 <url>` (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<ProblemDetails | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
const {
|
|
data,
|
|
error: readError,
|
|
reload,
|
|
} = useResource((signal) => fetchJson<Listing>(url, signal), [url]);
|
|
|
|
/** True when the entry was removed. */
|
|
async function remove(entry: AllowedDomain): Promise<boolean> {
|
|
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);
|