@devries-cameron20 / t3code-1 / commits / 92a6b4307f
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): usage limits show one account popover at a time (#18076)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Rakshith Bhat committed Oct 20, 2026 at 00:36 UTC 92a6b4307fc6a29096a2e6a83a7a526caed4fa61
2 files changed +254 -89
apps/web/src/components/usage/UsageLimitsPooled.browser.test.tsx
+120
@@ -2,6 +2,7 @@ import {
2 EnvironmentId,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 + UsageLimitSourceId,
6 type ServerProvider,
7 } from "@t3tools/contracts";
8 import type { LimitPresentations } from "@t3tools/shared/usageLimits";
@@ -164,3 +165,122 @@ it("omits an empty reset plate but keeps countdowns and banked credits visible",
165 await expect.element(plate()).toBeVisible();
166 expect(plate()?.textContent).toBe("1");
167 });
168 +
169 +it("moves the one limit popover between windows on hover", async () => {
170 + const [environmentId, presentation] = [...presentations("2026-09-03T14:00:00Z", 1)][0]!;
171 + const serverConfig = presentation.serverConfig!;
172 + const provider = serverConfig.providers[0]!;
173 + const twoWindows: LimitPresentations = new Map([
174 + [
175 + environmentId,
176 + {
177 + ...presentation,
178 + serverConfig: {
179 + ...serverConfig,
180 + providers: [
181 + {
182 + ...provider,
183 + usageLimits: {
184 + ...provider.usageLimits!,
185 + windows: [
186 + ...provider.usageLimits!.windows,
187 + { id: "weekly", kind: "weekly", label: "Weekly", usedPercent: 32 },
188 + ],
189 + },
190 + },
191 + ],
192 + },
193 + },
194 + ],
195 + ]);
196 + // Room above the bars so the popover opens there instead of flipping over the next card.
197 + container.style.paddingTop = "20rem";
198 + await act(() => root.render(<UsageLimitsPooled presentations={twoWindows} now={0} />));
199 + const segment = (label: string) =>
200 + Array.from(document.querySelectorAll("button")).find((node) =>
201 + node.getAttribute("aria-label")?.startsWith(label),
202 + )!;
203 + // Popup contents, once the previous segment's details have animated away.
204 + const popups = () =>
205 + document.querySelector("[data-previous]")
206 + ? null
207 + : Array.from(
208 + document.querySelectorAll('[data-slot="popover-popup"]'),
209 + (node) => node.textContent,
210 + );
211 + const session = segment("Alpha: 60%");
212 + const weekly = segment("Alpha: 68%");
213 +
214 + await act(() => userEvent.click(session));
215 + await expect.poll(popups).toEqual([expect.stringContaining("Left60%")]);
216 +
217 + await act(() => userEvent.hover(weekly));
218 + await expect.poll(popups).toEqual([expect.stringContaining("Left68%")]);
219 +
220 + // Weekly's popover now sits over the middle of the Session bar; come back in at its edge.
221 + await act(() => userEvent.hover(session, { position: { x: 4, y: 4 } }));
222 + await expect.poll(popups).toEqual([expect.stringContaining("Left60%")]);
223 +
224 + await act(() => root.render(<UsageLimitsPooled presentations={new Map()} now={0} />));
225 + await expect.poll(popups).toEqual([]);
226 + await act(() => userEvent.hover(container, { position: { x: 4, y: 4 } }));
227 +
228 + await act(() => root.render(<UsageLimitsPooled presentations={twoWindows} now={0} />));
229 + await expect.poll(popups).toEqual([]);
230 + await act(() => userEvent.click(segment("Alpha: 60%")));
231 + await expect.poll(popups).toEqual([expect.stringContaining("Left60%")]);
232 +});
233 +
234 +it("closes an open source-account popover when reset availability replaces its trigger", async () => {
235 + const [environmentId, presentation] = [...presentations("2026-09-03T14:00:00Z", 1)][0]!;
236 + const provider = presentation.serverConfig!.providers[0]!;
237 + const source = (nextCreditId?: string): LimitPresentations =>
238 + new Map([
239 + [
240 + environmentId,
241 + {
242 + ...presentation,
243 + serverConfig: {
244 + providers: [],
245 + usageLimitSources: [
246 + {
247 + id: UsageLimitSourceId.make("hub"),
248 + kind: "cliproxy",
249 + label: "Hub",
250 + checkedAt: "2026-09-03T11:00:00Z",
251 + accounts: [
252 + {
253 + id: "Alpha",
254 + driver: provider.driver,
255 + usageLimits: {
256 + ...provider.usageLimits!,
257 + resetCredits: {
258 + availableCount: 1,
259 + ...(nextCreditId ? { nextCreditId } : {}),
260 + },
261 + },
262 + },
263 + ],
264 + },
265 + ],
266 + },
267 + },
268 + ],
269 + ]);
270 + const popup = () => document.querySelector('[data-slot="popover-popup"]');
271 + await act(() => root.render(<UsageLimitsPooled presentations={source("credit-1")} now={0} />));
272 + await click("Alpha: 60%");
273 + await expect.poll(() => popup()?.textContent).toContain("Use reset");
274 +
275 + await act(() => root.render(<UsageLimitsPooled presentations={source()} now={0} />));
276 + await expect.poll(popup).toBeNull();
277 + await click("Alpha: 60%");
278 + await expect.poll(() => popup()?.textContent).toContain("Left60%");
279 +
280 + await act(() => root.render(<UsageLimitsPooled presentations={source("credit-2")} now={0} />));
281 + await expect.poll(popup).toBeNull();
282 + await click("Alpha: 60%");
283 + await expect.poll(() => popup()?.textContent).toContain("Use reset");
284 + await click("Use reset");
285 + await expect.poll(() => document.querySelector('[role="alertdialog"]')).not.toBeNull();
286 +});
apps/web/src/components/usage/UsageLimitsPooled.tsx
+134 -89
@@ -15,7 +15,7 @@ import {
15 remainingPercent,
16 } from "@t3tools/shared/usageLimits";
17 import { AlertTriangleIcon, ExternalLinkIcon, TicketIcon } from "lucide-react";
18 -import { Fragment, type ReactNode, useState } from "react";
18 +import { createContext, Fragment, type ReactNode, use, useEffect, useState } from "react";
19
20 import { ensureLocalApi } from "../../localApi";
21 import { usePrimarySettings } from "../../hooks/useSettings";
@@ -27,7 +27,7 @@ import { RedactedSensitiveText } from "../settings/RedactedSensitiveText";
27 import { Button } from "../ui/button";
28 import { OpenAI } from "../Icons";
29 import { Alert, AlertTitle } from "../ui/alert";
30 -import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
30 +import { Popover, PopoverCreateHandle, PopoverPopup, PopoverTrigger } from "../ui/popover";
31 import {
32 PaceIcon,
33 ResetCreditDialog,
@@ -204,37 +204,134 @@ function SegmentPopover({
204 );
205 }
206
207 +/**
208 + * Every segment on the page shares one popover, so hovering or clicking another
209 + * segment moves it instead of opening a second one. The payload is the segment
210 + * it currently describes.
211 + */
212 +type SegmentPayload = Omit<Parameters<typeof SegmentPopover>[0], "now" | "onRedeem">;
213 +const SegmentPopoverContext = createContext<ReturnType<
214 + typeof PopoverCreateHandle<SegmentPayload>
215 +> | null>(null);
216 +
217 +function ActiveSegmentPopover({
218 + handle,
219 + payload,
220 + pools,
221 + now,
222 +}: {
223 + readonly handle: ReturnType<typeof PopoverCreateHandle<SegmentPayload>>;
224 + readonly payload: SegmentPayload;
225 + readonly pools: readonly LimitPool[];
226 + readonly now: number;
227 +}) {
228 + const available = pools.some((pool) =>
229 + displayLimitWindows(pool).some((window) =>
230 + window.members.some(
231 + (member) =>
232 + member.account.key === payload.account.key &&
233 + member.window.id === payload.window.id &&
234 + member.window.kind === payload.window.kind &&
235 + Boolean(member.account.redeem) === Boolean(payload.redeem),
236 + ),
237 + ),
238 + );
239 + useEffect(() => {
240 + if (!available) handle.close();
241 + }, [available, handle]);
242 + return (
243 + <PopoverPopup side="top" sideOffset={6}>
244 + <SegmentPopover
245 + {...payload}
246 + now={now}
247 + onRedeem={() => {
248 + handle.close();
249 + payload.redeem?.setConfirming(true);
250 + }}
251 + />
252 + </PopoverPopup>
253 + );
254 +}
255 +
256 /**
257 * The confirm is a sibling of the popover, not a child: dialogs stack under
258 * popovers, and the popover closes as the confirm opens.
259 */
260 function PoolSegment({
261 + account,
262 + ...props
263 +}: {
264 + readonly account: LimitAccount;
265 + readonly window: LimitPoolMember["window"];
266 + readonly reset: LimitPoolWindow["resets"][number] | undefined;
267 + readonly refillPercent: number;
268 + readonly color: string;
269 + readonly now: number;
270 + readonly index: number;
271 + readonly showAccountName: boolean;
272 +}) {
273 + return account.redeem ? (
274 + <RedeemableSegment account={account} redeemAt={account.redeem} {...props} />
275 + ) : (
276 + <SegmentTriggers account={account} redeem={null} {...props} />
277 + );
278 +}
279 +
280 +function RedeemableSegment({
281 + redeemAt,
282 + ...props
283 +}: Omit<Parameters<typeof SegmentTriggers>[0], "redeem"> & {
284 + readonly redeemAt: NonNullable<LimitAccount["redeem"]>;
285 +}) {
286 + const redeem = useResetCredit(redeemAt.environmentId, redeemAt.input);
287 + return (
288 + <>
289 + <SegmentTriggers redeem={redeem} {...props} />
290 + <ResetCreditDialog
291 + open={redeem.confirming}
292 + onOpenChange={redeem.setConfirming}
293 + onConfirm={() => void redeem.redeem()}
294 + disabled={!redeem.canManageProviders}
295 + />
296 + {/* The popover closed before the confirm, so progress and the outcome need a home outside it. */}
297 + {redeem.busy || redeem.status ? (
298 + <span role="status" className="col-span-full text-xs text-muted-foreground">
299 + <AccountName account={props.account} className="font-medium text-foreground" />{" "}
300 + {redeem.busy ? "Using reset credit…" : redeem.status}
301 + </span>
302 + ) : null}
303 + </>
304 + );
305 +}
306 +
307 +function SegmentTriggers({
308 account,
309 window,
310 reset,
311 + redeem,
312 refillPercent,
313 color,
314 now,
315 index,
316 showAccountName,
220 -}: {
221 - readonly account: LimitAccount;
222 - readonly window: LimitPoolMember["window"];
223 - readonly reset: LimitPoolWindow["resets"][number] | undefined;
317 +}: SegmentPayload & {
318 readonly refillPercent: number;
319 readonly color: string;
320 readonly now: number;
321 readonly index: number;
322 readonly showAccountName: boolean;
323 }) {
230 - const [open, setOpen] = useState(false);
324 + const popover = use(SegmentPopoverContext) ?? undefined;
325 + const payload: SegmentPayload = { account, window, reset, redeem };
326 const remaining = remainingPercent(window);
327 const timeLeft = paceRemainingPercent(window, now);
328 const resetsIn = formatResetsIn(window, now);
329 const credits = account.limits.resetCredits?.availableCount ?? 0;
330 return (
236 - <Popover open={open} onOpenChange={setOpen}>
331 + <>
332 <PopoverTrigger
333 + handle={popover}
334 + payload={payload}
335 openOnHover
336 render={
337 <button
@@ -303,50 +400,31 @@ function PoolSegment({
400 ) : null}
401 </div>
402 </PopoverTrigger>
306 - <LegendRow account={account} window={window} color={color} now={now} index={index} />
307 - {account.redeem ? (
308 - <RedeemableSegmentPopup
309 - account={account}
310 - window={window}
311 - reset={reset}
312 - now={now}
313 - redeemAt={account.redeem}
314 - closePopover={() => setOpen(false)}
315 - />
316 - ) : (
317 - <PopoverPopup side="top" sideOffset={6}>
318 - <SegmentPopover
319 - account={account}
320 - window={window}
321 - reset={reset}
322 - now={now}
323 - redeem={null}
324 - onRedeem={() => {}}
325 - />
326 - </PopoverPopup>
327 - )}
328 - </Popover>
403 + <LegendRow payload={payload} color={color} now={now} index={index} />
404 + </>
405 );
406 }
407
408 function LegendRow({
333 - account,
334 - window,
409 + payload,
410 color,
411 now,
412 index,
413 }: {
339 - readonly account: LimitAccount;
340 - readonly window: LimitPoolMember["window"];
414 + readonly payload: SegmentPayload;
415 readonly color: string;
416 readonly now: number;
417 readonly index: number;
418 }) {
419 + const popover = use(SegmentPopoverContext) ?? undefined;
420 + const { account, window } = payload;
421 const remaining = remainingPercent(window);
422 const resetsIn = formatResetsIn(window, now);
423 const credits = account.limits.resetCredits?.availableCount ?? 0;
424 return (
425 <PopoverTrigger
426 + handle={popover}
427 + payload={payload}
428 style={{ gridColumn: "1 / -1", gridRow: index + 1 }}
429 render={<Button variant="ghost" size="compact" />}
430 className="min-w-0 @2xl/pool:hidden"
@@ -384,54 +462,6 @@ function LegendRow({
462 );
463 }
464
387 -function RedeemableSegmentPopup({
388 - account,
389 - window,
390 - reset,
391 - now,
392 - redeemAt,
393 - closePopover,
394 -}: {
395 - readonly account: LimitAccount;
396 - readonly window: LimitPoolMember["window"];
397 - readonly reset: LimitPoolWindow["resets"][number] | undefined;
398 - readonly now: number;
399 - readonly redeemAt: NonNullable<LimitAccount["redeem"]>;
400 - readonly closePopover: () => void;
401 -}) {
402 - const redeem = useResetCredit(redeemAt.environmentId, redeemAt.input);
403 - return (
404 - <>
405 - <PopoverPopup side="top" sideOffset={6}>
406 - <SegmentPopover
407 - account={account}
408 - window={window}
409 - reset={reset}
410 - now={now}
411 - redeem={redeem}
412 - onRedeem={() => {
413 - closePopover();
414 - redeem.setConfirming(true);
415 - }}
416 - />
417 - </PopoverPopup>
418 - <ResetCreditDialog
419 - open={redeem.confirming}
420 - onOpenChange={redeem.setConfirming}
421 - onConfirm={() => void redeem.redeem()}
422 - disabled={!redeem.canManageProviders}
423 - />
424 - {/* The popover closed before the confirm, so progress and the outcome need a home outside it. */}
425 - {redeem.busy || redeem.status ? (
426 - <span role="status" className="col-span-full text-xs text-muted-foreground">
427 - <AccountName account={account} className="font-medium text-foreground" />{" "}
428 - {redeem.busy ? "Using reset credit…" : redeem.status}
429 - </span>
430 - ) : null}
431 - </>
432 - );
433 -}
434 -
465 function PoolBar({
466 pool,
467 color,
@@ -551,6 +581,7 @@ export function UsageLimitsPooled({
581 readonly now: number;
582 readonly cursorPrompt?: ReactNode;
583 }) {
584 + const [segmentPopover] = useState(() => PopoverCreateHandle<SegmentPayload>());
585 const pools = collectLimitPools(collectLimitAccounts(presentations), now);
586 const notices = collectLimitNotices(presentations);
587 const externalLinks = collectExternalUsageLinks(presentations);
@@ -566,12 +597,26 @@ export function UsageLimitsPooled({
597 No provider on the selected environments reports subscription limits.
598 </p>
599 ) : null}
569 - {pools.map((pool, index) => (
570 - <Fragment key={pool.driver}>
571 - {index === cursorPromptAt ? cursorPrompt : null}
572 - <PoolSection pool={pool} now={now} />
573 - </Fragment>
574 - ))}
600 + <SegmentPopoverContext value={segmentPopover}>
601 + {pools.map((pool, index) => (
602 + <Fragment key={pool.driver}>
603 + {index === cursorPromptAt ? cursorPrompt : null}
604 + <PoolSection pool={pool} now={now} />
605 + </Fragment>
606 + ))}
607 + </SegmentPopoverContext>
608 + <Popover handle={segmentPopover}>
609 + {({ payload }) =>
610 + payload ? (
611 + <ActiveSegmentPopover
612 + handle={segmentPopover}
613 + payload={payload}
614 + pools={pools}
615 + now={now}
616 + />
617 + ) : null
618 + }
619 + </Popover>
620 {cursorPromptAt === pools.length ? cursorPrompt : null}
621 {externalLinks.map((link) => (
622 <section