Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
"use client";
import { useMemo, useState } from "react";
import { useLocale, useTranslations } from "next-intl";
import { buildGrokBillingCardRows } from "@/shared/utils/grokBilling";
import { buildKimiBillingCardRows } from "@/shared/utils/kimiBilling";
import {
isKimiBillingStatus,
isProviderBillingProvider,
type ProviderBillingStatus,
} from "@/shared/utils/providerBilling";
import {
formatCountdown,
formatQuotaLabel,
getBarColor,
getQuotaRemainingPercentage,
getQuotaVisibilityKey,
shouldShowQuotaUsageCount,
} from "../utils";
import QuotaMiniBar from "../QuotaMiniBar";
import { translateUsageOrFallback, type UsageTranslationValues } from "../i18nFallback";
import {
findKiloPassQuotaRow,
isKiloPassDisplayRow,
hasFixedQuotaOrder,
hasCanonicalWindowOrder,
sortQuotasByWindow,
} from "../quotaParsing";
import KiloPassMeter from "./KiloPassMeter";
import QuotaCardSpeed from "./QuotaCardSpeed";
import { hasCodexPaidCredits, type CodexPaidCredits } from "@/lib/providers/codexPaidCredits";
import AntigravityQuotaGroups, { resolveAntigravityQuotaGroups } from "../AntigravityQuotaGroups";
const CURRENCY_SYMBOLS: Record<string, string> = {
USD: "$",
CNY: "¥",
EUR: "€",
GBP: "£",
JPY: "¥",
KRW: "₩",
INR: "₹",
};
const DEFAULT_VISIBLE_ROWS = 3;
function ProviderBillingDetails({ billing }: { billing: ProviderBillingStatus }) {
const t = useTranslations("usage");
const locale = useLocale();
const rows = isKimiBillingStatus(billing)
? buildKimiBillingCardRows(billing, locale, (key, fallback) =>
translateUsageOrFallback(t, key, fallback)
)
: buildGrokBillingCardRows(billing, locale, (key, fallback) =>
translateUsageOrFallback(t, key, fallback)
);
return (
<div className="flex flex-col gap-1.5 border-t border-border/40 pt-2 text-[11px] text-text-main">
{rows.map((row) =>
row.kind === "link" ? (
<a
key={`${row.kind}-${row.label}`}
href={row.href}
target={row.target}
rel={row.rel}
className="inline-flex w-fit items-center gap-1 font-medium text-primary hover:underline"
>
{row.label}
<span className="material-symbols-outlined text-[12px]">open_in_new</span>
</a>
) : (
<div
key={`${row.kind}-${row.label}`}
className={`flex justify-between gap-2 ${
row.kind === "status" ? "items-start" : "items-center"
}`}
>
<span>{row.label}</span>
<span
className={`font-semibold ${row.kind === "status" ? "text-right" : "tabular-nums"}`}
>
{row.value}
</span>
</div>
)
)}
</div>
);
}
/** Pure helper — sorts quotas by remaining percentage, highest first. */
export function sortQuotasByRemaining(quotas: any[]): any[] {
return [...quotas].sort(
(a, b) => getQuotaRemainingPercentage(b) - getQuotaRemainingPercentage(a)
);
}
/**
* Pure helper — resolves the display order for a provider's quotas.
* Providers with a deterministic fixed-window order (Codex, GLM family,
* Kimi Coding — see quotaParsing.ts) keep the order
* parseQuotaData() already established. Every other provider still gets the
* remaining-percentage sort. Fixes #6687 (bars re-sorted by % undid the fixed
* session/weekly order).
*
* #7764 residual: providers outside that whitelist which nonetheless report
* rolling time windows (claude, minimax, zai, command-code, ...) are ordered
* chronologically via `hasCanonicalWindowOrder`/`sortQuotasByWindow`, so the
* expanded card agrees with the collapsed card (`topQuotas`) and with sibling
* accounts of the same provider.
*/
export function resolveQuotaDisplayOrder(providerId: string | undefined, quotas: any[]): any[] {
if (hasFixedQuotaOrder(providerId)) return [...quotas];
if (hasCanonicalWindowOrder(quotas)) return sortQuotasByWindow(quotas);
return sortQuotasByRemaining(quotas);
}
/** Pure helper — slices the sorted quotas down to the visible window. */
export function getVisibleQuotas(sortedQuotas: any[], expanded: boolean): any[] {
return expanded ? sortedQuotas : sortedQuotas.slice(0, DEFAULT_VISIBLE_ROWS);
}
/**
* Pure helper — the loading placeholder replaces the entire quota section
* with a spinner. Swapping rows for a spinner mid-refresh collapses the card
* height, which rebalances the outer CSS multi-column layout (provider groups
* visibly jump between columns). Only the initial load (nothing to display
* yet) may show the placeholder; a refresh keeps the stale rows rendered
* while the refresh button icon spins, and the UI updates once new data
* arrives.
*/
export function shouldShowLoadingPlaceholder(
loading: boolean,
quotaCount: number,
message?: string | null
): boolean {
return loading && quotaCount === 0 && !message;
}
interface Props {
quotas: any[];
quotaGroups?: Array<Record<string, unknown>>;
providerId?: string;
loading: boolean;
error: string | null;
message?: string | null;
billing?: ProviderBillingStatus | null;
paidCredits?: CodexPaidCredits;
refreshedAt?: string;
hasStaleData: boolean;
onRefresh: () => void;
onOpenCutoff: () => void;
onOpenCost: () => void;
onOpenResetCredits?: () => void;
canEditCutoff: boolean;
hasCutoffOverrides: boolean;
canRedeemResetCredit?: boolean;
redeemingResetCredit?: boolean;
loadingResetCredits?: boolean;
/** Per-operator quota row visibility (upstream 9router#2371 port). */
hiddenQuotaRows?: any[];
onHideQuota?: (quota: any) => void;
onShowQuota?: (quota: any) => void;
/** Read-time decode-speed stats from the usage API (`speed` payload). */
speed?: unknown;
}
function QuotaDetailRow({
q,
kiloPassRow = null,
onHideQuota,
onOpenResetCredits,
loadingResetCredits = false,
}: {
q: any;
/** Collapsed Kilo Pass display row; present only on kilocode provider cards. */
kiloPassRow?: any | null;
onHideQuota?: (quota: any) => void;
onOpenResetCredits?: () => void;
loadingResetCredits?: boolean;
}) {
const t = useTranslations("usage");
const canHide = typeof onHideQuota === "function" && !q.isCredits && !q.isResetCredits;
if (isKiloPassDisplayRow(q)) {
return (
<KiloPassMeter
base={q.kiloPassBase ?? kiloPassRow?.kiloPassBase ?? 0}
bonus={q.kiloPassBonus ?? kiloPassRow?.kiloPassBonus ?? 0}
used={q.used}
total={q.total}
remaining={q.remaining}
nextBillingAt={q.resetAt ?? kiloPassRow?.resetAt ?? null}
balance={q.kiloPassBalance ?? null}
/>
);
}
if (q.isResetCredits) {
const count = Number(q.creditCount ?? q.remaining ?? 0);
const colors = getBarColor(q.remainingPercentage ?? 100);
return (
<div className="flex min-h-[34px] items-center justify-between gap-2 py-1">
<span className="flex min-w-0 flex-1 items-center gap-1.5 text-[12px] font-medium leading-none text-text-main">
<span className="inline-flex size-6 shrink-0 items-center justify-center">
<span
className="material-symbols-outlined text-[15px] leading-none"
style={{ color: colors.text }}
>
restart_alt
</span>
</span>
<span className="truncate leading-none">
{translateUsageOrFallback(t, "resetCreditsLabel", "Reset credits")}
</span>
</span>
<button
type="button"
disabled={!onOpenResetCredits || loadingResetCredits}
onClick={(event) => {
event.stopPropagation();
onOpenResetCredits?.();
}}
aria-label={translateUsageOrFallback(t, "viewResetCredits", "View reset credits")}
className="inline-flex h-6 shrink-0 items-center gap-1 rounded-md px-1.5 text-[12px] font-bold leading-none tabular-nums hover:bg-black/[0.05] disabled:cursor-default dark:hover:bg-white/[0.05]"
style={{ color: colors.text }}
>
{loadingResetCredits && (
<span className="material-symbols-outlined animate-spin text-[12px]">
progress_activity
</span>
)}
{count.toLocaleString(undefined, { maximumFractionDigits: 0 })}
<span className="material-symbols-outlined text-[13px]">chevron_right</span>
</button>
</div>
);
}
if (q.isCredits) {
const colors = getBarColor(q.remainingPercentage ?? 0);
const sym = CURRENCY_SYMBOLS[q.currency] ?? q.currency ?? "";
const amount = (q.creditCount ?? q.remaining ?? 0).toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
return (
<div className="flex min-h-[34px] items-center justify-between gap-2 py-1">
<span className="flex min-w-0 flex-1 items-center gap-1.5 text-[12px] font-medium leading-none text-text-main">
<span className="inline-flex size-6 shrink-0 items-center justify-center">
<span
className="material-symbols-outlined text-[15px] leading-none"
style={{ color: colors.text }}
>
paid
</span>
</span>
<span className="truncate leading-none">
{formatQuotaLabel(q.name) || t("creditsLabel")}
</span>
</span>
<span
className="inline-flex h-6 shrink-0 items-center text-[12px] font-bold leading-none tabular-nums"
style={{ color: colors.text }}
>
{sym}
{amount}
</span>
</div>
);
}
const pctRaw = getQuotaRemainingPercentage(q);
const pct = Math.round(pctRaw);
const colors = getBarColor(pct);
const cd = formatCountdown(q.resetAt);
const label = q.displayName || formatQuotaLabel(q.name);
const usedNum = Number(q.used || 0);
const totalNum = Number(q.total || 0);
const showUsage = shouldShowQuotaUsageCount(q);
return (
<div className="flex flex-col gap-1 py-1" title={q.modelKey || q.name}>
<div className="flex items-center justify-between gap-2">
<span className="text-[12px] font-medium text-text-main truncate">{label}</span>
<span
className="text-[12px] font-bold tabular-nums shrink-0"
style={{ color: colors.text }}
>
{q.unlimited ? "∞" : translateUsageOrFallback(t, "percentLeft", `${pct}% left`, { pct })}
</span>
{canHide && (
<button
type="button"
onClick={(e) => {
e.stopPropagation();
onHideQuota?.(q);
}}
className="inline-flex size-5 shrink-0 items-center justify-center rounded text-text-muted transition-colors hover:bg-black/5 hover:text-text-main dark:hover:bg-white/5"
title={translateUsageOrFallback(t, "hideQuotaRow", "Hide this quota row")}
aria-label={translateUsageOrFallback(t, "hideQuotaRow", "Hide this quota row")}
>
<span className="material-symbols-outlined text-[13px] leading-none">
visibility_off
</span>
</button>
)}
</div>
{!q.unlimited && <QuotaMiniBar percent={pct} size="sm" />}
<div className="flex items-center justify-between gap-2 text-[10px] text-text-muted tabular-nums">
<span>
{showUsage && (
<>
{usedNum.toLocaleString()} / {totalNum.toLocaleString()}
</>
)}
</span>
{q.staleAfterReset ? (
<span title={t("refreshing")}>⟳</span>
) : cd ? (
<span>
⏱ {t("resetsIn")} {cd}
</span>
) : null}
</div>
</div>
);
}
export default function QuotaCardExpanded({
quotas,
quotaGroups = [],
providerId,
loading,
error,
message,
billing,
paidCredits,
refreshedAt,
hasStaleData,
onRefresh,
onOpenCutoff,
onOpenCost,
onOpenResetCredits,
canEditCutoff,
hasCutoffOverrides,
canRedeemResetCredit = false,
redeemingResetCredit = false,
loadingResetCredits = false,
hiddenQuotaRows = [],
onHideQuota,
onShowQuota,
speed,
}: Props) {
const t = useTranslations("usage");
const tr = (key: string, fallback: string, values?: UsageTranslationValues) =>
translateUsageOrFallback(t, key, fallback, values);
const [expanded, setExpanded] = useState(false);
const resolvedAntigravityGroups = useMemo(
() =>
providerId === "antigravity" || providerId === "agy"
? resolveAntigravityQuotaGroups(quotaGroups, quotas)
: [],
[providerId, quotaGroups, quotas]
);
const sortedQuotas = useMemo(
() => resolveQuotaDisplayOrder(providerId, quotas),
[quotas, providerId]
);
const visibleQuotas = useMemo(
() => getVisibleQuotas(sortedQuotas, expanded),
[sortedQuotas, expanded]
);
const hiddenCount = sortedQuotas.length - visibleQuotas.length;
// Only Kilo Code cards host the dedicated Kilo Pass meter; the collapsed display row carries
// the derived meter values while balance/renewal metadata render below it.
const kiloPassRow = providerId === "kilocode" ? findKiloPassQuotaRow(sortedQuotas) : null;
const refreshedLabel = refreshedAt
? new Date(refreshedAt).toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
})
: null;
return (
<div className="border-t border-border bg-bg-subtle/30 px-3 py-2.5 flex flex-col gap-1.5">
{shouldShowLoadingPlaceholder(loading, sortedQuotas.length, message) ? (
<div className="text-[11px] text-text-muted flex items-center gap-1.5">
<span className="material-symbols-outlined animate-spin text-[13px]">
progress_activity
</span>
{t("loadingQuotas")}
</div>
) : error ? (
<div className="text-[11px] text-red-500 flex items-start gap-1.5">
<span className="material-symbols-outlined text-[13px]">error</span>
<span>{error}</span>
</div>
) : (providerId === "antigravity" || providerId === "agy") &&
resolvedAntigravityGroups.length > 0 ? (
<AntigravityQuotaGroups groups={resolvedAntigravityGroups} />
) : quotas.length === 0 && message ? (
<div className="text-[11px] text-text-muted italic" title={message}>
{message}
</div>
) : quotas.length === 0 ? (
<div className="text-[11px] text-text-muted italic">{t("noQuotaData")}</div>
) : (
<div className="flex flex-col divide-y divide-border/40">
{visibleQuotas.map((q, i) => (
<QuotaDetailRow
key={`${q.name}-${q.modelKey ?? ""}-${i}`}
q={q}
kiloPassRow={kiloPassRow}
onHideQuota={onHideQuota}
onOpenResetCredits={q.isResetCredits ? onOpenResetCredits : undefined}
loadingResetCredits={loadingResetCredits}
/>
))}
</div>
)}
{isProviderBillingProvider(providerId) && billing && (
<ProviderBillingDetails billing={billing} />
)}
{providerId === "codex" && paidCredits && (
<div className="flex justify-between gap-2 border-t border-border/40 pt-2 text-[11px] text-text-main">
<span>{t("codexPaidCreditsLabel")}</span>
<span className="font-semibold">
{paidCredits.overageLimitReached || !hasCodexPaidCredits(paidCredits)
? t("codexPaidCreditsUnavailable")
: paidCredits.unlimited
? t("codexPaidCreditsUnlimited")
: paidCredits.balance === null
? t("codexPaidCreditsAvailable")
: paidCredits.balance.toLocaleString()}
</span>
</div>
)}
{!error && <QuotaCardSpeed speed={speed} />}
{hiddenQuotaRows.length > 0 && (
<div className="flex flex-wrap items-center gap-1 border-t border-border/40 pt-1.5 text-[10px] text-text-muted">
<span className="material-symbols-outlined text-[12px]">visibility_off</span>
<span>{tr("hiddenQuotaRowsLabel", "Hidden:")}</span>
{hiddenQuotaRows.map((q) => (
<button
key={getQuotaVisibilityKey(q)}
type="button"
onClick={(e) => {
e.stopPropagation();
onShowQuota?.(q);
}}
className="rounded-md border border-border px-1.5 py-0.5 transition-colors hover:bg-black/5 hover:text-text-main dark:hover:bg-white/5"
title={translateUsageOrFallback(t, "showQuotaRow", "Show this quota row")}
>
{q.displayName || formatQuotaLabel(q.name)}
</button>
))}
</div>
)}
{!error && sortedQuotas.length > DEFAULT_VISIBLE_ROWS && (
<button
type="button"
onClick={(e) => {
e.stopPropagation();
setExpanded((prev) => !prev);
}}
className="inline-flex items-center justify-center gap-1 text-[11px] font-medium px-2 py-1 rounded-md border border-border bg-bg-subtle hover:bg-black/[0.04] dark:hover:bg-white/[0.04] cursor-pointer"
>
<span className="material-symbols-outlined text-[12px]">
{expanded ? "expand_less" : "expand_more"}
</span>
{expanded
? tr("showLessQuotas", "Show less")
: tr("showMoreQuotas", `Show ${hiddenCount} more`, { count: hiddenCount })}
</button>
)}
<div className="min-w-0 space-y-1.5 pt-1.5 border-t border-border/40">
<div className="flex min-w-0 items-center justify-between gap-2">
{refreshedLabel && (
<span
className={`text-[10px] tabular-nums ${
hasStaleData ? "text-amber-500" : "text-text-muted"
}`}
title={
hasStaleData
? t("staleQuotaTooltip")
: `${tr("lastRefreshed", "Last refreshed")}: ${refreshedLabel}`
}
>
{tr("updatedShort", "Updated")} {refreshedLabel}
</span>
)}
<button
type="button"
disabled={loading}
title={tr("forceRefresh", "Refresh now")}
aria-label={tr("forceRefresh", "Refresh now")}
onClick={(e) => {
e.stopPropagation();
onRefresh();
}}
className="ml-auto inline-flex size-7 shrink-0 items-center justify-center rounded-md border border-border bg-bg-subtle hover:bg-black/[0.04] dark:hover:bg-white/[0.04] disabled:opacity-40 disabled:cursor-not-allowed cursor-pointer"
>
<span
className={`material-symbols-outlined text-[12px] ${loading ? "animate-spin" : ""}`}
>
refresh
</span>
</button>
</div>
<div className="grid min-w-0 grid-cols-[repeat(auto-fit,minmax(min(100%,5rem),1fr))] gap-1.5">
{canRedeemResetCredit && (
<button
type="button"
disabled={loading || redeemingResetCredit || loadingResetCredits}
onClick={(e) => {
e.stopPropagation();
onOpenResetCredits?.();
}}
className="inline-flex min-w-0 items-center justify-center gap-1 break-words text-[11px] font-medium px-1.5 py-1.5 rounded-md border border-primary/40 text-primary bg-bg-subtle hover:bg-black/[0.04] dark:hover:bg-white/[0.04] disabled:opacity-40 disabled:cursor-not-allowed cursor-pointer"
>
<span
className={`material-symbols-outlined text-[12px] ${
redeemingResetCredit || loadingResetCredits ? "animate-spin" : ""
}`}
>
{redeemingResetCredit || loadingResetCredits ? "progress_activity" : "restart_alt"}
</span>
<span className="min-w-0">{tr("manageResetCredits", "View credits")}</span>
</button>
)}
<button
type="button"
disabled={!canEditCutoff}
onClick={(e) => {
e.stopPropagation();
onOpenCutoff();
}}
className={`inline-flex min-w-0 items-center justify-center gap-1 break-words text-[11px] font-medium px-1.5 py-1.5 rounded-md border bg-bg-subtle hover:bg-black/[0.04] dark:hover:bg-white/[0.04] disabled:opacity-40 disabled:cursor-not-allowed cursor-pointer ${
hasCutoffOverrides ? "border-primary/40 text-primary" : "border-border"
}`}
>
<span className="material-symbols-outlined text-[12px]">tune</span>
<span className="min-w-0">{tr("editCutoffs", "Edit cutoffs")}</span>
</button>
<button
type="button"
onClick={(e) => {
e.stopPropagation();
onOpenCost();
}}
className="inline-flex min-w-0 items-center justify-center gap-1 break-words text-[11px] font-medium px-1.5 py-1.5 rounded-md border border-border bg-bg-subtle hover:bg-black/[0.04] dark:hover:bg-white/[0.04] cursor-pointer"
>
<span className="material-symbols-outlined text-[12px]">bar_chart</span>
<span className="min-w-0">{t("usdCost")}</span>
</button>
</div>
</div>
</div>
);
}