@devries-cameron20 / t3code-1 / commits / 65de56ef90
Fork 0

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

refactor(web): move the /usage-limits panel state out of ChatView (#20296)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 17:39 UTC 65de56ef902be5f908a21fc786406541e11257fa
3 files changed +110 -88
apps/web/src/components/ChatView.tsx
+19 -88
@@ -34,13 +34,9 @@ import {
34 latestRootProviderFailure,
35 } from "@t3tools/shared/orchestrationV2ThreadError";
36 import type { UsageLimitSourceSnapshots } from "@t3tools/contracts";
37 -import {
38 - collectProviderUsageLimits,
39 - hasProviderUsageLimits,
40 - isUsageLimitsCommand,
41 -} from "@t3tools/shared/usageLimits";
37 +import { isUsageLimitsCommand } from "@t3tools/shared/usageLimits";
38 import { feedbackBannerItem } from "./chat/ComposerFeedback";
43 -import { usageLimitsBannerItem } from "./chat/ComposerUsageLimits";
39 +import { useUsageLimitsPanel } from "./chat/useUsageLimitsPanel";
40 import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
41 import * as Schema from "effect/Schema";
42 import {
@@ -2637,16 +2633,8 @@ export default function ChatView(props: ChatViewProps) {
2633 hasActionableProposedPlan: hasActionableProposedPlan(activeProposedPlan),
2634 hasComposerAttachments: composerHasAttachments,
2635 });
2640 - // The open /usage-limits panel for this thread, model and turn. Only the open
2641 - // moment is stored: the rows read live provider data, so a redeemed reset
2642 - // credit or refreshed probe shows through. Anything that spends quota closes
2643 - // it: a new turn from any source, or the agent resuming after an approval or
2644 - // answered question.
2645 - const [usageLimitsPanel, setUsageLimitsPanel] = useState<{
2646 - readonly key: string;
2647 - readonly threadKey: string;
2648 - readonly now: number;
2649 - } | null>(null);
2636 + // A pending approval or question is part of the key: once it is answered,
2637 + // from this client or any other, the agent resumes and spends quota.
2638 const usageLimitsKey =
2639 activeProviderInstanceId === null || (isServerThread && activeThread === undefined)
2640 ? null
@@ -2656,77 +2644,20 @@ export default function ChatView(props: ChatViewProps) {
2644 activeThread?.latestRun?.runId ?? "",
2645 activePendingApproval?.requestId ?? activePendingUserInput?.requestId ?? "",
2646 ].join(":");
2659 - if (
2660 - usageLimitsPanel !== null &&
2661 - usageLimitsKey !== null &&
2662 - usageLimitsPanel.key !== usageLimitsKey
2663 - ) {
2664 - setUsageLimitsPanel(null);
2665 - }
2666 - const usageLimitSources = serverConfig?.usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES;
2667 - const usageLimitsReport = useMemo(
2668 - () =>
2669 - usageLimitsPanel !== null &&
2670 - usageLimitsKey !== null &&
2671 - usageLimitsPanel.key === usageLimitsKey &&
2672 - activeProviderInstanceId !== null
2673 - ? collectProviderUsageLimits(
2674 - activeProviderInstanceId,
2675 - providerStatuses,
2676 - usageLimitSources,
2677 - usageLimitsPanel.now,
2678 - )
2679 - : null,
2680 - [
2681 - activeProviderInstanceId,
2682 - providerStatuses,
2683 - usageLimitSources,
2684 - usageLimitsKey,
2685 - usageLimitsPanel,
2686 - ],
2687 - );
2688 - const usageLimitsBanner = useMemo(
2689 - () =>
2690 - usageLimitsReport !== null && usageLimitsPanel !== null
2691 - ? usageLimitsBannerItem(
2692 - `usage-limits:${usageLimitsPanel.key}:${usageLimitsPanel.now}`,
2693 - usageLimitsReport,
2694 - environmentId,
2695 - () => setUsageLimitsPanel(null),
2696 - )
2697 - : null,
2698 - [environmentId, usageLimitsPanel, usageLimitsReport],
2699 - );
2700 - // T3 owns /usage-limits only where Limits has data for the selected provider;
2701 - // elsewhere the name stays the provider's own and is sent through untouched.
2702 - const usageLimitsOffered =
2703 - activeProviderStatus !== null &&
2704 - hasProviderUsageLimits(activeProviderStatus.driver, providerStatuses, usageLimitSources);
2705 - const openUsageLimits = () => {
2706 - const now = Date.now();
2707 - const report =
2708 - activeProviderInstanceId !== null && usageLimitsKey !== null
2709 - ? collectProviderUsageLimits(
2710 - activeProviderInstanceId,
2711 - providerStatuses,
2712 - usageLimitSources,
2713 - now,
2714 - )
2715 - : null;
2716 - if (report && usageLimitsKey !== null) {
2717 - setUsageLimitsPanel({ key: usageLimitsKey, threadKey: routeThreadKey, now });
2718 - return true;
2719 - }
2720 - setUsageLimitsPanel(null);
2721 - toastManager.add({ type: "info", title: "Usage limits are unavailable for this provider" });
2722 - return false;
2723 - };
2724 - const openUsageLimitsStable = useStableCallback(openUsageLimits);
2725 - // Responses can resolve after navigating away; only the originating thread's panel clears.
2726 - const clearUsageLimitsFor = (threadKey: string) =>
2727 - setUsageLimitsPanel((current) =>
2728 - current !== null && current.threadKey === threadKey ? null : current,
2729 - );
2647 + const {
2648 + banner: usageLimitsBanner,
2649 + offered: usageLimitsOffered,
2650 + open: openUsageLimits,
2651 + clearFor: clearUsageLimitsFor,
2652 + } = useUsageLimitsPanel({
2653 + environmentId,
2654 + threadKey: routeThreadKey,
2655 + panelKey: usageLimitsKey,
2656 + providerInstanceId: activeProviderInstanceId,
2657 + providerDriver: activeProviderStatus?.driver ?? null,
2658 + providers: providerStatuses,
2659 + sources: serverConfig?.usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES,
2660 + });
2661 const {
2662 beginLocalDispatch,
2663 resetLocalDispatch,
@@ -10036,7 +9967,7 @@ export default function ChatView(props: ChatViewProps) {
9967 usageLimitsOffered &&
9968 usageLimitsKey !== null &&
9969 !composerHasNonPromptContent
10039 - ? openUsageLimitsStable
9970 + ? openUsageLimits
9971 : undefined
9972 }
9973 environmentUnavailable={activeEnvironmentUnavailableState}
apps/web/src/components/chat/useUsageLimitsPanel.ts new
+90
@@ -0,0 +1,90 @@
1 +import type {
2 + EnvironmentId,
3 + ProviderInstanceId,
4 + ServerProvider,
5 + UsageLimitSourceSnapshots,
6 +} from "@t3tools/contracts";
7 +import { collectProviderUsageLimits, hasProviderUsageLimits } from "@t3tools/shared/usageLimits";
8 +import { useState } from "react";
9 +import { useStableCallback } from "../../hooks/useStableCallback";
10 +import { toastManager } from "../ui/toast";
11 +import { usageLimitsBannerItem } from "./ComposerUsageLimits";
12 +
13 +/**
14 + * The open /usage-limits panel for one thread, model and turn. Only the open
15 + * moment is stored: the rows read live provider data, so a redeemed reset
16 + * credit or refreshed probe shows through. Anything that spends quota closes
17 + * it: a new turn from any source, or the agent resuming after an approval or
18 + * answered question.
19 + *
20 + * `panelKey` names that thread, model and turn. It is null while the provider
21 + * list or the thread itself is unavailable, such as during a reconnect; the
22 + * panel then stays hidden rather than being dropped.
23 + */
24 +export function useUsageLimitsPanel(input: {
25 + readonly environmentId: EnvironmentId;
26 + readonly threadKey: string;
27 + readonly panelKey: string | null;
28 + readonly providerInstanceId: ProviderInstanceId | null;
29 + readonly providerDriver: ServerProvider["driver"] | null;
30 + readonly providers: readonly ServerProvider[];
31 + readonly sources: UsageLimitSourceSnapshots;
32 +}) {
33 + const {
34 + environmentId,
35 + threadKey,
36 + panelKey,
37 + providerInstanceId,
38 + providerDriver,
39 + providers,
40 + sources,
41 + } = input;
42 + const [panel, setPanel] = useState<{
43 + readonly key: string;
44 + readonly threadKey: string;
45 + readonly now: number;
46 + } | null>(null);
47 + // Drop the snapshot as soon as the thread or model changes so it cannot resurface stale.
48 + if (panel !== null && panelKey !== null && panel.key !== panelKey) {
49 + setPanel(null);
50 + }
51 + const report =
52 + panel !== null && panelKey !== null && panel.key === panelKey && providerInstanceId !== null
53 + ? collectProviderUsageLimits(providerInstanceId, providers, sources, panel.now)
54 + : null;
55 + const banner =
56 + report !== null && panel !== null
57 + ? // A fresh id per opening: the stack keeps the last dismissed id as "exiting".
58 + usageLimitsBannerItem(`usage-limits:${panel.key}:${panel.now}`, report, environmentId, () =>
59 + setPanel(null),
60 + )
61 + : null;
62 + // T3 owns /usage-limits only where Limits has data for the selected provider;
63 + // elsewhere the name stays the provider's own and is sent through untouched.
64 + const offered =
65 + providerDriver !== null && hasProviderUsageLimits(providerDriver, providers, sources);
66 +
67 + // Answered locally from the last Limits snapshot; the agent never sees it.
68 + const open = useStableCallback(() => {
69 + const now = Date.now();
70 + const nextReport =
71 + providerInstanceId !== null && panelKey !== null
72 + ? collectProviderUsageLimits(providerInstanceId, providers, sources, now)
73 + : null;
74 + if (nextReport && panelKey !== null) {
75 + setPanel({ key: panelKey, threadKey, now });
76 + return true;
77 + }
78 + setPanel(null);
79 + toastManager.add({ type: "info", title: "Usage limits are unavailable for this provider" });
80 + return false;
81 + });
82 + // Responses can resolve after navigating away; only the originating thread's panel clears.
83 + const clearFor = useStableCallback((originThreadKey: string) =>
84 + setPanel((current) =>
85 + current !== null && current.threadKey === originThreadKey ? null : current,
86 + ),
87 + );
88 +
89 + return { banner, offered, open, clearFor };
90 +}
vite.config.ts
+1
@@ -328,6 +328,7 @@ export default defineConfig({
328 files: [
329 "apps/web/src/components/PersistentThreadTerminals.tsx",
330 "apps/web/src/components/chat/usePendingThreadRequests.ts",
331 + "apps/web/src/components/chat/useUsageLimitsPanel.ts",
332 "apps/web/src/components/terminal/**",
333 ],
334 rules: {