// @ts-nocheck import { useCallback, useEffect, useState } from 'react'; import { Button } from '@/components/ui/button'; import { RefreshCw, AlertCircle } from 'lucide-react'; import { OpenAIIcon } from '@/components/ui/provider-icon'; import { cn } from '@/lib/utils'; import { getElectronAPI } from '@/lib/electron'; import { formatCodexPlanType, formatCodexResetTime, getCodexWindowLabel, } from '@/lib/codex-usage-format'; import { useSetupStore } from '@/store/setup-store'; import { useAppStore, type CodexRateLimitWindow } from '@/store/app-store'; const ERROR_NO_API = 'Codex usage API not available'; 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_FETCH_ERROR = 'Failed to fetch usage'; const CODEX_REFRESH_LABEL = 'Refresh Codex usage'; const PLAN_LABEL = 'Plan'; const WARNING_THRESHOLD = 75; const CAUTION_THRESHOLD = 50; const MAX_PERCENTAGE = 100; const REFRESH_INTERVAL_MS = 60_000; const STALE_THRESHOLD_MS = 2 * 60_000; 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 { codexUsage, codexUsageLastUpdated, setCodexUsage } = useAppStore(); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const canFetchUsage = !!codexAuthStatus?.authenticated; 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 isStale = !codexUsageLastUpdated || Date.now() - codexUsageLastUpdated > STALE_THRESHOLD_MS; const fetchUsage = useCallback(async () => { setIsLoading(true); setError(null); try { const api = getElectronAPI(); if (!api.codex) { setError(ERROR_NO_API); return; } const result = await api.codex.getUsage(); if ('error' in result) { setError(result.message || result.error); return; } setCodexUsage(result); } catch (fetchError) { const message = fetchError instanceof Error ? fetchError.message : CODEX_FETCH_ERROR; setError(message); } finally { setIsLoading(false); } }, [setCodexUsage]); useEffect(() => { if (canFetchUsage && isStale) { void fetchUsage(); } }, [fetchUsage, canFetchUsage, isStale]); useEffect(() => { if (!canFetchUsage) return undefined; const intervalId = setInterval(() => { void fetchUsage(); }, REFRESH_INTERVAL_MS); return () => clearInterval(intervalId); }, [fetchUsage, canFetchUsage]); 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}.
)} {error && (
{error}
)} {hasMetrics && (
{rateLimitWindows.map((limitWindow, index) => { const { title, subtitle } = getCodexWindowLabel(limitWindow.windowDurationMins); return ( ); })}
)} {planType && (
{PLAN_LABEL}: {formatCodexPlanType(planType)}
)} {!hasMetrics && !error && canFetchUsage && !isLoading && (
{CODEX_NO_USAGE_MESSAGE}
)} {lastUpdatedLabel && (
{UPDATED_LABEL} {lastUpdatedLabel}
)}
); }