"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; interface CreatePlatformKeyProps { /** Server action: returns the one-time plaintext or an error. */ action: (label: string, scopes: string[]) => Promise<{ plaintext?: string; error?: string }>; scopes: readonly string[]; } /** Create a restricted API key and show its secret exactly once. */ export function CreatePlatformKey({ action, scopes }: CreatePlatformKeyProps) { const router = useRouter(); const [label, setLabel] = useState(""); const [picked, setPicked] = useState>(new Set(["payments:read"])); const [full, setFull] = useState(false); const [pending, setPending] = useState(false); const [secret, setSecret] = useState(null); const [error, setError] = useState(null); async function submit(event: React.FormEvent): Promise { event.preventDefault(); setPending(true); setError(null); const result = await action(label.trim() || "API key", full ? ["platform:admin"] : [...picked]); setPending(false); if (result.error || !result.plaintext) { setError(result.error ?? "Could not create the key."); return; } setSecret(result.plaintext); router.refresh(); } if (secret) { return (

Copy this key now. It is shown only once.

{secret}

); } const toggle = (scope: string) => setPicked((prev) => { const next = new Set(prev); if (next.has(scope)) next.delete(scope); else next.add(scope); return next; }); return (
setLabel(e.target.value)} placeholder="Reporting job" />
{!full ? (
{scopes.map((scope) => ( ))}
) : null} {error ? (

{error}

) : null}
); }