import { Gauge } from 'lucide-react'; import { Badge } from '@/components/ui/Badge'; import { Card, CardHeader, CardTitle } from '@/components/ui/Card'; import { ProgressBar } from '@/components/ui/ProgressBar'; import { isUnlimitedQuota } from '@/lib/billing-format'; import { formatDate, useTimeZone } from '@/lib/date'; import { MODULE_COLOR } from '@/lib/module-colors'; import type { SubscriptionPublic } from '@/types'; const mc = MODULE_COLOR.billing; // Above these fractions of the allowance the bar changes hue. const WARN_AT = 0.75; const CRITICAL_AT = 0.95; const STATUS_LABEL: Record = { active: 'Active', past_due: 'Past due', canceled: 'Canceled', inactive: 'Inactive', }; function barColor(fraction: number): 'lime' | 'cyan' | 'danger' { if (fraction >= CRITICAL_AT) return 'danger'; if (fraction >= WARN_AT) return 'cyan'; return 'lime'; } function formatTokens(tokens: number): string { return tokens.toLocaleString('en-US'); } interface QuotaMeterProps { subscription: SubscriptionPublic; } export function QuotaMeter({ subscription }: QuotaMeterProps) { const tz = useTimeZone(); const { used_tokens, quota_tokens, status, current_period_end } = subscription; const unlimited = isUnlimitedQuota(quota_tokens); const fraction = quota_tokens > 0 ? used_tokens / quota_tokens : 0; // Never "exhausted" without a ceiling -- the sentinel is negative, so the // raw comparison would be true for every free account. const exhausted = !unlimited && used_tokens >= quota_tokens; return ( } module="billing"> Monthly token quota

{formatTokens(used_tokens)} / {unlimited ? '∞' : formatTokens(quota_tokens)}

{STATUS_LABEL[status]}
{unlimited ? (

> Unlimited tokens on the free plan.

) : ( )}

Renews {formatDate(current_period_end, tz)} {subscription.cancel_at_period_end && ' — will not renew'}

{exhausted && (

> Quota exhausted. It resets when the period renews.

)}
); }