Twenty save, link, create, send, run, load and log-in buttons changed their label to "saving…" or similar while a request ran, so the button changed width under the cursor. They now use Button's busy prop like the message box and the broadcast dialog do: a spinner covers the label, which keeps its space. No behaviour change; the buttons are disabled while busy as before.
99 lines
2.8 KiB
TypeScript
99 lines
2.8 KiB
TypeScript
// <LinkApiKeyForm> — sets an agent's API key. The key goes to the agent's
|
|
// backend secrets in the swarm secret store as `ANTHROPIC_API_KEY=<key>`
|
|
// (`PUT …/backend-secrets`); no route hands it back, so the box starts empty
|
|
// and a save replaces whatever is stored. The advanced form
|
|
// (`LinkBackendEnvForm`) is where the rest of the backend environment lives.
|
|
import { useState } from "preact/hooks";
|
|
import { Panel } from "../ui/panel/Panel.js";
|
|
import { TextField } from "../ui/text-field/TextField.js";
|
|
import { Button } from "../ui/button/Button.js";
|
|
import { FormStack } from "../ui/form-stack/FormStack.js";
|
|
import { FormResult } from "../ui/submit/FormResult.js";
|
|
import { Hint } from "../ui/hint/Hint.js";
|
|
import { useSubmit } from "../ui/submit/useSubmit.js";
|
|
import {
|
|
apiKeySecrets,
|
|
backendSecretsUrl,
|
|
checkApiKey,
|
|
} from "../agent/detail/account-api.js";
|
|
|
|
export function LinkApiKeyForm({
|
|
hive,
|
|
agent,
|
|
onSaved,
|
|
onClose,
|
|
}: {
|
|
hive: string;
|
|
agent: string;
|
|
/** Called after a save, so the row can show the key as stored. */
|
|
onSaved?: () => void;
|
|
onClose?: () => void;
|
|
}) {
|
|
const [key, setKey] = useState("");
|
|
const { state, submitting, run, fail } = useSubmit();
|
|
|
|
function save(e: Event) {
|
|
e.preventDefault();
|
|
const problem = checkApiKey(key);
|
|
if (problem !== null) {
|
|
fail({ detail: problem });
|
|
return;
|
|
}
|
|
void run(
|
|
() =>
|
|
fetch(backendSecretsUrl(hive, agent), {
|
|
method: "PUT",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ env: apiKeySecrets(key) }),
|
|
}),
|
|
{
|
|
// The store holds the key; nothing here needs it.
|
|
onDone: () => {
|
|
setKey("");
|
|
onSaved?.();
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Panel title={`set the API key — ${agent}`} icon="🔑" onClose={onClose}>
|
|
<p>
|
|
Bills <strong>{agent}</strong> through the API instead of a Claude
|
|
login.
|
|
</p>
|
|
<FormStack onSubmit={save}>
|
|
<TextField
|
|
id="api-key"
|
|
label="API key"
|
|
type="password"
|
|
value={key}
|
|
required
|
|
placeholder="sk-ant-…"
|
|
onInput={setKey}
|
|
/>
|
|
<Hint>
|
|
Stored in the swarm secret store and never shown again; a save
|
|
replaces the stored key. The agent loads it at its next restart.
|
|
</Hint>
|
|
<Button
|
|
busy={submitting}
|
|
variant="primary"
|
|
type="submit"
|
|
disabled={submitting}
|
|
>
|
|
save
|
|
</Button>
|
|
</FormStack>
|
|
<FormResult
|
|
state={state}
|
|
done={() => (
|
|
<>
|
|
stored the API key for <strong>{agent}</strong>
|
|
</>
|
|
)}
|
|
failed="failed to store the API key"
|
|
/>
|
|
</Panel>
|
|
);
|
|
}
|