Skip to content

Commit 16c8742

Browse files
badmikeclaude
andcommitted
feat(usage): mark the even-pace point on pooled limit bars
The pooled bars on Usage → Limits show how much quota is left but not whether that is a lot or a little for the time still to run. A weekly window at 41% left is comfortable on day six and alarming on day two, and the bar reads the same either way. Each account segment now carries a tick above and below it at the point even spending would have reached, so the distance between the tick and the fill edge is the headroom. The tick is drawn in the provider colour that fills the bar, and only when the window reports both a duration and a reset; without a timeframe there is nothing to compare against and no tick appears. The segment popover gains the exact figures. The maths lives in the shared usageLimits module, so web and mobile agree on it, and the two views that already drew this marker inline now call it instead of repeating the arithmetic. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent d29c56a commit 16c8742

6 files changed

Lines changed: 197 additions & 77 deletions

File tree

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

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { useAtomValue } from "@effect/atom-react";
22
import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
3-
import { EnvironmentId } from "@t3tools/contracts";
3+
import { EnvironmentId, type ServerProviderUsageWindow } from "@t3tools/contracts";
44
import {
55
collectLimitAccounts,
66
collectLimitNotices,
77
collectLimitPools,
8+
evenPacePercent,
89
formatDuration,
910
formatResetsIn,
11+
paceGapLabel,
1012
remainingPercent,
1113
type LimitAccount,
1214
type LimitPoolWindow,
@@ -66,6 +68,38 @@ function AccountSegment({
6668
);
6769
}
6870

71+
/**
72+
* Where even spending would have left the fill, as a tick above and below the
73+
* segment rather than a line across it: the segment clips its own fill, and a
74+
* mark outside the bar reads without competing with the position number. The
75+
* provider colour ties it to the fill it is measured against. Drawn only when
76+
* the window reports a length and a reset; without both there is no timeframe
77+
* to compare against.
78+
*
79+
* The tick is shorter than the room its wrapper reserves, and the difference is
80+
* the gap that keeps it reading as a tick. Flush against the bar, the two run
81+
* together into one line.
82+
*/
83+
function EvenPaceTicks({
84+
window,
85+
now,
86+
color,
87+
}: {
88+
readonly window: ServerProviderUsageWindow;
89+
readonly now: number;
90+
readonly color: string;
91+
}) {
92+
const evenPace = evenPacePercent(window, now);
93+
if (evenPace === null) return null;
94+
const tick = { left: `${evenPace}%`, width: 1, height: 6, backgroundColor: color } as const;
95+
return (
96+
<View pointerEvents="none" className="absolute inset-0">
97+
<View className="absolute top-0" style={tick} />
98+
<View className="absolute bottom-0" style={tick} />
99+
</View>
100+
);
101+
}
102+
69103
function PoolWindowCard({
70104
pool,
71105
color,
@@ -118,26 +152,31 @@ function PoolWindowCard({
118152
<View className="flex-row gap-1">
119153
{pool.columns.map(({ account, window }, index) => {
120154
if (!window) return <View key={account.key} className="h-7 min-w-0 flex-1" />;
155+
const paceGap = paceGapLabel(window, now);
121156
return (
122-
<Pressable
123-
key={account.key}
124-
accessibilityRole="button"
125-
accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${remainingPercent(window)}% left`}
126-
accessibilityHint="Show account details"
127-
onPress={() => openAccount(account)}
128-
className="h-7 min-w-0 flex-1 overflow-hidden rounded-md bg-subtle"
129-
>
130-
<AccountSegment
131-
remaining={remainingPercent(window)}
132-
color={color}
133-
pending={Boolean(window.resetsAt)}
134-
/>
135-
<View pointerEvents="none" className="absolute inset-0 items-center justify-center">
136-
<Text className="text-xs font-t3-medium tabular-nums text-foreground">
137-
{index + 1}
138-
</Text>
139-
</View>
140-
</Pressable>
157+
// The wrapper reserves the ticks' room; the segment clips its fill, so they sit
158+
// outside it. Centring keeps both gaps equal when the row stretches the wrapper.
159+
<View key={account.key} className="relative min-w-0 flex-1 justify-center py-2.5">
160+
<Pressable
161+
accessibilityRole="button"
162+
accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${remainingPercent(window)}% left${paceGap ? `, ${paceGap}` : ""}`}
163+
accessibilityHint="Show account details"
164+
onPress={() => openAccount(account)}
165+
className="h-7 overflow-hidden rounded-md bg-subtle"
166+
>
167+
<AccountSegment
168+
remaining={remainingPercent(window)}
169+
color={color}
170+
pending={Boolean(window.resetsAt)}
171+
/>
172+
<View pointerEvents="none" className="absolute inset-0 items-center justify-center">
173+
<Text className="text-xs font-t3-medium tabular-nums text-foreground">
174+
{index + 1}
175+
</Text>
176+
</View>
177+
</Pressable>
178+
<EvenPaceTicks window={window} now={now} color={color} />
179+
</View>
141180
);
142181
})}
143182
</View>
@@ -338,6 +377,11 @@ export function UsageLimitAccountScreen({ route }: AccountScreenProps) {
338377
})}
339378
</Text>
340379
) : null}
380+
{evenPacePercent(window, now) !== null ? (
381+
<Text className="text-sm text-foreground-muted">
382+
{evenPacePercent(window, now)}% at even pace · {paceGapLabel(window, now)}
383+
</Text>
384+
) : null}
341385
{reset && reset.restoresPercent > 0 ? (
342386
<Text className="text-sm text-foreground-muted">
343387
Restores {reset.restoresPercent}% of the pool

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
UsageProviderKind,
1010
} from "@t3tools/contracts";
1111
import {
12-
elapsedShare,
12+
evenPacePercent,
1313
formatDuration,
1414
formatResetsIn,
1515
limitsNotice,
@@ -51,8 +51,7 @@ function WindowRow(props: {
5151
}) {
5252
const { window, now } = props;
5353
const remaining = remainingPercent(window);
54-
const elapsed = elapsedShare(window, now);
55-
const timeLeft = elapsed === null ? null : Math.round((1 - elapsed) * 100);
54+
const timeLeft = evenPacePercent(window, now);
5655
const pace = paceOf(window, now);
5756
const resetsIn = formatResetsIn(window, now);
5857
return (

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "@t3tools/contracts";
1010
import { useAtomValue } from "@effect/atom-react";
1111
import {
12-
elapsedShare,
12+
evenPacePercent,
1313
formatDuration,
1414
formatResetsIn,
1515
type LimitPace,
@@ -89,9 +89,8 @@ function WindowBar({
8989
}) {
9090
const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat);
9191
const remaining = remainingPercent(window);
92-
const elapsed = elapsedShare(window, now);
9392
// The fill is quota left, so the even-spending mark is the time left.
94-
const timeLeft = elapsed === null ? null : Math.round((1 - elapsed) * 100);
93+
const timeLeft = evenPacePercent(window, now);
9594
const resetsIn = formatResetsIn(window, now);
9695
const resetsAt = window.resetsAt
9796
? formatUpcomingTimestamp(window.resetsAt, timestampFormat, now)

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

Lines changed: 94 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import {
22
collectLimitAccounts,
33
collectLimitNotices,
44
collectLimitPools,
5+
evenPacePercent,
56
formatDuration,
67
formatResetsIn,
78
type LimitAccount,
89
type LimitPool,
910
type LimitPoolMember,
1011
type LimitPoolWindow,
12+
paceGapLabel,
1113
remainingPercent,
1214
} from "@t3tools/shared/usageLimits";
1315
import { TicketIcon } from "lucide-react";
@@ -147,6 +149,7 @@ function SegmentPopover({
147149
}) {
148150
const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat);
149151
const remaining = remainingPercent(window);
152+
const evenPace = evenPacePercent(window, now);
150153
const resetsIn = formatResetsIn(window, now);
151154
const where =
152155
account.environments.length > 0
@@ -181,6 +184,11 @@ function SegmentPopover({
181184
</div>
182185
<div className="flex flex-col gap-1 border-t border-border/60 pt-2.5">
183186
<Row label="Left">{remaining}%</Row>
187+
{evenPace !== null ? (
188+
<Row label="Even pace">
189+
{evenPace}% <span className="text-muted-foreground">· {paceGapLabel(window, now)}</span>
190+
</Row>
191+
) : null}
184192
{window.resetsAt ? (
185193
<Row label="Resets">
186194
{formatUpcomingTimestamp(window.resetsAt, timestampFormat, now)}
@@ -211,6 +219,26 @@ function SegmentPopover({
211219
);
212220
}
213221

222+
/**
223+
* Where even spending would have left the fill, as a tick above and below the
224+
* segment rather than a line across it: the segment's own label runs the full
225+
* width, and a mark outside the bar reads at a glance without competing with it.
226+
* The provider colour ties it to the fill it is measured against.
227+
*
228+
* The tick is shorter than the room its wrapper reserves, and the difference is
229+
* the gap that keeps it reading as a tick. Flush against the bar, the two run
230+
* together into one line.
231+
*/
232+
function EvenPaceTicks({ at, color }: { readonly at: number; readonly color: string }) {
233+
const tick = "absolute w-px -translate-x-1/2 h-1.5 @2xl/pool:h-2";
234+
return (
235+
<span aria-hidden className="pointer-events-none">
236+
<span className={cn(tick, "top-0")} style={{ left: `${at}%`, backgroundColor: color }} />
237+
<span className={cn(tick, "bottom-0")} style={{ left: `${at}%`, backgroundColor: color }} />
238+
</span>
239+
);
240+
}
241+
214242
/**
215243
* One account's share of one pooled window: the segment, its popover, and the
216244
* reset confirm. The confirm is a sibling of the popover, not a child: dialogs
@@ -234,66 +262,81 @@ function PoolSegment({
234262
}) {
235263
const [open, setOpen] = useState(false);
236264
const remaining = remainingPercent(window);
265+
const evenPace = evenPacePercent(window, now);
237266
const resetsIn = formatResetsIn(window, now);
238267
const credits = account.limits.resetCredits?.availableCount ?? 0;
239268
return (
240269
<Popover open={open} onOpenChange={setOpen}>
241-
<PopoverTrigger
242-
openOnHover
243-
render={
244-
<button
245-
type="button"
246-
style={{ gridColumn: index, gridRow: 1 }}
247-
aria-label={`${account.displayName ?? (account.email ? accountInitials(account.email) : account.driver)}: ${remaining}% left${resetsIn ? `, ${resetsIn}` : ""}${credits ? `, ${credits} reset ${credits === 1 ? "credit" : "credits"} banked` : ""}`}
248-
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-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background data-[popup-open]:ring-1 data-[popup-open]:ring-border @2xl/pool:h-8"
249-
/>
250-
}
270+
{/* The wrapper reserves the ticks' room and their gap; the segment clips its fill, so they
271+
cannot live inside it. Centring the segment keeps both gaps equal whatever height the
272+
grid row takes, and makes the button a flex item, which drops the baseline space an
273+
inline-level button would otherwise add under it. */}
274+
<div
275+
className="relative flex min-w-0 flex-col justify-center py-2.5 @2xl/pool:py-3"
276+
style={{ gridColumn: index, gridRow: 1 }}
251277
>
252-
{/* Translucent so the label reads over the fill for any provider colour and theme. */}
253-
<div
254-
aria-hidden
255-
className="absolute inset-y-0 left-0 rounded-md opacity-35"
256-
style={{ width: `${remaining}%`, backgroundColor: color }}
257-
/>
258-
{/* The spent share is hatched, not blank: it is what the countdown restores. */}
259-
{remaining < 100 && reset ? (
278+
<PopoverTrigger
279+
openOnHover
280+
render={
281+
<button
282+
type="button"
283+
aria-label={`${account.displayName ?? (account.email ? accountInitials(account.email) : account.driver)}: ${remaining}% left${evenPace === null ? "" : `, ${evenPace}% at even pace`}${resetsIn ? `, ${resetsIn}` : ""}${credits ? `, ${credits} reset ${credits === 1 ? "credit" : "credits"} banked` : ""}`}
284+
className="relative h-5 w-full min-w-0 shrink-0 cursor-pointer overflow-hidden rounded-md bg-muted text-start outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background data-[popup-open]:ring-1 data-[popup-open]:ring-border @2xl/pool:h-8"
285+
/>
286+
}
287+
>
288+
{/* Translucent so the label reads over the fill for any provider colour and theme. */}
260289
<div
261290
aria-hidden
262-
className="absolute inset-y-0 right-0 opacity-20"
263-
style={{
264-
width: `${100 - remaining}%`,
265-
backgroundImage: `repeating-linear-gradient(135deg, ${color} 0 1px, transparent 1px 5px)`,
266-
}}
291+
className="absolute inset-y-0 left-0 rounded-md opacity-35"
292+
style={{ width: `${remaining}%`, backgroundColor: color }}
267293
/>
268-
) : null}
269-
<span
270-
aria-hidden
271-
className="absolute inset-0 flex items-center justify-center text-[10px] leading-none font-semibold text-foreground/80 tabular-nums @2xl/pool:hidden"
272-
>
273-
{index}
274-
</span>
275-
<div className="relative hidden h-full min-w-0 items-center gap-1.5 px-2 text-xs @2xl/pool:flex">
276-
<AccountName account={account} className="min-w-0 truncate font-medium text-foreground" />
277-
<span className="shrink-0 font-semibold text-foreground tabular-nums">{remaining}%</span>
278-
{/* Countdown and badge get their own plate: fill and hatching run under them otherwise. */}
279-
<span className="ms-auto flex shrink-0 items-center gap-1.5 rounded-sm bg-background/85 px-1.5 py-0.5 text-[11px] text-foreground tabular-nums">
280-
{resetsIn?.replace("resets in ", "↻ ") ?? ""}
281-
{credits ? (
282-
<>
283-
{resetsIn ? (
284-
<span aria-hidden className="text-muted-foreground">
285-
·
286-
</span>
287-
) : null}
288-
<span aria-hidden className="inline-flex items-center gap-0.5 font-semibold">
289-
<TicketIcon className="size-3" aria-hidden />
290-
{credits}
291-
</span>
292-
</>
293-
) : null}
294+
{/* The spent share is hatched, not blank: it is what the countdown restores. */}
295+
{remaining < 100 && reset ? (
296+
<div
297+
aria-hidden
298+
className="absolute inset-y-0 right-0 opacity-20"
299+
style={{
300+
width: `${100 - remaining}%`,
301+
backgroundImage: `repeating-linear-gradient(135deg, ${color} 0 1px, transparent 1px 5px)`,
302+
}}
303+
/>
304+
) : null}
305+
<span
306+
aria-hidden
307+
className="absolute inset-0 flex items-center justify-center text-[10px] leading-none font-semibold text-foreground/80 tabular-nums @2xl/pool:hidden"
308+
>
309+
{index}
294310
</span>
295-
</div>
296-
</PopoverTrigger>
311+
<div className="relative hidden h-full min-w-0 items-center gap-1.5 px-2 text-xs @2xl/pool:flex">
312+
<AccountName
313+
account={account}
314+
className="min-w-0 truncate font-medium text-foreground"
315+
/>
316+
<span className="shrink-0 font-semibold text-foreground tabular-nums">
317+
{remaining}%
318+
</span>
319+
{/* Countdown and badge get their own plate: fill and hatching run under them otherwise. */}
320+
<span className="ms-auto flex shrink-0 items-center gap-1.5 rounded-sm bg-background/85 px-1.5 py-0.5 text-[11px] text-foreground tabular-nums">
321+
{resetsIn?.replace("resets in ", "↻ ") ?? ""}
322+
{credits ? (
323+
<>
324+
{resetsIn ? (
325+
<span aria-hidden className="text-muted-foreground">
326+
·
327+
</span>
328+
) : null}
329+
<span aria-hidden className="inline-flex items-center gap-0.5 font-semibold">
330+
<TicketIcon className="size-3" aria-hidden />
331+
{credits}
332+
</span>
333+
</>
334+
) : null}
335+
</span>
336+
</div>
337+
</PopoverTrigger>
338+
{evenPace !== null ? <EvenPaceTicks at={evenPace} color={color} /> : null}
339+
</div>
297340
<LegendRow account={account} window={window} color={color} now={now} index={index} />
298341
{account.redeem ? (
299342
<RedeemableSegmentPopup

‎packages/shared/src/usageLimits.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import {
2020
collectLimitSources,
2121
collectLimitsGroups,
2222
elapsedShare,
23+
evenPacePercent,
2324
formatResetsIn,
2425
limitsNotice,
26+
paceGapLabel,
2527
paceOf,
2628
providersWithLimits,
2729
remainingPercent,
@@ -63,9 +65,18 @@ describe("pace", () => {
6365
expect(paceOf({ ...window, usedPercent: 80 }, now)).toBe("ahead");
6466
});
6567

68+
it("marks where even spending would have left the quota and how far off it is", () => {
69+
expect(evenPacePercent(window, now)).toBe(40);
70+
expect(paceGapLabel(window, now)).toBe("20 pts of headroom");
71+
expect(paceGapLabel({ ...window, usedPercent: 60 }, now)).toBe("exactly on pace");
72+
expect(paceGapLabel({ ...window, usedPercent: 85 }, now)).toBe("25 pts ahead of pace");
73+
});
74+
6675
it("has no pace without a reset or a duration", () => {
6776
expect(paceOf({ ...window, resetsAt: undefined }, now)).toBeNull();
6877
expect(paceOf({ ...window, windowDurationMins: undefined }, now)).toBeNull();
78+
expect(evenPacePercent({ ...window, resetsAt: undefined }, now)).toBeNull();
79+
expect(paceGapLabel({ ...window, windowDurationMins: undefined }, now)).toBeNull();
6980
expect(formatResetsIn({ ...window, resetsAt: undefined }, now)).toBeNull();
7081
});
7182

0 commit comments

Comments
 (0)