swarm-ui: the egress card on the swarm page lists and edits the swarm-wide denied domains, and says which hives have not applied them
This commit is contained in:
parent
0bc350a8a1
commit
0421f28d49
1 changed files with 210 additions and 103 deletions
|
|
@ -1,20 +1,28 @@
|
|||
// The swarm's allowed-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.
|
||||
// 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 /api/egress/allowed-domains` (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.
|
||||
// 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 { useEffect, useState } from "preact/hooks";
|
||||
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
||||
import { ALLOWED_DOMAINS_URL, type AllowedDomain } from "./allowed-domains.js";
|
||||
import {
|
||||
ALLOWED_DOMAINS_URL,
|
||||
DENIED_DOMAINS_URL,
|
||||
type AllowedDomain,
|
||||
} from "./allowed-domains.js";
|
||||
|
||||
interface Listing {
|
||||
entries: AllowedDomain[];
|
||||
behind?: string[];
|
||||
}
|
||||
|
||||
export function useAllowedDomains() {
|
||||
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);
|
||||
|
|
@ -23,7 +31,7 @@ export function useAllowedDomains() {
|
|||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
const r = await fetch(ALLOWED_DOMAINS_URL);
|
||||
const r = await fetch(url);
|
||||
if (!r.ok) {
|
||||
if (!cancelled) setError(await readApiError(r));
|
||||
return;
|
||||
|
|
@ -31,6 +39,7 @@ export function useAllowedDomains() {
|
|||
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) });
|
||||
|
|
@ -38,13 +47,13 @@ export function useAllowedDomains() {
|
|||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [reload]);
|
||||
}, [url, reload]);
|
||||
|
||||
async function remove(entry: AllowedDomain) {
|
||||
if (busy) return;
|
||||
setBusy(true);
|
||||
try {
|
||||
const r = await fetch(ALLOWED_DOMAINS_URL, {
|
||||
const r = await fetch(url, {
|
||||
method: "DELETE",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(entry),
|
||||
|
|
@ -63,9 +72,13 @@ export function useAllowedDomains() {
|
|||
|
||||
return {
|
||||
entries,
|
||||
behind,
|
||||
error,
|
||||
busy,
|
||||
remove,
|
||||
refetch: () => setReload((n) => n + 1),
|
||||
};
|
||||
}
|
||||
|
||||
export const useAllowedDomains = () => useDomainList(ALLOWED_DOMAINS_URL);
|
||||
export const useDeniedDomains = () => useDomainList(DENIED_DOMAINS_URL);
|
||||
|
|
|
|||
Loading…
Reference in a new issue