Skip to content

Commit 5d4c202

Browse files
fix(web,mobile): Limits bars show the even-pace mark as a tick in a gap (#22408)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent f7f9e21 commit 5d4c202

10 files changed

Lines changed: 912 additions & 64 deletions

File tree

‎apps/mobile/src/features/usage/UsageLimitsPooled.tsx‎

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,53 @@ function accountName(account: LimitAccount) {
4141
return `${local[0] ?? ""}${domain[0] ?? ""}`.toUpperCase() || "Account";
4242
}
4343

44+
// Same gap-and-tick as the Limits rows: two clipped copies of the segment leave a see-through gap.
45+
function PaceGap({
46+
timeLeft,
47+
children,
48+
}: {
49+
readonly timeLeft: number | null;
50+
readonly children: ReactNode;
51+
}) {
52+
const [width, setWidth] = useState(0);
53+
const tickX =
54+
timeLeft === null || width < 6
55+
? null
56+
: Math.min(Math.max((width * timeLeft) / 100, 3), width - 3);
57+
return (
58+
<View
59+
pointerEvents="none"
60+
className="absolute inset-0"
61+
onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
62+
>
63+
{tickX === null
64+
? children
65+
: (
66+
[
67+
[0, tickX - 3],
68+
[tickX + 3, width],
69+
] as const
70+
).map(([start, end]) => (
71+
<View
72+
key={start}
73+
className="absolute top-0 bottom-0 overflow-hidden"
74+
style={{ left: start, width: end - start }}
75+
>
76+
<View className="absolute top-0 bottom-0" style={{ left: -start, width }}>
77+
{children}
78+
</View>
79+
</View>
80+
))}
81+
{tickX !== null ? (
82+
<View
83+
className="absolute top-0.5 bottom-0.5 w-0.5 rounded-full bg-foreground"
84+
style={{ left: tickX - 1 }}
85+
/>
86+
) : null}
87+
</View>
88+
);
89+
}
90+
4491
function AccountSegment({
4592
remaining,
4693
color,
@@ -155,22 +202,18 @@ function PoolWindowCard({
155202
accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${remainingPercent(window)}% left${timeLeft === null ? "" : `, ${timeLeft}% of the window left`}`}
156203
accessibilityHint="Show account details"
157204
onPress={() => openAccount(account)}
158-
className="h-7 min-w-0 flex-1 rounded-md bg-subtle"
205+
className="h-7 min-w-0 flex-1 rounded-md"
159206
>
160-
<View className="absolute inset-0 overflow-hidden rounded-md">
161-
<AccountSegment
162-
remaining={remainingPercent(window)}
163-
color={color}
164-
refillPercent={window.resetsAt ? member.refillPercent : 0}
165-
/>
166-
</View>
167-
{timeLeft !== null ? (
168-
<View
169-
pointerEvents="none"
170-
className="absolute -top-1 -bottom-1 w-0.5 -translate-x-px rounded-full bg-foreground-muted"
171-
style={{ left: `${timeLeft}%` }}
172-
/>
173-
) : null}
207+
<PaceGap timeLeft={timeLeft}>
208+
<View className="absolute inset-0 rounded-md bg-subtle" />
209+
<View className="absolute inset-0 overflow-hidden rounded-md">
210+
<AccountSegment
211+
remaining={remainingPercent(window)}
212+
color={color}
213+
refillPercent={window.resetsAt ? member.refillPercent : 0}
214+
/>
215+
</View>
216+
</PaceGap>
174217
<View
175218
pointerEvents="none"
176219
className="absolute inset-0 items-center justify-center overflow-hidden rounded-md"

‎apps/mobile/src/features/usage/UsageLimitsSection.tsx‎

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ function WindowRow(props: {
4949
const timeLeft = paceRemainingPercent(window, now);
5050
const pace = paceOf(window, now);
5151
const resetsIn = formatResetsIn(window, now);
52+
const [barWidth, setBarWidth] = useState(0);
53+
const tickX =
54+
timeLeft === null || barWidth < 6
55+
? null
56+
: Math.min(Math.max((barWidth * timeLeft) / 100, 3), barWidth - 3);
57+
const bar = (
58+
<View
59+
className="h-1.5 flex-row overflow-hidden rounded-full bg-subtle"
60+
style={barWidth > 0 ? { width: barWidth } : undefined}
61+
>
62+
<View
63+
className={
64+
remaining <= 10
65+
? "h-full rounded-full bg-red-500"
66+
: remaining <= 30
67+
? "h-full rounded-full bg-amber-500"
68+
: "h-full rounded-full bg-foreground"
69+
}
70+
style={[
71+
{ flex: remaining },
72+
remaining > 30 && props.color ? { backgroundColor: props.color } : null,
73+
]}
74+
/>
75+
<View style={{ flex: 100 - remaining }} />
76+
</View>
77+
);
5278
return (
5379
<View className="gap-1">
5480
<View className="flex-row items-baseline justify-between gap-3">
@@ -57,27 +83,35 @@ function WindowRow(props: {
5783
{remaining}% left
5884
</Text>
5985
</View>
60-
<View className="h-3 justify-center">
61-
<View className="h-1.5 flex-row overflow-hidden rounded-full bg-subtle">
62-
<View
63-
className={
64-
remaining <= 10
65-
? "h-full rounded-full bg-red-500"
66-
: remaining <= 30
67-
? "h-full rounded-full bg-amber-500"
68-
: "h-full rounded-full bg-foreground"
69-
}
70-
style={[
71-
{ flex: remaining },
72-
remaining > 30 && props.color ? { backgroundColor: props.color } : null,
73-
]}
74-
/>
75-
<View style={{ flex: 100 - remaining }} />
76-
</View>
77-
{timeLeft !== null ? (
86+
<View
87+
className="h-3 justify-center"
88+
onLayout={(event) => setBarWidth(event.nativeEvent.layout.width)}
89+
>
90+
{tickX === null ? (
91+
bar
92+
) : (
93+
<View className="h-1.5">
94+
{/* Clip the same full-width bar so neither fill proportions nor corner radii change. */}
95+
{(
96+
[
97+
[0, tickX - 3],
98+
[tickX + 3, barWidth],
99+
] as const
100+
).map(([start, end]) => (
101+
<View
102+
key={start}
103+
className="absolute top-0 bottom-0 overflow-hidden"
104+
style={{ left: start, width: end - start }}
105+
>
106+
<View style={{ marginLeft: -start }}>{bar}</View>
107+
</View>
108+
))}
109+
</View>
110+
)}
111+
{tickX !== null ? (
78112
<View
79-
className="absolute top-0 bottom-0 w-px bg-foreground"
80-
style={{ left: `${timeLeft}%`, opacity: 0.6 }}
113+
className="absolute top-px bottom-px w-0.5 rounded-full bg-foreground"
114+
style={{ left: tickX - 1 }}
81115
/>
82116
) : null}
83117
</View>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { render } from "../../../test/render";
2+
import { ProviderDriverKind, type ServerProviderUsageWindow } from "@t3tools/contracts";
3+
import { expect, it, vi } from "vite-plus/test";
4+
import { page } from "vite-plus/test/browser";
5+
6+
import "~/index.css";
7+
8+
vi.mock("../../state/session", () => ({
9+
useEnvironmentScope: () => true,
10+
readEnvironmentScope: () => true,
11+
}));
12+
vi.mock("../../state/server", () => ({ serverEnvironment: { consumeResetCredit: null } }));
13+
vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() }));
14+
vi.mock("../../hooks/useSettings", () => ({ usePrimarySettings: () => "24h" }));
15+
16+
import { LimitWindows } from "./UsageLimits";
17+
18+
const codex = ProviderDriverKind.make("codex");
19+
const now = Date.parse("2026-09-03T12:00:00Z");
20+
21+
const sessionWindow = (minutesUntilReset: number | null): ServerProviderUsageWindow => ({
22+
id: "five_hour",
23+
kind: "session",
24+
label: "Session",
25+
usedPercent: 40,
26+
...(minutesUntilReset === null
27+
? {}
28+
: {
29+
windowDurationMins: 300,
30+
resetsAt: new Date(now + minutesUntilReset * 60_000).toISOString(),
31+
}),
32+
});
33+
34+
const sized = (width: string) => {
35+
const container = document.createElement("div");
36+
container.style.width = width;
37+
return container;
38+
};
39+
40+
const bar = () => {
41+
const element = page.getByRole("img", { name: /^Session:/ }).element();
42+
const mark = element.querySelector(":scope > span[aria-hidden]");
43+
return {
44+
rect: element.getBoundingClientRect(),
45+
mark: mark?.getBoundingClientRect(),
46+
clip: getComputedStyle(element.querySelector(":scope > div")!).clipPath,
47+
};
48+
};
49+
50+
it("places the even-pace tick in a gap at the share of the window left", async () => {
51+
await using _screen = await render(
52+
<LimitWindows driver={codex} windows={[sessionWindow(90)]} now={now} />,
53+
{ container: sized("640px") },
54+
);
55+
const { rect, mark, clip } = bar();
56+
expect(mark!.left + mark!.width / 2 - rect.left).toBeCloseTo(rect.width * 0.3, 0);
57+
expect(clip).toMatch(/^polygon/);
58+
});
59+
60+
it("keeps the tick inside the bar at both ends of the window", async () => {
61+
for (const minutesUntilReset of [0, 300]) {
62+
await using _screen = await render(
63+
<LimitWindows driver={codex} windows={[sessionWindow(minutesUntilReset)]} now={now} />,
64+
{ container: sized("640px") },
65+
);
66+
const { rect, mark } = bar();
67+
expect(mark!.left).toBeGreaterThanOrEqual(rect.left);
68+
expect(mark!.right).toBeLessThanOrEqual(rect.right);
69+
}
70+
});
71+
72+
it("draws a continuous bar when the window timing is unknown", async () => {
73+
await using _screen = await render(
74+
<LimitWindows driver={codex} windows={[sessionWindow(null)]} now={now} />,
75+
{ container: sized("640px") },
76+
);
77+
expect(bar()).toMatchObject({ mark: undefined, clip: "none" });
78+
});
79+
80+
it("draws neither gap nor tick on a bar narrower than 6px", async () => {
81+
// 11rem + 1rem + 3.5px + 1rem + 7rem: the bar column gets 3.5px.
82+
await using _screen = await render(
83+
<LimitWindows driver={codex} windows={[sessionWindow(300)]} now={now} />,
84+
{ container: sized("323.5px") },
85+
);
86+
const { rect, mark, clip } = bar();
87+
expect(rect.width).toBeCloseTo(3.5, 1);
88+
expect({ mark, clip }).toEqual({ mark: undefined, clip: "none" });
89+
});

‎apps/web/src/components/usage/UsageLimits.tsx‎

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import {
1818
remainingPercent,
1919
} from "@t3tools/shared/usageLimits";
2020
import { GaugeIcon, TrendingDownIcon, TrendingUpIcon } from "lucide-react";
21-
import { Fragment, type ReactNode, useState } from "react";
21+
import { Fragment, type ReactNode, useLayoutEffect, useState } from "react";
2222

2323
import { usePrimarySettings } from "../../hooks/useSettings";
24+
import { observeResize } from "../../lib/observeResize";
2425
import { environmentPresentations } from "../../state/presentation";
2526
import { useEnvironmentScope, readEnvironmentScope } from "../../state/session";
2627
import { serverEnvironment } from "../../state/server";
@@ -73,6 +74,43 @@ export function PaceIcon({ pace }: { readonly pace: LimitPace }) {
7374
);
7475
}
7576

77+
// The tick sits in a transparent gap cut from the bar, clamped so it stays inside at 0% and 100%.
78+
// The gap is a clip-path slot rather than a mask: a mask composites its layer and re-antialiases
79+
// the bar's rounded ends, and the polygon reaches 1px past the box so only the slot edges clip.
80+
function paceMarkStyles(timeLeft: number | null) {
81+
if (timeLeft === null) return null;
82+
const at = `clamp(3px, ${timeLeft}%, calc(100% - 3px))`;
83+
const [left, right] = [`calc(${at} - 3px)`, `calc(${at} + 3px)`];
84+
const [near, far] = ["-1px", "calc(100% + 1px)"];
85+
return {
86+
tick: { left: at },
87+
gap: {
88+
clipPath: `polygon(${near} ${near}, ${left} ${near}, ${left} ${far}, ${right} ${far}, ${right} ${near}, ${far} ${near}, ${far} ${far}, ${near} ${far})`,
89+
},
90+
// For a track painted on an element that also draws focus rings, which a clip would cut.
91+
gappedTrack: {
92+
backgroundColor: "transparent",
93+
backgroundImage: `linear-gradient(to right, var(--color-muted) ${left}, transparent ${left}, transparent ${right}, var(--color-muted) ${right})`,
94+
},
95+
};
96+
}
97+
98+
/**
99+
* Pace mark styles for the bar given the returned ref. Below 6px there is no room for the gap
100+
* and tick, so the bar draws neither, as the mobile bars do.
101+
*/
102+
export function usePaceMark(timeLeft: number | null) {
103+
const [bar, setBar] = useState<HTMLElement | null>(null);
104+
const [roomy, setRoomy] = useState(false);
105+
useLayoutEffect(() => {
106+
if (!bar) return;
107+
const measure = () => setRoomy(bar.getBoundingClientRect().width >= 6);
108+
measure();
109+
return observeResize(bar, measure);
110+
}, [bar]);
111+
return [setBar, roomy ? paceMarkStyles(timeLeft) : null] as const;
112+
}
113+
76114
function WindowBar({
77115
color,
78116
window,
@@ -85,6 +123,7 @@ function WindowBar({
85123
const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat);
86124
const remaining = remainingPercent(window);
87125
const timeLeft = paceRemainingPercent(window, now);
126+
const [barRef, paceMark] = usePaceMark(timeLeft);
88127
const resetsIn = formatResetsIn(window, now);
89128
const resetsAt = window.resetsAt
90129
? formatUpcomingTimestamp(window.resetsAt, timestampFormat, now)
@@ -98,25 +137,29 @@ function WindowBar({
98137
<TooltipTrigger
99138
render={
100139
<div
140+
ref={barRef}
101141
role="img"
102142
aria-label={summary}
103143
tabIndex={0}
104144
className="relative h-6 cursor-default rounded-full outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
105145
/>
106146
}
107147
>
108-
<div className="absolute inset-x-0 inset-y-1.5 rounded-full bg-muted" />
109-
{remaining > 0 ? (
110-
<div
111-
className="absolute inset-y-1.5 left-0 rounded-full"
112-
style={{ width: `${remaining}%`, backgroundColor: color }}
113-
/>
114-
) : null}
115-
{timeLeft !== null ? (
148+
{/* Clip both layers so the gap exposes even a translucent composer surface. */}
149+
<div className="absolute inset-0" style={paceMark?.gap}>
150+
<div className="absolute inset-x-0 inset-y-1.5 rounded-full bg-muted" />
151+
{remaining > 0 ? (
152+
<div
153+
className="absolute inset-y-1.5 left-0 rounded-full"
154+
style={{ width: `${remaining}%`, backgroundColor: color }}
155+
/>
156+
) : null}
157+
</div>
158+
{paceMark ? (
116159
<span
117160
aria-hidden
118-
className="absolute inset-y-0.5 w-px -translate-x-1/2 bg-foreground/60"
119-
style={{ left: `${timeLeft}%` }}
161+
className="absolute inset-y-1 w-0.5 -translate-x-1/2 rounded-full bg-foreground"
162+
style={paceMark.tick}
120163
/>
121164
) : null}
122165
</TooltipTrigger>

‎apps/web/src/components/usage/UsageLimitsPooled.browser.test.tsx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,23 @@ it("omits an empty reset plate but keeps countdowns and banked credits visible",
177177
expect(plate()?.textContent).toBe("1");
178178
});
179179

180+
it("cuts the pace gap through the segment track and keeps the tick inside it", async () => {
181+
setup();
182+
// Five hours before the reset is the start of the window, the right-hand end of the bar.
183+
await using _screen = await render(
184+
<UsageLimitsPooled
185+
presentations={presentations("2026-09-03T14:00:00Z", 0)}
186+
now={Date.parse("2026-09-03T09:00:00Z")}
187+
/>,
188+
);
189+
const segment = page.getByRole("button", { name: /^Alpha:/ }).element();
190+
const tick = segment.querySelector(':scope > span[style*="left"]')!.getBoundingClientRect();
191+
const rect = segment.getBoundingClientRect();
192+
expect(getComputedStyle(segment).backgroundImage).toMatch(/^linear-gradient/);
193+
expect(tick.left).toBeGreaterThanOrEqual(rect.left);
194+
expect(tick.right).toBeLessThanOrEqual(rect.right);
195+
});
196+
180197
it("moves the one limit popover between windows on hover", async () => {
181198
setup();
182199
await using screen = await render(initial());

0 commit comments

Comments
 (0)