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

@ -9,12 +9,12 @@
// set: no route hands a token back. Linked labels and URLs show on
// the agent panel (`LinkedAccounts`).
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 { Button } from "../ui/button/Button.js";
import { Hint } from "../ui/hint/Hint.js";
import { FormStack } from "../ui/form-stack/FormStack.js";
import { FormResult } from "../ui/submit/FormResult.js";
import { useSubmit } from "../ui/submit/useSubmit.js";
import {
checkLink,
LABEL_PATTERN,
@ -22,17 +22,10 @@ import {
linkUrl,
type ForgeLinkKind,
} from "../agent/detail/account-api.js";
import "./LinkMatrixAccountForm.css";
// Prefilled for gitlab; the field stays editable for a self-hosted instance.
const GITLAB_DEFAULT_URL = "https://gitlab.com";
type SubmitState =
| { status: "idle" }
| { status: "submitting" }
| { status: "done"; label: string; url: string }
| { status: "error"; problem: ProblemDetails };
export function LinkForgeAccountForm({
kind,
hive,
@ -47,35 +40,37 @@ export function LinkForgeAccountForm({
const [label, setLabel] = useState("");
const [url, setUrl] = useState(kind === "gitlab" ? GITLAB_DEFAULT_URL : "");
const [token, setToken] = useState("");
const [result, setResult] = useState<SubmitState>({ status: "idle" });
// The label is kept with the result, so the success message reads against
// what was sent even if the field is edited afterwards.
const { state, submitting, run, fail } = useSubmit<{
label: string;
url: string;
}>();
async function submit(e: Event) {
function submit(e: Event) {
e.preventDefault();
const invalid = checkLink(label, url, token);
if (invalid) {
setResult({ status: "error", problem: { detail: invalid } });
fail({ detail: invalid });
return;
}
setResult({ status: "submitting" });
try {
const r = await fetch(linkUrl(hive, agent, kind, label), {
method: "PUT",
headers: { "content-type": "application/json" },
body: linkBody(url, token),
});
if (!r.ok) {
setResult({ status: "error", problem: await readApiError(r) });
return;
}
const body = (await r.json().catch(() => ({}))) as { url?: string };
// The label is stored with the result, so the success message reads
// against what was sent even if the field is edited afterwards.
setResult({ status: "done", label, url: body.url ?? url.trim() });
// The store holds the token now; nothing here needs it.
setToken("");
} catch (err) {
setResult({ status: "error", problem: { detail: String(err) } });
}
const sent = label;
void run(
() =>
fetch(linkUrl(hive, agent, kind, sent), {
method: "PUT",
headers: { "content-type": "application/json" },
body: linkBody(url, token),
}),
{
parse: async (r) => {
const body = (await r.json().catch(() => ({}))) as { url?: string };
return { label: sent, url: body.url ?? url.trim() };
},
// The store holds the token now; nothing here needs it.
onDone: () => setToken(""),
},
);
}
return (
@ -107,7 +102,7 @@ export function LinkForgeAccountForm({
the external forge first; nothing is created there.
</p>
)}
<form class="link-matrix-account-form" onSubmit={submit}>
<FormStack onSubmit={submit}>
<TextField
id={`${kind}-account-label`}
label="label"
@ -139,26 +134,20 @@ export function LinkForgeAccountForm({
required
onInput={setToken}
/>
<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.label}</strong> ({result.url}) to{" "}
<strong>{agent}</strong>
</Hint>
)}
{result.status === "error" && (
<ApiErrorPanel
context="failed to link the account"
problem={result.problem}
/>
)}
</FormStack>
<FormResult
state={state}
done={(linked) => (
<>
linked <strong>{linked.label}</strong> ({linked.url}) to{" "}
<strong>{agent}</strong>
</>
)}
failed="failed to link the account"
/>
</Panel>
);
}