TypeScript
feat(usage): show even-pace marks on Limits bars (#14477)
oliver6343c28d9507
diff --git a/apps/mobile/src/features/usage/UsageLimitsPooled.tsx b/apps/mobile/src/features/usage/UsageLimitsPooled.tsxindex 4aabf050014..9013a3dfcc9 100644--- a/apps/mobile/src/features/usage/UsageLimitsPooled.tsx+++ b/apps/mobile/src/features/usage/UsageLimitsPooled.tsx@@ -9,6 +9,7 @@ import {cursorUsageWindowDetails,displayLimitWindows,formatResetsIn,+ paceRemainingPercent,remainingPercent,type LimitAccount,type LimitPoolWindow,@@ -146,22 +147,38 @@ function PoolWindowCard({if (!member.window)return <View key={member.account.key} className="h-7 min-w-0 flex-1" />;const { account, window } = member;+ const timeLeft = paceRemainingPercent(window, now);return (<Pressablekey={account.key}accessibilityRole="button"- accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${remainingPercent(window)}% left`}+ accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${remainingPercent(window)}% left${timeLeft === null ? "" : `, ${timeLeft}% of the window left`}`}accessibilityHint="Show account details"onPress={() => openAccount(account)}- className="h-7 min-w-0 flex-1 overflow-hidden rounded-md bg-subtle"+ className="h-7 min-w-0 flex-1 rounded-md bg-subtle">- <AccountSegment- remaining={remainingPercent(window)}- color={color}- refillPercent={window.resetsAt ? member.refillPercent : 0}- />- <View pointerEvents="none" className="absolute inset-0 items-center justify-center">- <Text className="text-xs font-t3-medium tabular-nums text-foreground">+ <View className="absolute inset-0 overflow-hidden rounded-md">+ <AccountSegment+ remaining={remainingPercent(window)}+ color={color}+ refillPercent={window.resetsAt ? member.refillPercent : 0}+ />+ </View>+ {timeLeft !== null ? (+ <View+ pointerEvents="none"+ className="absolute -top-1 -bottom-1 w-0.5 -translate-x-px rounded-full bg-foreground-muted"+ style={{ left: `${timeLeft}%` }}+ />+ ) : null}+ <View+ pointerEvents="none"+ className="absolute inset-0 items-center justify-center overflow-hidden rounded-md"+ >+ <Text+ numberOfLines={1}+ className="max-w-full shrink text-xs font-t3-medium tabular-nums text-foreground"+ >{index + 1}</Text></View>@@ -370,6 +387,7 @@ export function UsageLimitAccountScreen({ route }: AccountScreenProps) {.find((candidate) => candidate.driver === account?.driver)?.windows.find((candidate) => candidate.id === windowId && candidate.kind === windowKind);const window = pool?.members.find((member) => member.account.key === accountKey)?.window;+ const evenPace = window ? paceRemainingPercent(window, now) : null;const reset = pool?.resets.find((candidate) => candidate.member.account.key === accountKey);const [revealed, setRevealed] = useState(false);return (@@ -424,6 +442,11 @@ export function UsageLimitAccountScreen({ route }: AccountScreenProps) {})}</Text>) : null}+ {evenPace !== null ? (+ <Text className="text-sm text-foreground-muted">+ Even pace: {evenPace}% left by now+ </Text>+ ) : null}{reset && reset.restoresPercent > 0 ? (<Text className="text-sm text-foreground-muted">Restores {reset.restoresPercent}% of the pooldiff --git a/apps/mobile/src/features/usage/UsageLimitsSection.tsx b/apps/mobile/src/features/usage/UsageLimitsSection.tsxindex 79ebc9fbd9f..e5a16a8b75f 100644--- a/apps/mobile/src/features/usage/UsageLimitsSection.tsx+++ b/apps/mobile/src/features/usage/UsageLimitsSection.tsx@@ -10,11 +10,11 @@ import {type ServerProviderUsageWindow,} from "@t3tools/contracts";import {- elapsedShare,formatDuration,formatResetsIn,limitsNotice,paceOf,+ paceRemainingPercent,remainingPercent,} from "@t3tools/shared/usageLimits";import { type ReactNode, useEffect, useEffectEvent, useRef, useState } from "react";@@ -46,8 +46,7 @@ function WindowRow(props: {}) {const { window, now } = props;const remaining = remainingPercent(window);- const elapsed = elapsedShare(window, now);- const timeLeft = elapsed === null ? null : Math.round((1 - elapsed) * 100);+ const timeLeft = paceRemainingPercent(window, now);const pace = paceOf(window, now);const resetsIn = formatResetsIn(window, now);return (diff --git a/apps/web/src/components/usage/UsageLimits.tsx b/apps/web/src/components/usage/UsageLimits.tsxindex 632b2df0fde..0ac9cf23629 100644--- a/apps/web/src/components/usage/UsageLimits.tsx+++ b/apps/web/src/components/usage/UsageLimits.tsx@@ -10,11 +10,11 @@ import {} from "@t3tools/contracts";import { useAtomValue } from "@effect/atom-react";import {- elapsedShare,formatDuration,formatResetsIn,type LimitPace,paceOf,+ paceRemainingPercent,remainingPercent,} from "@t3tools/shared/usageLimits";import { GaugeIcon, TrendingDownIcon, TrendingUpIcon } from "lucide-react";@@ -84,8 +84,7 @@ function WindowBar({}) {const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat);const remaining = remainingPercent(window);- const elapsed = elapsedShare(window, now);- const timeLeft = elapsed === null ? null : Math.round((1 - elapsed) * 100);+ const timeLeft = paceRemainingPercent(window, now);const resetsIn = formatResetsIn(window, now);const resetsAt = window.resetsAt? formatUpcomingTimestamp(window.resetsAt, timestampFormat, now)diff --git a/apps/web/src/components/usage/UsageLimitsPooled.tsx b/apps/web/src/components/usage/UsageLimitsPooled.tsxindex 379c6f9d102..52d938cdf5c 100644--- a/apps/web/src/components/usage/UsageLimitsPooled.tsx+++ b/apps/web/src/components/usage/UsageLimitsPooled.tsx@@ -11,6 +11,7 @@ import {type LimitPool,type LimitPoolMember,type LimitPoolWindow,+ paceRemainingPercent,remainingPercent,} from "@t3tools/shared/usageLimits";import { AlertTriangleIcon, ExternalLinkIcon, TicketIcon } from "lucide-react";@@ -137,6 +138,7 @@ function SegmentPopover({}) {const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat);const remaining = remainingPercent(window);+ const timeLeft = paceRemainingPercent(window, now);const resetsIn = formatResetsIn(window, now);const where =account.environments.length > 0@@ -177,6 +179,7 @@ function SegmentPopover({{resetsIn ? ` · ${resetsIn.replace("resets in ", "in ")}` : ""}</Row>) : null}+ {timeLeft !== null ? <Row label="Even pace">{timeLeft}% left by now</Row> : null}{reset && reset.restoresPercent > 0 ? (<Row label="Restores">+{reset.restoresPercent}% of pool</Row>) : null}@@ -226,6 +229,7 @@ function PoolSegment({}) {const [open, setOpen] = useState(false);const remaining = remainingPercent(window);+ const timeLeft = paceRemainingPercent(window, now);const resetsIn = formatResetsIn(window, now);const credits = account.limits.resetCredits?.availableCount ?? 0;return (@@ -236,33 +240,42 @@ function PoolSegment({<buttontype="button"style={{ gridColumn: index, gridRow: 1 }}- aria-label={`${account.displayName ?? (account.email ? accountInitials(account.email) : account.driver)}: ${remaining}% left${resetsIn ? `, ${resetsIn}` : ""}${credits ? `, ${credits} reset ${credits === 1 ? "credit" : "credits"} banked` : ""}`}- className="relative h-5 min-w-0 cursor-pointer overflow-hidden rounded-md bg-muted text-start outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background data-[popup-open]:ring-1 data-[popup-open]:ring-inset data-[popup-open]:ring-border @2xl/pool:h-8"+ aria-label={`${account.displayName ?? (account.email ? accountInitials(account.email) : account.driver)}: ${remaining}% left${resetsIn ? `, ${resetsIn}` : ""}${timeLeft === null ? "" : `, ${timeLeft}% of the window left`}${credits ? `, ${credits} reset ${credits === 1 ? "credit" : "credits"} banked` : ""}`}+ className="relative h-5 min-w-0 cursor-pointer rounded-md bg-muted text-start outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background data-[popup-open]:ring-1 data-[popup-open]:ring-inset data-[popup-open]:ring-border @2xl/pool:h-8"/>}>- <div- aria-hidden- className="absolute inset-y-0 left-0 rounded-md opacity-35"- style={{ width: `${remaining}%`, backgroundColor: color }}- />- {remaining < 100 && reset && refillPercent > 0 ? (+ <div aria-hidden className="absolute inset-0 overflow-hidden rounded-md"><divaria-hidden- className="absolute inset-y-0 right-0 opacity-20"- style={{- width: `${Math.min(100 - remaining, refillPercent)}%`,- backgroundImage: `repeating-linear-gradient(135deg, ${color} 0 1px, transparent 1px 5px)`,- }}+ className="absolute inset-y-0 left-0 rounded-md opacity-35"+ style={{ width: `${remaining}%`, backgroundColor: color }}+ />+ {remaining < 100 && reset && refillPercent > 0 ? (+ <div+ aria-hidden+ className="absolute inset-y-0 right-0 opacity-20"+ style={{+ width: `${Math.min(100 - remaining, refillPercent)}%`,+ backgroundImage: `repeating-linear-gradient(135deg, ${color} 0 1px, transparent 1px 5px)`,+ }}+ />+ ) : null}+ </div>+ {timeLeft !== null ? (+ <span+ aria-hidden+ className="absolute -inset-y-1 w-0.5 -translate-x-1/2 rounded-full bg-muted-foreground"+ style={{ left: `${timeLeft}%` }}/>) : null}<spanaria-hidden- className="absolute inset-0 flex items-center justify-center text-3xs leading-none font-semibold text-foreground/80 tabular-nums @2xl/pool:hidden"+ className="absolute inset-0 flex items-center justify-center overflow-hidden rounded-md text-3xs leading-none font-semibold text-foreground/80 tabular-nums @2xl/pool:hidden">{index}</span>- <div className="relative hidden h-full min-w-0 items-center gap-1.5 px-2 text-xs @2xl/pool:flex">+ <div className="relative hidden h-full min-w-0 items-center gap-1.5 overflow-hidden rounded-md px-2 text-xs @2xl/pool:flex">{showAccountName ? (<AccountNameaccount={account}@@ -271,7 +284,7 @@ function PoolSegment({) : null}<span className="shrink-0 font-semibold text-foreground tabular-nums">{remaining}%</span>{resetsIn || credits ? (- <span className="ms-auto flex shrink-0 items-center gap-1.5 rounded-sm bg-background/85 px-1.5 py-0.5 text-2xs text-foreground tabular-nums">+ <span className="ms-auto flex min-w-0 items-center gap-1.5 overflow-hidden rounded-sm bg-background/85 px-1.5 py-0.5 text-2xs whitespace-nowrap text-foreground tabular-nums">{resetsIn?.replace("resets in ", "↻ ") ?? ""}{credits ? (<>diff --git a/apps/web/src/components/usage/UsagePage.refresh.browser.test.tsx b/apps/web/src/components/usage/UsagePage.refresh.browser.test.tsxindex e174b702d00..928679b122a 100644--- a/apps/web/src/components/usage/UsagePage.refresh.browser.test.tsx+++ b/apps/web/src/components/usage/UsagePage.refresh.browser.test.tsx@@ -124,7 +124,7 @@ const setPresentations = (entries: ReadonlyArray<readonly [EnvironmentId, objectact(async () => appAtomRegistry.set(state.presentations, new Map(entries) as Presentations));const setNow = (iso: string) => vi.mocked(Date.now).mockReturnValue(Date.parse(iso));const resetsIn = (countdown: string) =>- page.getByRole("button", { name: `codex: 60% left, resets in ${countdown}` });+ page.getByRole("button", { name: `codex: 60% left, resets in ${countdown}`, exact: false });const refreshButton = () => page.getByRole("button", { name: "Refresh limits" });const selectMetric = (label: string) => userEvent.click(page.getByRole("button", { name: label }));diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.tsindex 50c0d3080c4..7cc2962f664 100644--- a/packages/shared/src/usageLimits.test.ts+++ b/packages/shared/src/usageLimits.test.ts@@ -22,6 +22,7 @@ import {formatResetsIn,limitsNotice,paceOf,+ paceRemainingPercent,providersWithLimits,remainingPercent,usesChatGptSharing,@@ -72,6 +73,7 @@ describe("pace", () => {it("places the clock three fifths through a five-hour window with two hours left", () => {expect(elapsedShare(window, now)).toBeCloseTo(0.6);+ expect(paceRemainingPercent(window, now)).toBe(40);expect(paceOf(window, now)).toBe("under");expect(paceOf({ ...window, usedPercent: 62 }, now)).toBe("on");expect(paceOf({ ...window, usedPercent: 80 }, now)).toBe("ahead");@@ -80,9 +82,16 @@ describe("pace", () => {it("has no pace without a reset or a duration", () => {expect(paceOf({ ...window, resetsAt: undefined }, now)).toBeNull();expect(paceOf({ ...window, windowDurationMins: undefined }, now)).toBeNull();+ expect(paceRemainingPercent({ ...window, resetsAt: undefined }, now)).toBeNull();+ expect(paceRemainingPercent({ ...window, windowDurationMins: undefined }, now)).toBeNull();expect(formatResetsIn({ ...window, resetsAt: undefined }, now)).toBeNull();});+ it("keeps pace marks within the bar before a window starts and after it expires", () => {+ expect(paceRemainingPercent(window, Date.parse("2026-09-03T08:00:00.000Z"))).toBe(100);+ expect(paceRemainingPercent(window, Date.parse("2026-09-03T15:00:00.000Z"))).toBe(0);+ });+it("phrases the reset as a countdown", () => {expect(formatResetsIn(window, now)).toBe("resets in 2h 0m");expect(formatResetsIn({ ...window, resetsAt: "2026-09-06T15:30:00.000Z" }, now)).toBe(diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.tsindex 5e124174ddb..2ced4094548 100644--- a/packages/shared/src/usageLimits.ts+++ b/packages/shared/src/usageLimits.ts@@ -546,6 +546,15 @@ export function elapsedShare(window: ServerProviderUsageWindow, now: number): nureturn Math.max(0, Math.min(1, (length - (resetsAt - now)) / length));}+// Bars show quota remaining, so even pace tracks time remaining.+export function paceRemainingPercent(+ window: ServerProviderUsageWindow,+ now: number,+): number | null {+ const elapsed = elapsedShare(window, now);+ return elapsed === null ? null : Math.round((1 - elapsed) * 100);+}+export type LimitPace = "ahead" | "on" | "under";export function paceOf(window: ServerProviderUsageWindow, now: number): LimitPace | null {