Watch
0
0
Fork
You've already forked hyperhive
0

swarm-ui: forms share one submit flow, result line and field column

useSubmit holds the idle, sending, done and error state and the request
flow that six forms each wrote out; FormResult shows the success line and
the problem panel; FormStack is the column the link, claude-login,
inference and limits forms each styled by hand. The create-agent, link
account, backend-env and agent-input forms use the submit flow; the link,
claude-login, inference and limits forms use FormStack.
This commit is contained in:
iris 2026-11-06 18:47:34 +01:00 • committed by mara
commit 811b23cd6b

View file

@ -26,14 +26,13 @@
// `WantedMenu`-sized, which is why that one stays inline in
// `AgentsPage.tsx` and this one doesn't).
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 { Panel } from "../ui/panel/Panel.js";
import { TextField } from "../ui/text-field/TextField.js";
import { SelectField } from "../ui/select-field/SelectField.js";
import { Button } from "../ui/button/Button.js";
import { Hint } from "../ui/hint/Hint.js";
import "./LinkMatrixAccountForm.css";
import { FormStack } from "../ui/form-stack/FormStack.js";
import { FormResult } from "../ui/submit/FormResult.js";
import { useSubmit } from "../ui/submit/useSubmit.js";
type Mode = "token" | "password";
@ -45,15 +44,13 @@ type Mode = "token" | "password";
// gate, this just avoids a round-trip for an obviously-bad name.
const ACCOUNT_PATTERN = "[A-Za-z0-9_\\-]{1,63}";
type SubmitState =
| { status: "idle" }
| { status: "submitting" }
// `userId` is set in password mode (the server resolves it from the
// login response) and `undefined` in token mode — see
// `PutMatrixAccountResponse`'s own doc for why token mode doesn't
// spend a round trip resolving one.
| { status: "done"; account: string; userId?: string }
| { status: "error"; problem: ProblemDetails };
// `userId` is set in password mode (the server resolves it from the login
// response) and `undefined` in token mode — see `PutMatrixAccountResponse`'s
// own doc for why token mode doesn't spend a round trip resolving one.
interface Linked {
account: string;
userId?: string;
}
const MODE_OPTIONS = [
{ value: "token", label: "paste a token" },
@ -78,7 +75,7 @@ export function LinkMatrixAccountForm({
const [userId, setUserId] = useState("");
const [password, setPassword] = useState("");
const [homeserver, setHomeserver] = useState("");
const [result, setResult] = useState<SubmitState>({ status: "idle" });
const { state, submitting, run } = useSubmit<Linked>();
// A secret lives in state only while it is being typed and sent: it is
// dropped when the request ends, whether it worked or not, and when the
@ -88,51 +85,53 @@ export function LinkMatrixAccountForm({
setPassword("");
}
async function submit(e: Event) {
function submit(e: Event) {
e.preventDefault();
setResult({ status: "submitting" });
try {
const r = await fetch(
`/api/hives/${encodeURIComponent(hive)}/agents/${encodeURIComponent(agent)}/matrix-accounts/${encodeURIComponent(account)}`,
{
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify(
mode === "token"
? {
mode,
token,
// Omitted rather than sent empty — the contract marks
// it optional in token mode, and an empty string
// isn't "no homeserver", it's a homeserver named "".
homeserver: homeserver.trim() || undefined,
}
: {
mode,
user_id: userId,
password,
// Required in password mode — the field below enforces
// that before this branch is ever reachable.
homeserver: homeserver.trim(),
},
),
const sent = account;
void run(
async () => {
try {
return await fetch(
`/api/hives/${encodeURIComponent(hive)}/agents/${encodeURIComponent(agent)}/matrix-accounts/${encodeURIComponent(sent)}`,
{
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify(
mode === "token"
? {
mode,
token,
// Omitted rather than sent empty — the contract marks
// it optional in token mode, and an empty string
// isn't "no homeserver", it's a homeserver named "".
homeserver: homeserver.trim() || undefined,
}
: {
mode,
user_id: userId,
password,
// Required in password mode — the field below enforces
// that before this branch is ever reachable.
homeserver: homeserver.trim(),
},
),
},
);
} finally {
clearSecrets();
}
},
{
// The account name is kept with the result, so the success message
// reads against what was sent even if the field is edited afterwards.
parse: async (r) => {
const body = (await r.json().catch(() => ({}))) as {
user_id?: string;
};
return { account: sent, userId: body.user_id };
},
);
clearSecrets();
if (!r.ok) {
setResult({ status: "error", problem: await readApiError(r) });
return;
}
const body = (await r.json().catch(() => ({}))) as {
user_id?: string;
};
// The account name is stored with the result, so the success message
// reads against what was sent even if the field is edited afterwards.
setResult({ status: "done", account, userId: body.user_id });
} catch (err) {
clearSecrets();
setResult({ status: "error", problem: { detail: String(err) } });
}
},
);
}
return (
@ -146,7 +145,7 @@ export function LinkMatrixAccountForm({
daemon reads it from there within two minutes — nothing here restarts
anything directly.
</p>
<form class="link-matrix-account-form" onSubmit={submit}>
<FormStack onSubmit={submit}>
<TextField
id="matrix-account-name"
label="account"
@ -216,31 +215,25 @@ export function LinkMatrixAccountForm({
required={mode === "password"}
onInput={setHomeserver}
/>
<Button
variant="primary"
type="submit"
disabled={result.status === "submitting"}
>
{result.status === "submitting" ? "linking…" : "link account"}
<Button variant="primary" type="submit" disabled={submitting}>
{submitting ? "linking…" : "link account"}
</Button>
</form>
{result.status === "done" && (
<Hint tone="ok" class="link-matrix-account-result-ok">
linked <strong>{result.account}</strong> to <strong>{agent}</strong>
{result.userId ? (
<>
{" "}
— logged in as <strong>{result.userId}</strong>
</>
) : null}
</Hint>
)}
{result.status === "error" && (
<ApiErrorPanel
context="failed to link the account"
problem={result.problem}
/>
)}
</FormStack>
<FormResult
state={state}
done={(linked) => (
<>
linked <strong>{linked.account}</strong> to <strong>{agent}</strong>
{linked.userId ? (
<>
{" "}
— logged in as <strong>{linked.userId}</strong>
</>
) : null}
</>
)}
failed="failed to link the account"
/>
</Panel>
);
}