@devries-cameron20 / t3code Mirror · read-only / commits / 0241bbfc22
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 08:00 UTC

perf(web): the usage state hook compiles without manual memoization (#20718)

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

Julius Marminge committed Oct 16, 2026 at 21:46 UTC 0241bbfc22cdde08d482a97eb6ef079f20749bf6
1 file changed +55 -50
apps/web/src/state/usage.ts
+55 -50
@@ -12,7 +12,7 @@ import { needsCursorKeychainAccess, refreshUsage } from "@t3tools/client-runtime
12 import { resolveUsageAccess } from "@t3tools/client-runtime/state/usage-access";
13 import * as Option from "effect/Option";
14 import { AsyncResult, Atom } from "effect/reactivity";
15 -import { useCallback, useMemo, useState } from "react";
15 +import { useState } from "react";
16
17 import { mergeUsage, type EnvironmentUsage, type MergedUsage } from "@t3tools/shared/usageMerge";
18 import { appAtomRegistry } from "../rpc/atomRegistry";
@@ -132,64 +132,69 @@ function withoutProviders(
132 );
133 }
134
135 +function deriveSelection(
136 + environments: readonly EnvironmentUsageStatus[],
137 + selectedEnvironmentIds: ReadonlySet<EnvironmentId> | null,
138 + hiddenProviders: ReadonlySet<UsageProviderKind>,
139 +) {
140 + const selectedEnvironments =
141 + selectedEnvironmentIds === null
142 + ? environments
143 + : environments.filter((environment) => selectedEnvironmentIds.has(environment.environmentId));
144 + return {
145 + environments,
146 + selectedEnvironmentIds,
147 + hiddenProviders,
148 + selectedEnvironments,
149 + merged: mergeAnsweredUsage(withoutProviders(selectedEnvironments, hiddenProviders)),
150 + };
151 +}
152 +
153 export function useUsage(
154 input: UsageSummaryInput,
155 selectedEnvironmentIds: ReadonlySet<EnvironmentId> | null = null,
156 hiddenProviders: ReadonlySet<UsageProviderKind> = NO_HIDDEN_PROVIDERS,
157 ): UsageView {
140 - const windowKey = useMemo(
141 - () =>
142 - JSON.stringify({
143 - sinceDay: input.sinceDay,
144 - untilDay: input.untilDay,
145 - timeZone: input.timeZone,
146 - resolution: input.resolution,
147 - sinceTime: input.sinceTime,
148 - untilTime: input.untilTime,
149 - }),
150 - [
151 - input.sinceDay,
152 - input.untilDay,
153 - input.timeZone,
154 - input.resolution,
155 - input.sinceTime,
156 - input.untilTime,
157 - ],
158 - );
158 + const windowKey = JSON.stringify({
159 + sinceDay: input.sinceDay,
160 + untilDay: input.untilDay,
161 + timeZone: input.timeZone,
162 + resolution: input.resolution,
163 + sinceTime: input.sinceTime,
164 + untilTime: input.untilTime,
165 + });
166 const atom = usageByWindowAtom(windowKey);
167 const environments = useAtomValue(atom);
161 - const selectedEnvironments = useMemo(
162 - () =>
163 - selectedEnvironmentIds === null
164 - ? environments
165 - : environments.filter((environment) =>
166 - selectedEnvironmentIds.has(environment.environmentId),
167 - ),
168 - [environments, selectedEnvironmentIds],
169 - );
170 -
171 - const refresh = useCallback(
172 - (nextInput?: UsageSummaryInput) =>
173 - refreshUsage({
174 - registry: appAtomRegistry,
175 - server: serverEnvironment,
176 - presentations: environmentPresentations,
177 - environmentIds: selectedEnvironments
178 - .filter(
179 - (environment) =>
180 - environment.canReadDiagnostics &&
181 - readEnvironmentScope(environment.environmentId, AuthDiagnosticsReadScope),
182 - )
183 - .map(({ environmentId }) => environmentId),
184 - input: nextInput ?? (JSON.parse(windowKey) as UsageSummaryInput),
185 - }),
186 - [selectedEnvironments, windowKey],
168 + // The render-time sync below compares merged by identity, so the derivation is
169 + // held in state keyed on its inputs rather than rebuilt every render.
170 + const [derived, setDerived] = useState(() =>
171 + deriveSelection(environments, selectedEnvironmentIds, hiddenProviders),
172 );
173 + let current = derived;
174 + if (
175 + derived.environments !== environments ||
176 + derived.selectedEnvironmentIds !== selectedEnvironmentIds ||
177 + derived.hiddenProviders !== hiddenProviders
178 + ) {
179 + current = deriveSelection(environments, selectedEnvironmentIds, hiddenProviders);
180 + setDerived(current);
181 + }
182 + const { selectedEnvironments, merged } = current;
183
189 - const merged = useMemo(
190 - () => mergeAnsweredUsage(withoutProviders(selectedEnvironments, hiddenProviders)),
191 - [selectedEnvironments, hiddenProviders],
192 - );
184 + const refresh = (nextInput?: UsageSummaryInput) =>
185 + refreshUsage({
186 + registry: appAtomRegistry,
187 + server: serverEnvironment,
188 + presentations: environmentPresentations,
189 + environmentIds: selectedEnvironments
190 + .filter(
191 + (environment) =>
192 + environment.canReadDiagnostics &&
193 + readEnvironmentScope(environment.environmentId, AuthDiagnosticsReadScope),
194 + )
195 + .map(({ environmentId }) => environmentId),
196 + input: nextInput ?? (JSON.parse(windowKey) as UsageSummaryInput),
197 + });
198
199 const answeredCount = selectedEnvironments.filter(
200 (environment) => environment.summary !== null,