The best way to control Claude Code, Codex and more from any computer or phone
perf(web): the usage provider chart compiles without manual memoization (#20695)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 21:49 UTC
b190a2fec24aa7b8bc5b4c1e41e946e2ddf8a561
1 file changed
+37
-29
apps/web/src/components/usage/UsageProviderChart.tsx
+37
-29
@@ -1,6 +1,6 @@
1
import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon";
2
import type { UsageProviderKind } from "@t3tools/contracts";
3
-import { useCallback, useLayoutEffect, useMemo, useRef, useState } from "react";
3
+import { useLayoutEffect, useRef, useState } from "react";
4
5
import type { DailyTotals, HourlyTotals } from "@t3tools/shared/usageMerge";
6
import {
@@ -223,6 +223,21 @@ function buildChart(
223
return { columns, scale, stepX, paths: paths.toSorted((a, b) => b.total - a.total) };
224
}
225
226
+// Rebuilding the chart walks every period and provider; keep it cached across hover renders.
227
+function useChart(
228
+ periods: readonly string[],
229
+ totals: readonly DailyTotals[] | readonly HourlyTotals[],
230
+ metric: UsageChartMetric,
231
+ providers: readonly UsageProviderKind[],
232
+ loadingProviders: ReadonlySet<UsageProviderKind>,
233
+) {
234
+ "use memo";
235
+ const byPeriod = new Map<string, DailyTotals | HourlyTotals>(
236
+ totals.map((entry) => ["hourStart" in entry ? entry.hourStart : entry.day, entry]),
237
+ );
238
+ return buildChart(periods, byPeriod, metric, providers, loadingProviders);
239
+}
240
+
241
export function UsageProviderChart({
242
providers,
243
loadingProviders = NONE_LOADING,
@@ -236,29 +251,25 @@ export function UsageProviderChart({
251
timeZone,
252
}: UsageProviderChartProps) {
253
const periods = resolution === "hour" ? hours : days;
239
- const byPeriod = useMemo(
240
- () =>
241
- resolution === "hour"
242
- ? new Map(hourly.map((entry) => [entry.hourStart, entry]))
243
- : new Map(daily.map((entry) => [entry.day, entry])),
244
- [daily, hourly, resolution],
254
+ const { columns, scale, paths, stepX } = useChart(
255
+ periods,
256
+ resolution === "hour" ? hourly : daily,
257
+ metric,
258
+ providers,
259
+ loadingProviders,
260
);
261
const [hoverIndex, setHoverIndex] = useState<number | null>(null);
262
const plotRef = useRef<HTMLDivElement | null>(null);
263
const tooltipRef = useRef<HTMLDivElement | null>(null);
264
const hoverPositionRef = useRef<{ x: number; y: number } | null>(null);
265
251
- const { columns, scale, paths, stepX } = useMemo(
252
- () => buildChart(periods, byPeriod, metric, providers, loadingProviders),
253
- [byPeriod, loadingProviders, metric, periods, providers],
254
- );
266
const toY = (value: number) => valueToY(value, scale.max);
267
const seriesClassName = (loading: boolean) =>
268
cn("transition-opacity", loading && "opacity-40 delay-150");
269
270
const format = metric === "tokens" ? formatTokens : formatUsd;
271
261
- const positionTooltip = useCallback(() => {
272
+ const positionTooltip = () => {
273
const plot = plotRef.current;
274
const tooltip = tooltipRef.current;
275
const hoverPosition = hoverPositionRef.current;
@@ -281,7 +292,7 @@ export function UsageProviderChart({
292
const top = Math.min(Math.max(0, preferredTop), Math.max(0, plotHeight - tooltipHeight));
293
plot.style.setProperty("--usage-tooltip-left", `${left}px`);
294
plot.style.setProperty("--usage-tooltip-top", `${top}px`);
284
- }, []);
295
+ };
296
297
useLayoutEffect(() => {
298
if (hoverIndex === null) return;
@@ -294,22 +305,19 @@ export function UsageProviderChart({
305
return observeResize([plot, tooltip], positionTooltip);
306
}, [hoverIndex, positionTooltip]);
307
297
- const handleMove = useCallback(
298
- (event: React.MouseEvent<HTMLDivElement>) => {
299
- const plot = plotRef.current;
300
- if (plot === null || periods.length === 0) return;
301
- const bounds = plot.getBoundingClientRect();
302
- if (bounds.width === 0) return;
303
- const localX = Math.min(bounds.width, Math.max(0, event.clientX - bounds.left));
304
- const localY = Math.min(bounds.height, Math.max(0, event.clientY - bounds.top));
305
- const fraction = localX / bounds.width;
306
- const index = Math.round(fraction * (periods.length - 1));
307
- hoverPositionRef.current = { x: localX, y: localY };
308
- positionTooltip();
309
- setHoverIndex(Math.min(periods.length - 1, Math.max(0, index)));
310
- },
311
- [periods.length, positionTooltip],
312
- );
308
+ const handleMove = (event: React.MouseEvent<HTMLDivElement>) => {
309
+ const plot = plotRef.current;
310
+ if (plot === null || periods.length === 0) return;
311
+ const bounds = plot.getBoundingClientRect();
312
+ if (bounds.width === 0) return;
313
+ const localX = Math.min(bounds.width, Math.max(0, event.clientX - bounds.left));
314
+ const localY = Math.min(bounds.height, Math.max(0, event.clientY - bounds.top));
315
+ const fraction = localX / bounds.width;
316
+ const index = Math.round(fraction * (periods.length - 1));
317
+ hoverPositionRef.current = { x: localX, y: localY };
318
+ positionTooltip();
319
+ setHoverIndex(Math.min(periods.length - 1, Math.max(0, index)));
320
+ };
321
322
const hoveredPeriod = hoverIndex === null ? undefined : periods[hoverIndex];
323
const hoveredColumn = hoverIndex === null ? undefined : columns[hoverIndex];