videogen/apps/web/components/apikeys/CreateApiKeyForm.tsx
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

108 lines
3.4 KiB
TypeScript

"use client";
import * as React from "react";
import { AlertCircle, KeyRound, Loader2 } from "lucide-react";
import type { CreatedApiKey } from "@/lib/types";
import { ApiError, createApiKey } from "@/lib/api";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useApiToken } from "@/lib/use-api-token";
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
interface CreateApiKeyFormProps {
/** Called with the newly minted key (including its one-time raw secret). */
onCreated: (key: CreatedApiKey) => void;
}
const NAME_MAX = 200;
type Phase =
| { status: "idle" }
| { status: "creating" }
| { status: "error"; message: string };
/**
* Form to mint a personal API key: the caller names it, submits, and the parent
* receives the created key (with its one-time raw secret) to reveal. The name
* input is a controlled, immutable value and is cleared after a successful mint.
*/
export function CreateApiKeyForm({ onCreated }: CreateApiKeyFormProps) {
const getApiToken = useApiToken();
const [name, setName] = React.useState("");
const [phase, setPhase] = React.useState<Phase>({ status: "idle" });
const busy = phase.status === "creating";
const canSubmit = !busy && name.trim().length > 0;
const handleSubmit = React.useCallback(
async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
if (name.trim().length === 0) return;
setPhase({ status: "creating" });
try {
const token = await getApiToken();
const created = await createApiKey(name, { token });
onCreated(created);
setName("");
setPhase({ status: "idle" });
} catch (err) {
const message =
friendlyErrorMessage(err, "Could not create your API key. Please try again.");
setPhase({ status: "error", message });
}
},
[getApiToken, name, onCreated],
);
return (
<Card>
<CardContent className="p-6">
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="api-key-name">Key name</Label>
<Input
id="api-key-name"
value={name}
maxLength={NAME_MAX}
placeholder="e.g. CI, n8n, my laptop"
onChange={(e) => setName(e.target.value)}
disabled={busy}
required
autoComplete="off"
/>
<p className="text-xs text-muted-foreground">
A label to help you recognise this key later. You can create
several and revoke any of them independently.
</p>
</div>
{phase.status === "error" && (
<p
role="alert"
className="flex items-center gap-2 text-sm text-destructive"
>
<AlertCircle className="size-4 shrink-0" aria-hidden />
{phase.message}
</p>
)}
<Button type="submit" disabled={!canSubmit} className="w-full">
{busy ? (
<>
<Loader2 className="animate-spin" /> Creating key…
</>
) : (
<>
<KeyRound /> Create API key
</>
)}
</Button>
</form>
</CardContent>
</Card>
);
}