import { useQuery } from '@tanstack/react-query'; import { getGatewayKeyUsage } from '../../api'; interface Props { keyId: string; onClose: () => void; } function fmtTokens(n: number): string { if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(2)}M`; if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`; return n.toLocaleString(); } /** * Per-key usage detail. Shows current-month stats (with a progress bar * vs budget) and a simple bar chart of the last 6-12 months of token * usage. No external chart library — pure CSS bars keep the UI bundle * lean. */ export function GatewayKeyUsagePanel({ keyId, onClose }: Props) { const { data, isLoading, error } = useQuery({ queryKey: ['gateway-key-usage', keyId], queryFn: () => getGatewayKeyUsage(keyId), staleTime: 5_000, }); const maxHistTokens = data ? Math.max(1, ...data.history.map(h => h.tokensIn + h.tokensOut)) : 1; const pctUsed = data && data.tokensBudget !== null && data.tokensBudget > 0 ? Math.min(100, (data.tokensTotal / data.tokensBudget) * 100) : null; return (
{keyId}