From 288975f7b85c40ef247805e34f64633ab780bb26 Mon Sep 17 00:00:00 2001
From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com>
Date: Tue, 1 Dec 2026 20:31:33 -0500
Subject: [PATCH] Keep usage loading state, model filter, and session table
range consistent
---
.../usage/SessionUsageContent.test.tsx | 52 ++++++++++++++
.../components/usage/SessionUsageContent.tsx | 3 +-
.../components/usage/UsageDashboard.test.tsx | 53 +++++++++++++--
.../src/components/usage/UsageDashboard.tsx | 7 +-
.../usage/UsageSessionsTable.test.tsx | 67 +++++++++++++++++++
.../components/usage/UsageSessionsTable.tsx | 19 ++----
6 files changed, 178 insertions(+), 23 deletions(-)
diff --git a/frontend/src/components/usage/SessionUsageContent.test.tsx b/frontend/src/components/usage/SessionUsageContent.test.tsx
index 7cd16f6a..972adc6c 100644
--- a/frontend/src/components/usage/SessionUsageContent.test.tsx
+++ b/frontend/src/components/usage/SessionUsageContent.test.tsx
@@ -269,6 +269,58 @@ describe('SessionUsageContent', () => {
expect(statText('Steps')).toContain('1')
})
+ it('keeps the selected model when the range becomes unresolved', async () => {
+ vi.spyOn(Date, 'now').mockReturnValue(NOW)
+ const user = userEvent.setup()
+
+ renderContent({
+ fetchRoutes: [
+ messagesRoute({
+ messages: [
+ assistantStep({
+ id: 'msg_a',
+ created: NOW - 60_000,
+ cost: 1,
+ input: 1000,
+ model: MODEL_A,
+ }),
+ assistantStep({
+ id: 'msg_b',
+ created: NOW - 120_000,
+ cost: 2,
+ input: 2000,
+ model: MODEL_B,
+ }),
+ ],
+ }),
+ ],
+ })
+
+ await screen.findByRole('heading', { name: 'Models' })
+
+ await user.click(screen.getByRole('button', { name: 'Filter by model' }))
+ await user.click(await screen.findByRole('menuitemcheckbox', { name: 'anthropic/claude' }))
+ expect(screen.getByRole('button', { name: 'Filter by model' })).toHaveTextContent(
+ 'anthropic/claude',
+ )
+
+ await user.click(screen.getByRole('combobox', { name: 'Range' }))
+ await user.click(screen.getByRole('option', { name: 'Custom' }))
+ expect(
+ screen.getByText('Choose a start and end time to see usage for a custom range.'),
+ ).toBeInTheDocument()
+
+ await user.click(screen.getByRole('combobox', { name: 'Range' }))
+ await user.click(screen.getByRole('option', { name: 'Last 4 hours' }))
+
+ await waitFor(() =>
+ expect(screen.getByRole('button', { name: 'Filter by model' })).toHaveTextContent(
+ 'anthropic/claude',
+ ),
+ )
+ expect(screen.getByRole('heading', { name: 'Models' })).toBeInTheDocument()
+ })
+
it('persists the range selection and keeps the model filter out of storage', async () => {
vi.spyOn(Date, 'now').mockReturnValue(NOW)
const user = userEvent.setup()
diff --git a/frontend/src/components/usage/SessionUsageContent.tsx b/frontend/src/components/usage/SessionUsageContent.tsx
index 381c1726..fc7e9b34 100644
--- a/frontend/src/components/usage/SessionUsageContent.tsx
+++ b/frontend/src/components/usage/SessionUsageContent.tsx
@@ -47,10 +47,11 @@ export function SessionUsageContent({ sessionId, compact }: SessionUsageContentP
)
useEffect(() => {
+ if (resolvedRange === null || steps === undefined) return
const available = new Set(summary.availableModels)
const remaining = models.filter((key) => available.has(key))
if (remaining.length !== models.length) setModels(remaining)
- }, [models, summary.availableModels])
+ }, [models, resolvedRange, steps, summary.availableModels])
const toolTotals = useMemo(
() =>
diff --git a/frontend/src/components/usage/UsageDashboard.test.tsx b/frontend/src/components/usage/UsageDashboard.test.tsx
index ae4488fc..b875cad1 100644
--- a/frontend/src/components/usage/UsageDashboard.test.tsx
+++ b/frontend/src/components/usage/UsageDashboard.test.tsx
@@ -1,7 +1,7 @@
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
import { screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
-import type { SessionStatsInfo } from '@opencode-manager/shared/opencode'
+import type { SessionInfo, SessionStatsInfo } from '@opencode-manager/shared/opencode'
import { LOCAL_OPENCODE_SERVER_ID } from '@opencode-manager/shared/schemas'
import { renderWithProviders } from '@/test/render'
import { installPointerCapture } from '@/test/browserFakes'
@@ -57,13 +57,18 @@ function statsRoute(path: string = statsPath): FetchRoute {
return { method: 'GET', path, respond: () => ({ json: { data: statsFixture } }) }
}
-function localSessionsRoute(): FetchRoute {
+const inRangeTime = {
+ created: statsFixture.range.from + 1000,
+ updated: statsFixture.range.from + 2000,
+}
+
+function localSessionsRoute(sessions?: SessionInfo[]): FetchRoute {
return {
method: 'GET',
path: '/api/opencode/api/session',
respond: () => ({
json: {
- data: [
+ data: sessions ?? [
makeSession({
id: 'ses_1',
title: 'Fix tests',
@@ -75,7 +80,7 @@ function localSessionsRoute(): FetchRoute {
reasoning: 0,
cache: { read: 0, write: 0 },
},
- time: { created: 1, updated: 2 },
+ time: inRangeTime,
}),
],
cursor: {},
@@ -95,7 +100,7 @@ function remoteSessionsRoute(serverId: string): FetchRoute {
id: 'ses_remote',
title: 'Remote session',
cost: 1,
- time: { created: 1, updated: 2 },
+ time: inRangeTime,
}),
],
cursor: {},
@@ -180,6 +185,44 @@ describe('UsageDashboard', () => {
expect(screen.queryByRole('heading', { name: 'Activity' })).not.toBeInTheDocument()
})
+ it('lists only sessions inside the range reported by the stats response', async () => {
+ freezeNow()
+
+ const beforeRange = makeSession({
+ id: 'ses_before',
+ title: 'Before stats range',
+ cost: 2,
+ tokens: {
+ input: 100,
+ output: 50,
+ reasoning: 0,
+ cache: { read: 0, write: 0 },
+ },
+ time: { created: statsFixture.range.from - 5000, updated: statsFixture.range.from - 1000 },
+ })
+ const insideRange = makeSession({
+ id: 'ses_inside',
+ title: 'Inside stats range',
+ cost: 0.5,
+ tokens: {
+ input: 10,
+ output: 5,
+ reasoning: 0,
+ cache: { read: 0, write: 0 },
+ },
+ time: inRangeTime,
+ })
+
+ renderWithProviders(
Loading sessions…
) : sortedSessions.length === 0 ? (No sessions in this range.