Skip to content

Commit 80d08da

Browse files
bmdavis419claude
andcommitted
fix(web): a window that fails everywhere keeps the last usage on screen
useUsage saved a snapshot whenever nothing was pending, so a window where every selected environment failed replaced the kept usage with an empty one and showed zero totals. It now saves only answered usage, and the page mutes kept usage whenever it belongs to another window. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 51b7b39 commit 80d08da

5 files changed

Lines changed: 22 additions & 11 deletions

File tree

‎apps/web/src/components/usage/UsagePage.refresh.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ vi.mock("../../state/usage", () => ({
5050
},
5151
],
5252
isPending: false,
53+
shown: null,
5354
isPartial: false,
5455
refresh: async () => undefined,
5556
}),

‎apps/web/src/components/usage/UsagePage.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ beforeEach(() => {
7373
environments,
7474
selectedEnvironments: environments,
7575
isPending: false,
76+
shown: null,
7677
isPartial: false,
7778
refresh: vi.fn(),
7879
});

‎apps/web/src/components/usage/UsagePage.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ export function UsagePage() {
146146
merged: answeredUsage,
147147
environments,
148148
selectedEnvironments,
149-
isPending,
150149
shown,
151150
isPartial,
152151
refresh,
@@ -156,13 +155,14 @@ export function UsagePage() {
156155
const shownWindow = shown?.window ?? window;
157156
const shownHourly = shownWindow.resolution === "hour";
158157
const refreshingUsage = isRefreshing && !showingLimits;
159-
// Kept usage is all old, so every figure waits on the new window.
158+
// Usage kept from another window is all old, so every figure stays muted.
159+
const showingKept = shown !== null && shown.window !== window;
160160
const loading = useMemo(
161161
() =>
162-
isPending
162+
showingKept
163163
? { partial: true, everyProvider: true, providers: new Set<UsageProviderKind>() }
164164
: usageLoadingState(selectedEnvironments, refreshingUsage),
165-
[isPending, refreshingUsage, selectedEnvironments],
165+
[showingKept, refreshingUsage, selectedEnvironments],
166166
);
167167
const isProviderLoading = (provider: UsageProviderKind) =>
168168
loading.everyProvider || loading.providers.has(provider);

‎apps/web/src/state/usage.test.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,14 @@ describe("usage environment selection", () => {
177177
expect(latest.isPending).toBe(true);
178178
expect(latest.shown?.merged.costUsd).toBe(10);
179179

180+
// A window that fails everywhere keeps it too.
181+
testState.environments = [{ ...environment("a", null), isPending: false, error: "Offline" }];
182+
await act(() => renderer?.update(<Probe selected={selected} />));
183+
expect(latest.isPending).toBe(false);
184+
expect(latest.shown?.merged.costUsd).toBe(10);
185+
180186
// Another selection has nothing of its own to show.
187+
testState.environments = [environment("a", null)];
181188
await select("a");
182189
expect(latest.shown).toBeNull();
183190
});

‎apps/web/src/state/usage.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ export interface UsageView {
7575
readonly isPending: boolean;
7676
/**
7777
* The usage to draw: this window's once a selected environment answers,
78-
* until then the last window answered for the same selection, else null.
78+
* until then the last window answered for the same selection. Null while
79+
* nothing has answered and something still could.
7980
*/
8081
readonly shown: { readonly window: UsageSummaryInput; readonly merged: MergedUsage } | null;
8182
/**
@@ -177,18 +178,19 @@ export function useUsage(
177178
(NonNullable<UsageView["shown"]> & { readonly selection: typeof selectedEnvironmentIds }) | null
178179
>(null);
179180
if (
180-
!isPending &&
181+
answeredCount > 0 &&
181182
(lastAnswered?.merged !== merged ||
182183
lastAnswered.window !== input ||
183184
lastAnswered.selection !== selectedEnvironmentIds)
184185
) {
185186
setLastAnswered({ window: input, merged, selection: selectedEnvironmentIds });
186187
}
187-
const shown = !isPending
188-
? { window: input, merged }
189-
: lastAnswered?.selection === selectedEnvironmentIds
190-
? lastAnswered
191-
: null;
188+
const kept = lastAnswered?.selection === selectedEnvironmentIds ? lastAnswered : null;
189+
// With no answers, even failed ones keep the last answered usage on screen.
190+
const shown =
191+
answeredCount > 0
192+
? { window: input, merged }
193+
: (kept ?? (isPending ? null : { window: input, merged }));
192194

193195
return {
194196
merged,

0 commit comments

Comments
 (0)