import { Button } from '@/components/ui/button'; import { Spinner } from '@/components/ui/spinner'; import { RefreshCw, AlertCircle } from 'lucide-react'; import { OpenAIIcon } from '@/components/ui/provider-icon'; import { cn } from '@/lib/utils'; import { formatCodexPlanType, formatCodexResetTime, getCodexWindowLabel, } from '@/lib/codex-usage-format'; import { useSetupStore } from '@/store/setup-store'; import { useCodexUsage } from '@/hooks/queries'; import type { CodexRateLimitWindow } from '@/store/app-store'; const CODEX_USAGE_TITLE = 'Codex Usage'; const CODEX_USAGE_SUBTITLE = 'Shows usage limits reported by the Codex CLI.'; const CODEX_AUTH_WARNING = 'Authenticate Codex CLI to view usage limits.'; const CODEX_LOGIN_COMMAND = 'codex login'; const CODEX_NO_USAGE_MESSAGE = 'Usage limits are not available yet. Try refreshing if this persists.'; const UPDATED_LABEL = 'Updated'; const CODEX_REFRESH_LABEL = 'Refresh Codex usage'; const PLAN_LABEL = 'Plan'; const WARNING_THRESHOLD = 75; const CAUTION_THRESHOLD = 50; const MAX_PERCENTAGE = 100; const USAGE_COLOR_CRITICAL = 'bg-red-500'; const USAGE_COLOR_WARNING = 'bg-amber-500'; const USAGE_COLOR_OK = 'bg-emerald-500'; const isRateLimitWindow = ( limitWindow: CodexRateLimitWindow | null ): limitWindow is CodexRateLimitWindow => Boolean(limitWindow); export function CodexUsageSection() { const codexAuthStatus = useSetupStore((state) => state.codexAuthStatus); const canFetchUsage = !!codexAuthStatus?.authenticated; // Use React Query for data fetching with automatic polling const { data: codexUsage, isLoading, isFetching, error, refetch } = useCodexUsage(canFetchUsage); const rateLimits = codexUsage?.rateLimits ?? null; const primary = rateLimits?.primary ?? null; const secondary = rateLimits?.secondary ?? null; const planType = rateLimits?.planType ?? null; const rateLimitWindows = [primary, secondary].filter(isRateLimitWindow); const hasMetrics = rateLimitWindows.length > 0; const lastUpdatedLabel = codexUsage?.lastUpdated ? new Date(codexUsage.lastUpdated).toLocaleString() : null; const showAuthWarning = !canFetchUsage && !codexUsage && !isLoading; const errorMessage = error instanceof Error ? error.message : error ? String(error) : null; const getUsageColor = (percentage: number) => { if (percentage >= WARNING_THRESHOLD) { return USAGE_COLOR_CRITICAL; } if (percentage >= CAUTION_THRESHOLD) { return USAGE_COLOR_WARNING; } return USAGE_COLOR_OK; }; const RateLimitCard = ({ title, subtitle, window: limitWindow, }: { title: string; subtitle: string; window: CodexRateLimitWindow; }) => { const safePercentage = Math.min(Math.max(limitWindow.usedPercent, 0), MAX_PERCENTAGE); const resetLabel = formatCodexResetTime(limitWindow.resetsAt); return (

{title}

{subtitle}

{Math.round(safePercentage)}%
{resetLabel &&

{resetLabel}

}
); }; return (

{CODEX_USAGE_TITLE}

{CODEX_USAGE_SUBTITLE}

{showAuthWarning && (
{CODEX_AUTH_WARNING} Run {CODEX_LOGIN_COMMAND}.
)} {errorMessage && (
{errorMessage}
)} {hasMetrics && (
{rateLimitWindows.map((limitWindow, index) => { const { title, subtitle } = getCodexWindowLabel(limitWindow.windowDurationMins); return ( ); })}
)} {planType && (
{PLAN_LABEL}: {formatCodexPlanType(planType)}
)} {!hasMetrics && !errorMessage && canFetchUsage && !isLoading && (
{CODEX_NO_USAGE_MESSAGE}
)} {lastUpdatedLabel && (
{UPDATED_LABEL} {lastUpdatedLabel}
)}
); }