// — the named SSH connections of one agent, a folded // section of its SSH popover: a table of them with a remove button each, and a // form to add one. // // A connection is `name; user; host; port`. It becomes an `ssh ` entry in // the agent's ssh config, and the agent's hive lets it reach that host and port. // It only takes effect while the agent's ssh switch is on, so the section says // so when it is off, and stays usable then: the list is what the operator set. // // Reads `GET /api/agents/{name}/ssh-connections` when the section is first // opened. A save is a `PUT` of one connection, which replaces one of the same // name, so the form says "replace" and names it when the name is taken. A save // answers the whole list; a remove is a `DELETE`. The controller does all the // checking, and the form shows its refusal as it arrives. // // Its state belongs to one agent: callers key it by agent name. 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 { ActionRow } from "../../ui/action-row/ActionRow.js"; import { Button } from "../../ui/button/Button.js"; import { useConfirm } from "../../ui/confirm-dialog/useConfirm.js"; import { FormStack } from "../../ui/form-stack/FormStack.js"; import { Hint } from "../../ui/hint/Hint.js"; import { TextField } from "../../ui/text-field/TextField.js"; import { connectionBody, connectionUrl, connectionsUrl, replaces, sortedConnections, type SshConnection, type SshConnections, } from "./ssh-connections.js"; import "./AgentSshConnections.css"; export function AgentSshConnections({ agent, on, }: { agent: string; on: boolean; }) { // Read on the first open, so a card nobody asks about reads nothing. const [opened, setOpened] = useState(false); return (
{ if ((e.currentTarget as HTMLDetailsElement).open) setOpened(true); }} > connections {opened ? : null}
); } function Connections({ agent, on }: { agent: string; on: boolean }) { const [list, setList] = useState(null); const [loadError, setLoadError] = useState(null); const [name, setName] = useState(""); const [user, setUser] = useState(""); const [host, setHost] = useState(""); const [port, setPort] = useState("22"); const [busy, setBusy] = useState(false); const [problem, setProblem] = useState(null); const [confirm, confirmDialog] = useConfirm(); async function load() { setLoadError(null); try { const r = await fetch(connectionsUrl(agent)); if (!r.ok) { setLoadError(await readApiError(r)); return; } setList(((await r.json()) as SshConnections).connections); } catch (e: unknown) { setLoadError({ detail: String(e) }); } } useEffect(() => { void load(); }, [agent]); async function save(e: Event) { e.preventDefault(); const body = connectionBody(user, host, port); if (body === null || name.trim() === "") return; setBusy(true); setProblem(null); try { const r = await fetch(connectionUrl(agent, name.trim()), { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(body), }); if (!r.ok) { // The form stays as typed, so the operator can fix it and save again. setProblem(await readApiError(r)); return; } setList(((await r.json()) as SshConnections).connections); setName(""); setUser(""); setHost(""); setPort("22"); } catch (err: unknown) { setProblem({ detail: String(err) }); } finally { setBusy(false); } } async function remove(c: SshConnection) { if ( !(await confirm({ label: `remove the connection ${c.name}`, confirmLabel: "remove", danger: true, body: (

Remove {c.name} ({c.user}@{c.host}:{c.port})? {agent}{" "} can no longer ssh {c.name}, and its hive stops letting it reach that host.

), })) ) { return; } setBusy(true); setProblem(null); try { const r = await fetch(connectionUrl(agent, c.name), { method: "DELETE" }); if (!r.ok) { setProblem(await readApiError(r)); return; } setList((l) => (l ?? []).filter((x) => x.name !== c.name)); } catch (err: unknown) { setProblem({ detail: String(err) }); } finally { setBusy(false); } } function edit(c: SshConnection) { setName(c.name); setUser(c.user); setHost(c.host); setPort(String(c.port)); setProblem(null); } const replacing = list !== null && replaces(list, name); const complete = name.trim() !== "" && connectionBody(user, host, port) !== null; return (

A named connection is an ssh <name> entry in {agent} 's ssh config, and its hive lets {agent} reach that host and port. {on ? null : " Inactive while ssh is off."}

{loadError ? ( <> ) : list === null ? ( reading the connections… ) : list.length === 0 ? ( no connections ) : (
{sortedConnections(list).map((c) => ( ))}
name connects to actions
{c.name} {c.user}@{c.host}:{c.port} edit(c)} />{" "} void remove(c)} />
)} void save(e)}> {replacing ? ( There is already a connection named {name.trim()}; saving replaces it. ) : null} {problem ? ( ) : null} {confirmDialog}
); }