Skip to content

Commit f4467d9

Browse files
fix(usage): settle refreshes before the new window commits
1 parent c15f4f8 commit f4467d9

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,8 @@ export function useUsage(
159159
refreshing: false,
160160
error: null as string | null,
161161
});
162-
const currentWindowKey = useRef(viewKey);
163162
const currentRefreshId = useRef(0);
164163
const pendingRefreshWindowKey = useRef(viewKey);
165-
useEffect(() => {
166-
currentWindowKey.current = viewKey;
167-
}, [viewKey]);
168164
useEffect(() => {
169165
// A refresh started while selecting the next window already targets this
170166
// committed key. Keep its request id and state so the completion can settle
@@ -226,7 +222,7 @@ export function useUsage(
226222
error = "Refresh failed. Showing the last successful usage snapshot.";
227223
}
228224
const nextState = completeUsageRefresh(
229-
currentWindowKey.current,
225+
pendingRefreshWindowKey.current,
230226
currentRefreshId.current,
231227
requestWindowKey,
232228
requestId,

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ let latest: UsageView;
109109
function Probe({
110110
selected,
111111
refreshThreads = false,
112+
windowInput = input,
112113
}: {
113114
selected: ReadonlySet<EnvironmentId> | null;
114115
refreshThreads?: boolean;
116+
windowInput?: typeof input;
115117
}) {
116-
const usage = useUsage(input, undefined, refreshThreads, selected);
118+
const usage = useUsage(windowInput, undefined, refreshThreads, selected);
117119
useLayoutEffect(() => {
118120
latest = usage;
119121
}, [usage]);
@@ -300,3 +302,26 @@ it("refreshes thread keys with the new window and newly published providers", as
300302
query.mockRestore();
301303
}
302304
});
305+
306+
it.each([false, true])(
307+
"settles a next-window refresh before React commits, failure=%s",
308+
async (fails) => {
309+
const selected = new Set([EnvironmentId.make("a")]);
310+
await select("a");
311+
const nextInput = {
312+
...input,
313+
sinceDay: UsageDay.make("2026-09-05"),
314+
untilDay: UsageDay.make("2026-09-06"),
315+
};
316+
if (fails) testState.refreshUsage.mockRejectedValueOnce(new Error("offline"));
317+
else testState.refreshUsage.mockResolvedValueOnce(undefined);
318+
await act(async () => {
319+
renderer?.update(<Probe selected={selected} windowInput={nextInput} />);
320+
await latest.refresh(nextInput);
321+
});
322+
expect(latest.isRefreshing).toBe(false);
323+
expect(latest.refreshError).toBe(
324+
fails ? "Refresh failed. Showing the last successful usage snapshot." : null,
325+
);
326+
},
327+
);

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,8 @@ export function useUsage(
205205
refreshing: false,
206206
error: null,
207207
});
208-
const currentViewKey = useRef(viewKey);
209208
const currentRefreshId = useRef(0);
210209
const pendingRefreshViewKey = useRef(viewKey);
211-
useEffect(() => {
212-
currentViewKey.current = viewKey;
213-
}, [viewKey]);
214210
useEffect(() => {
215211
const nextState = refreshStateForWindowChange(
216212
manualRefreshState,
@@ -305,7 +301,7 @@ export function useUsage(
305301
error = "Refresh failed. Showing the last successful usage snapshot.";
306302
}
307303
const nextState = completeUsageRefresh(
308-
currentViewKey.current,
304+
pendingRefreshViewKey.current,
309305
currentRefreshId.current,
310306
requestViewKey,
311307
requestId,

0 commit comments

Comments
 (0)