import { useState } from "react"; import { Card } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Key, MoreVertical, Copy, Eye, EyeOff, Trash2, Calendar, Activity, CheckCircle2, XCircle, } from "lucide-react"; import { cn } from "@/lib/utils"; interface ApiKeyCardProps { apiKey: { id: string; name: string; key_prefix: string; scopes: string[]; last_used_at?: string | null; usage_count: number; is_active: boolean; created_at: string; expires_at?: string | null; }; onDelete: (id: string) => void; onToggleActive: (id: string, isActive: boolean) => void; onViewStats: (id: string) => void; } export function ApiKeyCard({ apiKey, onDelete, onToggleActive, onViewStats }: ApiKeyCardProps) { const [showKey, setShowKey] = useState(false); const [copied, setCopied] = useState(false); const copyToClipboard = () => { navigator.clipboard.writeText(apiKey.key_prefix + "***"); setCopied(true); setTimeout(() => setCopied(false), 2000); }; const isExpired = apiKey.expires_at && new Date(apiKey.expires_at) < new Date(); const daysUntilExpiry = apiKey.expires_at ? Math.ceil((new Date(apiKey.expires_at).getTime() - Date.now()) / (1000 * 60 * 60 * 24)) : null; return (
{/* Icon and Name */}

{apiKey.name}

{!apiKey.is_active && ( Inactive )} {isExpired && ( Expired )}
{/* API Key Display */}
{showKey ? apiKey.key_prefix : apiKey.key_prefix.substring(0, 12)} {"*".repeat(showKey ? 0 : 40)}
{/* Scopes */}
{apiKey.scopes.map((scope) => ( {scope} ))}
{/* Stats */}
{apiKey.usage_count.toLocaleString()} requests
Last used:{" "} {apiKey.last_used_at ? new Date(apiKey.last_used_at).toLocaleDateString() : "Never"}
{/* Expiration Warning */} {daysUntilExpiry !== null && daysUntilExpiry < 30 && daysUntilExpiry > 0 && (
Expires in {daysUntilExpiry} day{daysUntilExpiry !== 1 ? "s" : ""}
)}
{/* Actions Menu */} onViewStats(apiKey.id)}> View Statistics onToggleActive(apiKey.id, !apiKey.is_active)} > {apiKey.is_active ? ( <> Deactivate Key ) : ( <> Activate Key )} onDelete(apiKey.id)} className="text-destructive focus:text-destructive" > Delete Key
); }