"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Loader2, Copy, KeyRound, Trash2, Check } 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 { 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(); } async function revoke(id: string) { if (!confirm("Revoke this key? Apps using it will stop working.")) return; const res = await revokeApiKeyAction(id); if (res.ok) { toast.success("Key revoked"); router.refresh(); } else { toast.error(res.error ?? "Could not revoke"); } } return (
{newKey && (

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

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

No API keys yet.

) : (
{keys.map((k) => (

{k.name}

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

))}
)}
); }