Skip to content

Commit 55c7861

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 3b44c3c commit 55c7861

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
@@ -74,6 +74,7 @@ beforeEach(() => {
7474
environments,
7575
selectedEnvironments: environments,
7676
isPending: false,
77+
shown: null,
7778
isPartial: false,
7879
refresh: vi.fn(),
7980
});

‎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
@@ -178,7 +178,14 @@ describe("usage environment selection", () => {
178178
expect(latest.isPending).toBe(true);
179179
expect(latest.shown?.merged.costUsd).toBe(10);
180180

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

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ export interface UsageView {
9898
readonly isPending: boolean;
9999
/**
100100
* The usage to draw: this window's once a selected environment answers,
101-
* until then the last window answered for the same selection, else null.
101+
* until then the last window answered for the same selection. Null while
102+
* nothing has answered and something still could.
102103
*/
103104
readonly shown: { readonly window: UsageSummaryInput; readonly merged: MergedUsage } | null;
104105
/**
@@ -208,18 +209,19 @@ export function useUsage(
208209
(NonNullable<UsageView["shown"]> & { readonly selection: typeof selectedEnvironmentIds }) | null
209210
>(null);
210211
if (
211-
!isPending &&
212+
answeredCount > 0 &&
212213
(lastAnswered?.merged !== merged ||
213214
lastAnswered.window !== input ||
214215
lastAnswered.selection !== selectedEnvironmentIds)
215216
) {
216217
setLastAnswered({ window: input, merged, selection: selectedEnvironmentIds });
217218
}
218-
const shown = !isPending
219-
? { window: input, merged }
220-
: lastAnswered?.selection === selectedEnvironmentIds
221-
? lastAnswered
222-
: null;
219+
const kept = lastAnswered?.selection === selectedEnvironmentIds ? lastAnswered : null;
220+
// With no answers, even failed ones keep the last answered usage on screen.
221+
const shown =
222+
answeredCount > 0
223+
? { window: input, merged }
224+
: (kept ?? (isPending ? null : { window: input, merged }));
223225

224226
return {
225227
merged,

0 commit comments

Comments
 (0)