"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Loader2, Copy, KeyRound, Trash2 } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Card, CardContent } from "@/components/ui/card"; import { ConfirmDialog } from "@/components/admin/ui/confirm-dialog"; import { EmptyState } from "@/components/ui/empty-state"; import { createApiKeyAction, revokeApiKeyAction } from "@/app/(app)/api-keys/actions"; interface KeyRow { id: string; name: string; prefix: string; lastUsedAt: string | null; createdAt: string; } export function ApiKeysClient({ keys }: { keys: KeyRow[] }) { const router = useRouter(); const [name, setName] = useState(""); const [creating, setCreating] = useState(false); const [newKey, setNewKey] = useState(null); async function create(e: React.FormEvent) { e.preventDefault(); setCreating(true); const res = await createApiKeyAction(name); setCreating(false); if (!res.ok || !res.key) { toast.error(res.error ?? "Could not create"); return; } setNewKey(res.key); setName(""); router.refresh(); } return (
{newKey && (

Your new API key — copy it now, it won't be shown again.

{newKey}
)}
setName(e.target.value)} />
{keys.length === 0 ? ( ) : (
{keys.map((k) => (

{k.name}

{k.prefix} · created {new Date(k.createdAt).toLocaleDateString()} {k.lastUsedAt ? ` · last used ${new Date(k.lastUsedAt).toLocaleDateString()}` : " · never used"}

Revoke } title="Revoke this key?" description={`Apps using "${k.name}" will immediately stop working. This cannot be undone.`} confirmLabel="Revoke key" successMessage="Key revoked" onConfirm={async () => { const res = await revokeApiKeyAction(k.id); return { ok: res.ok, error: res.error }; }} />
))}
)}
); }