From e951d20f0b95a26a4cb10a89f70ec9abf51ffdd0 Mon Sep 17 00:00:00 2001 From: Wesley Smits Date: Sun, 22 Nov 2026 14:18:24 +0300 Subject: [PATCH 1/7] feat(web): customize the usage limits layout --- apps/web/src/components/AppRouteModal.tsx | 18 +- apps/web/src/components/usage/UsageLimits.tsx | 14 +- .../usage/UsageLimitsPooled.browser.test.tsx | 93 +++++ .../components/usage/UsageLimitsPooled.tsx | 334 ++++++++++++++++-- apps/web/src/components/usage/UsagePage.tsx | 109 ++++-- .../usage/usageLimitsLayout.test.ts | 18 + .../src/components/usage/usageLimitsLayout.ts | 52 +++ .../usage/usagePagePreferences.test.ts | 15 +- .../components/usage/usagePagePreferences.ts | 2 + apps/web/src/i18n/translations/de.json | 13 +- apps/web/src/i18n/translations/en.json | 13 +- apps/web/src/i18n/translations/es.json | 13 +- apps/web/src/i18n/translations/fr.json | 13 +- apps/web/src/i18n/translations/ja.json | 13 +- apps/web/src/i18n/translations/ko.json | 13 +- apps/web/src/i18n/translations/pt-BR.json | 13 +- apps/web/src/i18n/translations/ru.json | 13 +- apps/web/src/i18n/translations/sv.json | 13 +- apps/web/src/i18n/translations/zh.json | 13 +- docs/user/usage.md | 4 + 20 files changed, 702 insertions(+), 87 deletions(-) create mode 100644 apps/web/src/components/usage/usageLimitsLayout.test.ts create mode 100644 apps/web/src/components/usage/usageLimitsLayout.ts diff --git a/apps/web/src/components/AppRouteModal.tsx b/apps/web/src/components/AppRouteModal.tsx index 1414b198afdc..18448d871cf1 100644 --- a/apps/web/src/components/AppRouteModal.tsx +++ b/apps/web/src/components/AppRouteModal.tsx @@ -12,12 +12,24 @@ import { Button } from "./ui/button"; import { DialogBackdrop } from "./ui/dialog"; import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; +function escapeOutsidePopup(target: EventTarget | null, popup: HTMLElement | null): boolean { + return target !== document.body && target instanceof Node && !popup?.contains(target); +} + /** * Large modal for the Settings and Usage routes. Esc, the close button, or a press on the * backdrop returns to the page that opened it. Keyboard guards that skip dialogs exempt * `[data-app-modal]`, because these pages own their shortcuts. */ -export function AppRouteModal({ label, children }: { label: string; children: ReactNode }) { +export function AppRouteModal({ + label, + children, + onEscape, +}: { + label: string; + children: ReactNode; + onEscape?: () => boolean; +}) { const closeAppModal = useCloseAppModal(); const [open, setOpen] = useState(true); // Text selection that ends on the backdrop is not a dismissal. @@ -47,9 +59,7 @@ export function AppRouteModal({ label, children }: { label: string; children: Re const target = eventDetails.event.target; if ( eventDetails.reason === "escape-key" && - target !== document.body && - target instanceof Node && - !popupRef.current?.contains(target) + (escapeOutsidePopup(target, popupRef.current) || onEscape?.()) ) { return eventDetails.cancel(); } diff --git a/apps/web/src/components/usage/UsageLimits.tsx b/apps/web/src/components/usage/UsageLimits.tsx index a36b220ee3ef..779f2634160b 100644 --- a/apps/web/src/components/usage/UsageLimits.tsx +++ b/apps/web/src/components/usage/UsageLimits.tsx @@ -40,6 +40,7 @@ import { import { Button } from "../ui/button"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { UsageLimitsPooled } from "./UsageLimitsPooled"; +import type { UsageLimitsLayout } from "./usageLimitsLayout"; import { formatResetsIn, resetCreditChoices } from "@t3tools/shared/usageLimitsCopy"; import { PROVIDER_PRESENTATION } from "./usageProviders"; @@ -393,10 +394,14 @@ export function UsageLimitsSection({ selectedEnvironmentIds, hiddenProviders, now, + layout, + onLayoutChange, }: { readonly selectedEnvironmentIds: ReadonlySet | null; readonly hiddenProviders: ReadonlySet; readonly now: number; + readonly layout: UsageLimitsLayout; + readonly onLayoutChange?: ((layout: UsageLimitsLayout) => void) | undefined; }) { const presentations = useAtomValue(environmentPresentations.presentationsWithProvidersAtom); const hiddenDrivers = new Set( @@ -428,5 +433,12 @@ export function UsageLimitsSection({ ]; }), ); - return ; + return ( + + ); } diff --git a/apps/web/src/components/usage/UsageLimitsPooled.browser.test.tsx b/apps/web/src/components/usage/UsageLimitsPooled.browser.test.tsx index ab114fbbd6f2..4656b8471816 100644 --- a/apps/web/src/components/usage/UsageLimitsPooled.browser.test.tsx +++ b/apps/web/src/components/usage/UsageLimitsPooled.browser.test.tsx @@ -5,10 +5,13 @@ import { type ServerProvider, } from "@t3tools/contracts"; import { CHATGPT_USAGE_URL } from "@t3tools/shared/usageLimits"; +import { useState } from "react"; import { describe, expect, it } from "vite-plus/test"; +import { userEvent } from "vite-plus/test/browser"; import { render } from "../../../test/render"; import { UsageLimitsPooled } from "./UsageLimitsPooled"; +import { DEFAULT_USAGE_LIMITS_LAYOUT } from "./usageLimitsLayout"; const checkedAt = "2026-10-25T16:00:00.000Z"; @@ -63,3 +66,93 @@ describe("UsageLimitsPooled ChatGPT usage card", () => { .toBeVisible(); }); }); + +function quotaProvider( + driver: string, + windows: NonNullable["windows"], +): ServerProvider { + const provider = managedChatGpt(driver, driver); + return { + ...provider, + driver: ProviderDriverKind.make(driver), + usageLimits: { checkedAt, windows }, + }; +} + +function LayoutExample() { + const [layout, setLayout] = useState(DEFAULT_USAGE_LIMITS_LAYOUT); + const [editing, setEditing] = useState(false); + const presentations = new Map([ + [ + EnvironmentId.make("desk"), + { + entry: { target: { label: "Desk" } }, + serverConfig: { + providers: [ + quotaProvider("codex", [ + { id: "weekly", kind: "weekly", label: "Weekly", usedPercent: 76 }, + ]), + quotaProvider("antigravity", [ + { id: "gemini-5h", kind: "session", label: "Gemini 5-hour", usedPercent: 3 }, + { id: "gemini-weekly", kind: "weekly", label: "Gemini Weekly", usedPercent: 50 }, + { + id: "gemini-argon-wtu-weekly", + kind: "weekly", + label: "Gemini 4 Argon Weekly", + usedPercent: 0, + }, + ]), + ], + }, + }, + ], + ]); + return ( + <> + + + + ); +} + +it("reorders pools and windows with the keyboard and restores individual hidden windows", async () => { + await using screen = await render(); + await screen.getByRole("button", { name: "Edit layout" }).click(); + + const moveProvider = screen.getByRole("button", { name: "Move Antigravity up" }); + moveProvider.element().focus(); + await userEvent.keyboard("{Enter}"); + await expect.element(moveProvider).toHaveFocus(); + expect( + [...screen.container.querySelectorAll("h2")].map((heading) => heading.textContent), + ).toEqual(["Antigravity", "Codex"]); + + const moveWindow = screen.getByRole("button", { name: "Move Gemini Weekly up" }); + await moveWindow.click(); + await expect.element(moveWindow).toHaveFocus(); + + await screen.getByRole("button", { name: "Hide Gemini 4 Argon Weekly" }).click(); + await expect + .element(screen.getByRole("button", { name: "Show Gemini 4 Argon Weekly" })) + .toHaveFocus(); + await screen.getByRole("button", { name: "Finish editing" }).click(); + await expect + .element(screen.getByText("Gemini 4 Argon Weekly", { exact: true })) + .not.toBeInTheDocument(); + await expect.element(screen.getByText("Gemini 5-hour", { exact: true })).toBeVisible(); + const weekly = screen.getByText("Gemini Weekly", { exact: true }).element(); + const session = screen.getByText("Gemini 5-hour", { exact: true }).element(); + expect(weekly.compareDocumentPosition(session) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy(); + + await screen.getByRole("button", { name: "Edit layout" }).click(); + await screen.getByRole("button", { name: "Show Gemini 4 Argon Weekly" }).click(); + await screen.getByRole("button", { name: "Finish editing" }).click(); + await expect.element(screen.getByText("Gemini 4 Argon Weekly", { exact: true })).toBeVisible(); +}); diff --git a/apps/web/src/components/usage/UsageLimitsPooled.tsx b/apps/web/src/components/usage/UsageLimitsPooled.tsx index c3932252691e..5c09f631d2b0 100644 --- a/apps/web/src/components/usage/UsageLimitsPooled.tsx +++ b/apps/web/src/components/usage/UsageLimitsPooled.tsx @@ -14,10 +14,17 @@ import { import { T, useGT, useMessages } from "gt-react"; import type { Popover as PopoverPrimitive } from "@base-ui/react/popover"; import { normalizeProviderAccentColor } from "@t3tools/client-runtime/state/provider-instance-display"; -import { AlertTriangleIcon, ExternalLinkIcon, TicketIcon } from "lucide-react"; +import { + AlertTriangleIcon, + ArrowDownIcon, + ArrowUpIcon, + ExternalLinkIcon, + EyeIcon, + EyeOffIcon, + TicketIcon, +} from "lucide-react"; import { createContext, - Fragment, type ReactNode, type RefObject, use, @@ -25,6 +32,7 @@ import { useRef, useState, } from "react"; +import { flushSync } from "react-dom"; import { ensureLocalApi } from "../../localApi"; import { usePrimarySettings } from "../../hooks/useSettings"; @@ -45,6 +53,14 @@ import { formatResetsIn, resetCreditChoices, } from "@t3tools/shared/usageLimitsCopy"; +import { + DEFAULT_USAGE_LIMITS_LAYOUT, + limitWindowKey, + moveLimitItem, + orderLimitItems, + type LimitMoveDirection, + type UsageLimitsLayout, +} from "./usageLimitsLayout"; function accountInitials(email: string): string { const [local = "", domain = ""] = email.split("@"); @@ -546,15 +562,17 @@ function PoolBar({ pool, color, now, + className, }: { readonly pool: LimitPoolWindow; readonly color: string; readonly now: number; + readonly className?: string; }) { const restores = new Map(pool.resets.map((reset) => [reset.member.account.key, reset])); const gt = useGT(); return ( -
+
reset.restoresPercent > 0); + return ( +
+ {label !== null ? ( + {label ?? pool.label} + ) : null} + {hidden ? ( + + Hidden + + ) : null} + + + {pool.remainingPercent}% + + + + left + + {pool.pace ? : null} + + + {nextRefill && pool.columns.length > 1 ? ( + + ↻ +{nextRefill.restoresPercent}% + + ) : null} +
+ ); +} + function PoolWindowCard({ pool, color, now, label, description, + controls, + hidden = false, }: { readonly pool: LimitPoolWindow; readonly color: string; readonly now: number; readonly label?: string | null | undefined; readonly description?: string | undefined; + readonly controls?: ReactNode; + readonly hidden?: boolean; }) { - const nextRefill = pool.resets.find((reset) => reset.restoresPercent > 0); + const dimmed = hidden && "opacity-60"; return ( -
-
- {label !== null ? ( - {label ?? pool.label} - ) : null} - - - {pool.remainingPercent}% - - - - left - - {pool.pace ? : null} - - - {nextRefill && pool.columns.length > 1 ? ( - - ↻ +{nextRefill.restoresPercent}% - - ) : null} -
- + ); } -function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: number }) { +type MoveLimit = (direction: LimitMoveDirection, button: HTMLButtonElement) => void; + +function MoveButtons({ + label, + position, + count, + onMove, +}: { + readonly label: string; + readonly position: number; + readonly count: number; + readonly onMove: MoveLimit; +}) { + const gt = useGT(); + const up = gt("Move {label} up", { label }); + const down = gt("Move {label} down", { label }); + return ( + <> + + + + ); +} + +type LayoutSectionEditor = { + readonly providerPosition: number; + readonly providerCount: number; + readonly hiddenWindows: ReadonlySet; + readonly moveProvider: MoveLimit; + readonly moveWindow: (index: number, ...move: Parameters) => void; + readonly toggleWindow: (key: string) => void; +}; + +function PoolSection({ + pool, + windows, + now, + editor, +}: { + readonly pool: LimitPool; + readonly windows: readonly LimitPoolWindow[]; + readonly now: number; + readonly editor?: LayoutSectionEditor | undefined; +}) { const color = barColor(pool.driver); const label = providerClients.get(pool.driver)?.label ?? String(pool.driver); - const windows = displayLimitWindows(pool); const m = useMessages(); return (
@@ -652,18 +788,49 @@ function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: nu iconClassName="size-4 text-foreground/80" /> {label} + {editor ? ( + + + + ) : null} - {windows.map((window) => { + {windows.map((window, index) => { const details = pool.driver === "cursor" ? cursorUsageWindowDetails(window.id, pool, m) : undefined; + const key = limitWindowKey(pool.driver, window); + const name = details?.label ?? window.label; + const hidden = editor?.hiddenWindows.has(key) ?? false; return (