import { Progress } from "@/components/ui/progress"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; import { resetDurationLabels } from "@/lib/constants/governance"; import { cn } from "@/lib/utils"; import { formatCompactNumber } from "@/lib/utils/governance"; interface RateLimitShape { token_max_limit?: number | null; token_reset_duration?: string | null; token_current_usage?: number | null; request_max_limit?: number | null; request_reset_duration?: string | null; request_current_usage?: number | null; } interface RateLimitDisplayProps { rateLimits: RateLimitShape | null | undefined; /** Compact mode for narrow cells — still renders bars, just tighter */ compact?: boolean; /** Render limit + reset period only (no usage bar). Use for template entities like access profiles. */ limitOnly?: boolean; } const formatResetDuration = (duration?: string | null) => { if (!duration) return ""; return resetDurationLabels[duration] || duration; }; function LimitText({ label, max, resetDuration }: { label: string; max: number; resetDuration?: string | null }) { return (
{formatCompactNumber(max)} {label} {formatResetDuration(resetDuration)}
); } function Bar({ label, current, max, resetDuration, compact }: { label: string; current: number; max: number; resetDuration?: string | null; compact?: boolean; }) { const pct = max > 0 ? Math.min((current / max) * 100, 100) : 0; const isExhausted = max > 0 && current >= max; const barClass = isExhausted ? "[&>div]:bg-red-500/70" : pct > 80 ? "[&>div]:bg-amber-500/70" : "[&>div]:bg-emerald-500/70"; return (
{formatCompactNumber(max)} {label} {formatResetDuration(resetDuration)}

{current.toLocaleString()} / {max.toLocaleString()} {label}

{resetDuration ? (

Resets {formatResetDuration(resetDuration)}

) : null}
); } export function RateLimitDisplay({ rateLimits, compact, limitOnly }: RateLimitDisplayProps) { if (!rateLimits) { return -; } const hasTokens = rateLimits.token_max_limit != null && rateLimits.token_max_limit > 0; const hasRequests = rateLimits.request_max_limit != null && rateLimits.request_max_limit > 0; if (!hasTokens && !hasRequests) { return -; } return (
{hasTokens ? ( limitOnly ? ( ) : ( ) ) : null} {hasRequests ? ( limitOnly ? ( ) : ( ) ) : null}
); }