fix(usage): keep the sidebar usage summary current while it is shown

The summary fetched reports once on mount and never again: no interval,
window-focus and reconnect refetches are off app-wide, and the daemon does
not push report changes. It only updated when the Usage modal or Settings
mounted the same query and refetched it. It now re-reads every minute while
visible; the daemon answers from its five-minute cache, so source APIs are
still hit at most once per cache window.
This commit is contained in:
Mohamed Boudra
2026-11-06 04:11:48 +01:00
parent 3ed57e9dd7
commit 387be14a2e
2 changed files with 27 additions and 1 deletions
@@ -469,3 +469,27 @@ test("visible usage summary requests reports on load", async ({ page }) => {
await expectPinnedUsage(page, ["31% 5h", "54% wk", "7% 5h", "12% wk"]);
expect(usage.listRequests()).toHaveLength(1);
});
test("visible usage summary picks up changed reports without opening Usage", async ({ page }) => {
test.setTimeout(180_000);
const usage = await installUsageReportsFixture(page, {
lists: [() => claudeSessionAt(31), () => claudeSessionAt(80)],
});
await seedSidebarFooterPreferences(page, [{ key: "usage", visible: true }]);
await page.setViewportSize(WIDE);
await page.clock.install();
await gotoAppShell(page);
await expectPinnedUsage(page, ["31% 5h", "54% wk", "7% 5h", "12% wk"]);
expect(usage.listRequests()).toHaveLength(1);
await page.clock.fastForward(61_000);
await expectPinnedUsage(page, ["80% 5h", "54% wk", "7% 5h", "12% wk"]);
await expect(usageModal(page)).toHaveCount(0);
});
function claudeSessionAt(usedPct: number) {
const reports = claudeAndCodexReports();
const claude = reports[0]!;
if (claude.report.status === "available") claude.report.windows[0]!.usedPct = usedPct;
return reports;
}
+3 -1
View File
@@ -166,7 +166,8 @@ const NO_REPORTS: UsageReportEntry[] = [];
/**
* The reports of the sidebar's usage host, which is connected and reports usage; none until they
* load, or without a host.
* load, or without a host. Re-read every minute while the summary is shown, since the daemon never
* pushes report changes.
*/
export function useUsageHostReports(serverId: string | null): UsageReportEntry[] {
const setting = useUsageSetting(serverId);
@@ -177,6 +178,7 @@ export function useUsageHostReports(serverId: string | null): UsageReportEntry[]
queryFn: serverId ? () => listReports(queryClient, serverId) : skipToken,
dataShape: "list",
staleTimeMs: REPORTS_STALE_TIME_MS,
refetchInterval: REPORTS_STALE_TIME_MS,
});
return setting.enabled ? (query.data ?? NO_REPORTS) : NO_REPORTS;
}