Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/agent/detail/AllowedDomains.tsx
iris c9e6cd2e61 swarm ui: landmarks, a skip link, a page heading, named repeated buttons and a log role on the terminal
The shell now has a main landmark that takes the focus when a page opens, a skip link as the first Tab stop, a labelled nav, aria-current on the active tab, and a hidden h1 with the page's name (no page drew one). The tab close button and tab link get a visible focus ring and a larger target. Repeated edit, remove, delete, fire now, pause, duplicate buttons and the icon-only terminal, expand and close badges get a name that says what they act on (Button gains ariaLabel). A badge can be pressed (aria-pressed), used for the internet modes and the SSH target form, whose fake tablist is now a labelled group. The terminal is a focusable role=log with live announcements off, and a latest badge scrolls back to the newest row once the operator has scrolled up and the terminal has stopped following.
2026-11-20 00:23:07 +01:00

250 lines
8.9 KiB
TypeScript

// <AllowedDomains> — the destinations the swarm lets one agent reach through
// its hive's egress proxy, beyond the base list every agent has: the entries
// that hold for every agent and the ones for this agent alone, with an editor
// to add and remove them.
//
// Reads `GET /api/egress/allowed-domains`, which answers every entry, and
// shows the ones that apply to this agent. Adding POSTs one entry and removing
// DELETEs one, each with the entry as the body, each answering the whole list.
// The route's refusal (an IP address, a wildcard, a name the proxy already
// allows everyone) shows as it arrives, next to the box. The hive reads a
// change within about 30 seconds.
//
// An entry adds to the names the hive's own configuration allows, which stay
// allowed. An agent that holds the unrestricted
// internet flag reaches any name the proxy's private-range rules leave, so an
// entry matters only to a restricted one.
//
// An entry the controller keeps from the agent's backend environment or a linked
// account (`derived`) shows as automatic with no remove control; the route
// refuses to remove it.
//
// Its state belongs to one agent: callers key it by agent name.
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 { ActionRow } from "../../ui/action-row/ActionRow.js";
import { BadgePopover } from "../../ui/badge-popover/BadgePopover.js";
import { Button } from "../../ui/button/Button.js";
import { FormStack } from "../../ui/form-stack/FormStack.js";
import { CheckboxField } from "../../ui/checkbox-field/CheckboxField.js";
import { Dialog } from "../../ui/dialog/Dialog.js";
import { Panel } from "../../ui/panel/Panel.js";
import { TextField } from "../../ui/text-field/TextField.js";
import {
ALLOWED_DOMAINS_URL,
DENIED_DOMAINS_URL,
entriesFor,
entryBody,
entryGroups,
type EntryGroup,
} from "./allowed-domains.js";
import { useAllowedDomains } from "./useAllowedDomains.js";
import { BadgeSkeleton } from "../../ui/skeleton/BadgeSkeleton.js";
import "../../ui/badge-row/BadgeRow.css";
import "./AgentLimits.css";
// What each source's badge says, and what its list holds.
const GROUP_LABEL: Record<EntryGroup["source"], string> = {
"swarm-wide": "swarm-wide",
own: "this agent",
automatic: "automatic",
};
function groupHelp(source: EntryGroup["source"], agent: string): string {
switch (source) {
case "swarm-wide":
return "Names every agent in the swarm may reach through its hive's proxy.";
case "own":
return `Names only ${agent} may reach through its hive's proxy. Remove one to stop allowing it.`;
case "automatic":
return `Kept from ${agent}'s backend environment or a linked account; change or unlink that to change them.`;
}
}
export function AllowedDomains({ agent }: { agent: string }) {
const { entries, error, busy, remove, refetch } = useAllowedDomains();
const [adding, setAdding] = useState(false);
// A list already loaded stays under a failed refetch, so a change that
// worked is not hidden by a read that did not.
const errorPanel = error ? (
<>
<ApiErrorPanel
context="the allowed domains were not read or changed"
problem={error}
/>
<Button onClick={() => refetch()}>retry</Button>
</>
) : null;
if (entries === null) {
return (
errorPanel ?? <BadgeSkeleton what="the allowed domains" width="7em" />
);
}
const shown = entriesFor(entries, agent);
return (
<div class="ui-agent-limits">
{errorPanel}
<div class="ui-badge-row">
{shown.length === 0 ? (
<Badge label="allowed domains" value="none added" />
) : null}
{entryGroups(shown).map((g) => (
<BadgePopover
key={g.source}
label={GROUP_LABEL[g.source]}
value={g.entries.length}
title={groupHelp(g.source, agent)}
popoverLabel={`${agent} allowed domains, ${GROUP_LABEL[g.source]}`}
>
<p class="ui-badge-popover-help">{groupHelp(g.source, agent)}</p>
<ul class="ui-badge-popover-list">
{g.entries.map((e) => (
<li key={`${e.agent ?? "*"}/${e.domain}`}>
<Badge value={e.domain} />{" "}
{e.derived === true ? null : (
<Badge
variant="quiet"
value="remove"
ariaLabel={`remove ${e.domain}`}
title={`stop allowing ${e.domain} for ${
e.agent === undefined ? "every agent" : agent
}`}
disabled={busy}
onClick={() => void remove(e)}
/>
)}
</li>
))}
</ul>
</BadgePopover>
))}
<Badge
variant="quiet"
value="add"
title={`allow a domain for ${agent} or for every agent`}
onClick={() => setAdding(true)}
/>
</div>
<Dialog
open={adding}
onClose={() => setAdding(false)}
label="add an allowed domain"
plain
>
{/* Remounted per open, so a cancelled add leaves nothing behind. */}
{adding ? (
<AddForm
agent={agent}
onClose={() => setAdding(false)}
onAdded={() => {
setAdding(false);
refetch();
}}
/>
) : null}
</Dialog>
</div>
);
}
// The add form of one allowed or denied domain: for `agent`, or for every
// agent. With no `agent` (the swarm page) it can only add a swarm-wide entry.
export function AddForm({
agent,
kind = "allow",
onClose,
onAdded,
}: {
agent?: string;
kind?: "allow" | "deny";
onClose: () => void;
onAdded: () => void;
}) {
const deny = kind === "deny";
const [domain, setDomain] = useState("");
const [forSwarm, setForSwarm] = useState(agent === undefined);
const [busy, setBusy] = useState(false);
const [problem, setProblem] = useState<ProblemDetails | null>(null);
async function save(e: Event) {
e.preventDefault();
setBusy(true);
setProblem(null);
try {
const r = await fetch(deny ? DENIED_DOMAINS_URL : ALLOWED_DOMAINS_URL, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(entryBody(domain, forSwarm, agent ?? "")),
});
if (!r.ok) {
// The box stays as typed, so the operator can fix it and add again.
setProblem(await readApiError(r));
return;
}
onAdded();
} catch (err: unknown) {
setProblem({ detail: String(err) });
} finally {
setBusy(false);
}
}
return (
<Panel
title={`${deny ? "denied" : "allowed"} domain — ${agent ?? "swarm-wide"}`}
icon={"🌐\uFE0F"}
onClose={onClose}
>
<p>
{deny
? agent === undefined
? "Stops every agent reaching one name through its hive's proxy."
: `Stops ${agent} reaching one name through its hive's proxy, or every agent when the box below is ticked.`
: agent === undefined
? "Lets every agent reach one more name through its hive's proxy."
: `Lets ${agent} reach one more name through its hive's proxy, or every agent when the box below is ticked.`}{" "}
A name needs at least two labels, such as <code>github.com</code>; a
leading dot, as in <code>.githubusercontent.com</code>, also{" "}
{deny ? "refuses" : "allows"} every subdomain. IP addresses and
wildcards are refused.{" "}
{deny
? "A denied name is refused in every egress mode, even when a swarm entry or a hive's own configuration allows it."
: "A name that resolves to a private address stays blocked. The entry adds to the names each hive's own configuration allows."}{" "}
The hive reads a change within about 30 seconds.
</p>
<FormStack onSubmit={(e) => void save(e)}>
<TextField
id={deny ? "denied-domain" : "allowed-domain"}
label="domain"
value={domain}
placeholder="e.g. github.com"
onInput={setDomain}
/>
{agent === undefined ? null : (
<CheckboxField
id={deny ? "denied-domain-swarm" : "allowed-domain-swarm"}
label="for every agent in the swarm"
checked={forSwarm}
onChange={setForSwarm}
/>
)}
{problem ? (
<ApiErrorPanel context="the domain was not added" problem={problem} />
) : null}
<ActionRow>
<Button
variant="primary"
type="submit"
disabled={busy || domain.trim() === ""}
>
{busy ? "adding…" : "add"}
</Button>
</ActionRow>
</FormStack>
</Panel>
);
}