import { AuthProvidersManageScope, type EnvironmentId, type ProviderConsumeResetCreditOutcome, ProviderConsumeResetCreditInput, ServerProvider, ServerProviderResetCredits, ServerProviderUsageWindow, UsageProviderKind, } from "@t3tools/contracts"; import { useAtomValue } from "@effect/atom-react"; import { normalizeProviderAccentColor } from "@t3tools/client-runtime/state/provider-instance-display"; import { type LimitPace, paceOf, paceRemainingPercent, remainingPercent, type ResetCreditChoice, } from "@t3tools/shared/usageLimits"; import { GaugeIcon, TrendingDownIcon, TrendingUpIcon } from "lucide-react"; import { msg, T, useGT, useMessages } from "gt-react"; import { Fragment, useLayoutEffect, useState } from "react"; import { usePrimarySettings } from "../../hooks/useSettings"; import { observeResize } from "../../lib/observeResize"; import { environmentPresentations } from "../../state/presentation"; import { useEnvironmentScope, readEnvironmentScope } from "../../state/session"; import { serverEnvironment } from "../../state/server"; import { useAtomCommand } from "../../state/use-atom-command"; import { formatUpcomingTimestamp } from "../../timestampFormat"; import { AlertDialog, AlertDialogClose, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogPopup, AlertDialogTitle, } from "../ui/alert-dialog"; import { Button } from "../ui/button"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { type LimitDisplay, UsageLimitsPooled } from "./UsageLimitsPooled"; import { formatResetsIn, resetCreditChoices } from "@t3tools/shared/usageLimitsCopy"; import { PROVIDER_PRESENTATION } from "./usageProviders"; const PACE: Record = { ahead: { label: msg("Ahead of pace: spending faster than the window elapses"), icon: TrendingUpIcon, }, on: { label: msg("On pace with the window"), icon: GaugeIcon }, under: { label: msg("Under pace: headroom left for the rest of the window"), icon: TrendingDownIcon, }, }; function barColor(driver: ServerProvider["driver"]): string { const presentation = Object.values(PROVIDER_PRESENTATION).find( (entry) => entry.driverKind === driver, ); return presentation?.color ?? "var(--foreground)"; } function PaceIcon({ pace }: { readonly pace: LimitPace }) { const Icon = PACE[pace].icon; const m = useMessages(); return ( } > {m(PACE[pace].label)} ); } // The tick sits in a transparent gap cut from the bar, clamped so it stays inside at 0% and 100%. // The gap is a clip-path slot rather than a mask: a mask composites its layer and re-antialiases // the bar's rounded ends, and the polygon reaches 1px past the box so only the slot edges clip. function paceMarkStyles(timeLeft: number | null) { if (timeLeft === null) return null; const at = `clamp(3px, ${timeLeft}%, calc(100% - 3px))`; const [left, right] = [`calc(${at} - 3px)`, `calc(${at} + 3px)`]; const [near, far] = ["-1px", "calc(100% + 1px)"]; return { tick: { left: at }, gap: { clipPath: `polygon(${near} ${near}, ${left} ${near}, ${left} ${far}, ${right} ${far}, ${right} ${near}, ${far} ${near}, ${far} ${far}, ${near} ${far})`, }, // For a track painted on an element that also draws focus rings, which a clip would cut. gappedTrack: { backgroundColor: "transparent", backgroundImage: `linear-gradient(to right, var(--color-muted) ${left}, transparent ${left}, transparent ${right}, var(--color-muted) ${right})`, }, }; } /** * Pace mark styles for the bar given the returned ref. Below 6px there is no room for the gap * and tick, so the bar draws neither, as the mobile bars do. */ function usePaceMark(timeLeft: number | null) { const [bar, setBar] = useState(null); const [roomy, setRoomy] = useState(false); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- measures the bar DOM width before paint and remeasures through a ResizeObserver useLayoutEffect(() => { if (!bar) return; const measure = () => setRoomy(bar.getBoundingClientRect().width >= 6); measure(); return observeResize(bar, measure); }, [bar]); return [setBar, roomy ? paceMarkStyles(timeLeft) : null] as const; } function WindowBar({ color, window, now, }: { readonly color: string; readonly window: ServerProviderUsageWindow; readonly now: number; }) { const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat); const gt = useGT(); const m = useMessages(); const remaining = remainingPercent(window); const timeLeft = paceRemainingPercent(window, now); const [barRef, paceMark] = usePaceMark(timeLeft); const resetsIn = formatResetsIn(window, now, m); const resetsAt = window.resetsAt ? formatUpcomingTimestamp(window.resetsAt, timestampFormat, now, undefined, m) : null; const label = window.label; const summary = timeLeft === null ? resetsIn ? gt("{label}: {remaining}% left, {resetsIn}", { label, remaining, resetsIn }) : gt("{label}: {remaining}% left", { label, remaining }) : resetsIn ? gt("{label}: {remaining}% left, {timeLeft}% of the window left, {resetsIn}", { label, remaining, timeLeft, resetsIn, }) : gt("{label}: {remaining}% left, {timeLeft}% of the window left", { label, remaining, timeLeft, }); return ( } > {/* Clip both layers so the gap exposes even a translucent composer surface. */}
{remaining > 0 ? (
) : null}
{paceMark ? ( ) : null}
{timeLeft !== null ? gt("{remaining}% left · {timeLeft}% of the window left", { remaining, timeLeft }) : gt("{remaining}% left", { remaining })} {timeLeft !== null ? ( The line is where even spending would be. ) : null} {resetsAt ? ( {resetsIn ? gt("Resets {resetsAt} · {resetsIn}", { resetsAt, resetsIn }) : gt("Resets {resetsAt}", { resetsAt })} ) : null}
); } export function LimitWindows({ driver, accentColor, windows, now, compact = false, }: { readonly driver: ServerProvider["driver"]; readonly accentColor?: string | undefined; readonly windows: ReadonlyArray; readonly now: number; readonly compact?: boolean; }) { const color = normalizeProviderAccentColor(accentColor) ?? barColor(driver); const gt = useGT(); const m = useMessages(); return (
{windows.map((window) => { const pace = paceOf(window, now); const resetsIn = formatResetsIn(window, now, m); return ( {window.label} {gt("{remaining}% left", { remaining: remainingPercent(window) })} {pace ? : null} {resetsIn ?? ""} ); })}
); } const OUTCOME_TEXT: Record = { reset: msg("Reset applied. Your windows have cleared."), nothingToReset: msg("Nothing to reset right now."), noCredit: msg("No reset credit left."), alreadyRedeemed: msg("That credit was already redeemed."), }; export function useResetCredit(environmentId: EnvironmentId) { const canManageProviders = useEnvironmentScope(environmentId, AuthProvidersManageScope); const consume = useAtomCommand(serverEnvironment.consumeResetCredit, { reportFailure: false }); const m = useMessages(); // Kept after close so the dialog's text survives its exit animation. const [choice, setChoice] = useState(null); const [confirming, setConfirming] = useState(false); const [busy, setBusy] = useState(false); const [status, setStatus] = useState(null); const redeem = async () => { setConfirming(false); if (!choice || !readEnvironmentScope(environmentId, AuthProvidersManageScope)) return; setBusy(true); setStatus(null); const result = await consume({ environmentId, input: choice.input }); setBusy(false); if (result._tag === "Success") { setStatus(result.value.warning ?? m(OUTCOME_TEXT[result.value.outcome])); return; } setStatus( "error" in result.cause && result.cause.error instanceof Error ? result.cause.error.message : m(msg("Could not use the reset credit.")), ); }; const confirm = (next: ResetCreditChoice) => { if (!readEnvironmentScope(environmentId, AuthProvidersManageScope)) return; setChoice(next); setConfirming(true); }; return { canManageProviders, choice, confirming, setConfirming, confirm, busy, status, redeem }; } /** Require confirmation because redemption spends credits. Mount outside popovers, which stack above * dialogs and unmount on close. */ export function ResetCreditDialog({ open, onOpenChange, choice, onConfirm, disabled = false, }: { readonly open: boolean; readonly onOpenChange: (open: boolean) => void; readonly choice: ResetCreditChoice | null; readonly onConfirm: () => void; readonly disabled?: boolean; }) { return ( Use a reset credit? {choice?.confirmDescription} }> Cancel ); } export function ResetCredits({ environmentId, input, credits, now, }: { readonly environmentId: EnvironmentId; readonly input: ProviderConsumeResetCreditInput; readonly credits: ServerProviderResetCredits; readonly now: number; }) { const { canManageProviders, choice, confirming, setConfirming, confirm, busy, status, redeem } = useResetCredit(environmentId); const gt = useGT(); const m = useMessages(); if (credits.availableCount === 0 && status === null) return null; return (
{resetCreditChoices(credits, input, now, false, m).map((choice) => (
{choice.summary} {choice.availableCount > 0 ? ( ) : null}
))} {status ? {status} : null} void redeem()} disabled={!canManageProviders} />
); } /** Advance now only on explicit refresh to avoid page repaints from ticking. */ export function UsageLimitsSection({ selectedEnvironmentIds, hiddenProviders, now, display, hideEmails, }: { readonly selectedEnvironmentIds: ReadonlySet | null; readonly hiddenProviders: ReadonlySet; readonly now: number; readonly display: LimitDisplay; readonly hideEmails: boolean; }) { const presentations = useAtomValue(environmentPresentations.presentationsWithProvidersAtom); const hiddenDrivers = new Set( [...hiddenProviders].map((provider) => PROVIDER_PRESENTATION[provider].driverKind), ); const isVisible = (driver: ServerProvider["driver"]) => !hiddenDrivers.has(driver); const selected = new Map( [...presentations].flatMap(([id, presentation]) => { if (selectedEnvironmentIds !== null && !selectedEnvironmentIds.has(id)) return []; const config = presentation.serverConfig; if (hiddenDrivers.size === 0 || config === null) return [[id, presentation] as const]; return [ [ id, { ...presentation, serverConfig: { ...config, providers: config.providers.filter((provider) => isVisible(provider.driver)), usageLimitSources: config.usageLimitSources?.flatMap((source) => { const accounts = source.accounts.filter((account) => isVisible(account.driver)); return accounts.length === 0 && source.accounts.length > 0 ? [] : [{ ...source, accounts }]; }), }, }, ] as const, ]; }), ); return ( ); }