import { useState } from "react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Checkbox } from "@/components/ui/checkbox"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { AlertCircle, Copy, CheckCircle2 } from "lucide-react"; import { cn } from "@/lib/utils"; interface CreateApiKeyDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onCreateKey: (data: { name: string; scopes: string[]; expiresInDays?: number; }) => Promise<{ full_key?: string; error?: string }>; } export function CreateApiKeyDialog({ open, onOpenChange, onCreateKey, }: CreateApiKeyDialogProps) { const [name, setName] = useState(""); const [scopes, setScopes] = useState(["read"]); const [expiresInDays, setExpiresInDays] = useState(undefined); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(""); const [createdKey, setCreatedKey] = useState(null); const [copied, setCopied] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); if (!name.trim()) { setError("Please enter a name for your API key"); return; } if (scopes.length === 0) { setError("Please select at least one scope"); return; } setIsSubmitting(true); try { const result = await onCreateKey({ name: name.trim(), scopes, expiresInDays, }); if (result.error) { setError(result.error); } else if (result.full_key) { setCreatedKey(result.full_key); } } catch (err) { setError("Failed to create API key. Please try again."); } finally { setIsSubmitting(false); } }; const handleClose = () => { setName(""); setScopes(["read"]); setExpiresInDays(undefined); setError(""); setCreatedKey(null); setCopied(false); onOpenChange(false); }; const copyToClipboard = () => { if (createdKey) { navigator.clipboard.writeText(createdKey); setCopied(true); setTimeout(() => setCopied(false), 2000); } }; const toggleScope = (scope: string) => { setScopes((prev) => prev.includes(scope) ? prev.filter((s) => s !== scope) : [...prev, scope] ); }; return ( {!createdKey ? ( <> Create API Key Generate a new API key to access the AeThex platform programmatically.
{/* Name Input */}
setName(e.target.value)} maxLength={50} disabled={isSubmitting} />

A friendly name to help you identify this key

{/* Scopes */}
toggleScope("read")} disabled={isSubmitting} />
toggleScope("write")} disabled={isSubmitting} />
toggleScope("admin")} disabled={isSubmitting} />
{/* Expiration */}
{/* Error Message */} {error && (
{error}
)}
) : ( <> API Key Created Successfully Make sure to copy your API key now. You won't be able to see it again!
{/* Success Message */}

Your API key has been created

Copy it now and store it securely. For security reasons, we can't show it again.

{/* Key Display */}
{createdKey}
{/* Warning */}

Important Security Notice

  • Never commit this key to version control
  • Store it securely (e.g., environment variables)
  • Regenerate the key if you suspect it's compromised
)}
); }