From fe5a5a910f4f15834710e171a9d6ab0a50dc9c73 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sat, 14 Nov 2026 08:50:02 +0100 Subject: [PATCH 1/2] fix(ui): the test case or Finding you selected stays selected when you come back to its tab Selecting an older record, then opening another tab or starting a new draft from the Note, brought the tab back on the newest record: the record views unmount while another tab is open, and their selection lived inside them. The controller now remembers the selected record per view (useRecordSelection). The views start from it and report each selection; navigation that opens a specific record (Library, a route, a created record) sets it through focusRecord, which also keeps the route's focused record. Removes the unused statusLabel and the unused mergeRecordLists re-export. Co-Authored-By: Claude Opus 5.5 --- .../src/app/AppShell.recordSelection.test.tsx | 68 +++++++++++++++++++ frontend/src/app/AppShell.tsx | 6 +- frontend/src/app/sessionNavigationActions.ts | 8 +-- .../src/app/useAppController.navigation.ts | 6 +- frontend/src/app/useAppController.ts | 8 +-- frontend/src/app/useRecordSelection.ts | 28 ++++++++ frontend/src/ui/format.ts | 4 -- frontend/src/views/FindingsView.tsx | 4 ++ frontend/src/views/TestwareView.tsx | 12 +++- 9 files changed, 125 insertions(+), 19 deletions(-) create mode 100644 frontend/src/app/AppShell.recordSelection.test.tsx create mode 100644 frontend/src/app/useRecordSelection.ts diff --git a/frontend/src/app/AppShell.recordSelection.test.tsx b/frontend/src/app/AppShell.recordSelection.test.tsx new file mode 100644 index 00000000..167c4f00 --- /dev/null +++ b/frontend/src/app/AppShell.recordSelection.test.tsx @@ -0,0 +1,68 @@ +import '@testing-library/jest-dom/vitest' +import { fireEvent, render, screen, waitFor, within } from '@testing-library/react' +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import { draftFixture, entryFixture, findingFixture } from '../test/fixtures' +import { cleanupControllerTest, getTauriMock, setupControllerTest, useAppController } from './useAppController.testHarness' + +const { AppShell } = await import('./AppShell') +const tauri = getTauriMock() +function Subject() { return } + +beforeEach(() => { + setupControllerTest() + HTMLElement.prototype.scrollIntoView = vi.fn() + tauri.listDrafts.mockResolvedValue([ + draftFixture({ id: 'draft-new', title: 'Newer test cases', updatedAt: '2026-11-13T09:00:00Z' }), + draftFixture({ id: 'draft-old', title: 'Older test cases', updatedAt: '2026-07-22T00:00:00Z' }), + ]) + tauri.listFindings.mockResolvedValue([ + findingFixture({ id: 'finding-new', title: 'Newer finding', updatedAt: '2026-11-13T09:00:00Z' }), + findingFixture({ id: 'finding-old', title: 'Older finding', updatedAt: '2026-07-22T00:00:00Z' }), + ]) + tauri.readNoteSource.mockResolvedValue({ entry: entryFixture({ body: '

Pay with a saved card.

' }), revision: 0 }) +}) +afterEach(cleanupControllerTest) + +const openTab = async (name: RegExp) => fireEvent.click(await screen.findByRole('tab', { name })) +const shownRecord = (title: string) => screen.findByRole('heading', { level: 2, name: title }) + +async function selectRecord(listName: string, title: string) { + fireEvent.click(await within(await screen.findByRole('complementary', { name: listName })).findByText(title)) + await shownRecord(title) +} + +async function openAndCancelDraft(createLabel: string, titleLabel: string) { + const create = screen.getByRole('button', { name: createLabel }) + await waitFor(() => expect(create).toBeEnabled()) + fireEvent.click(create) + await screen.findByRole('textbox', { name: titleLabel }) + fireEvent.click(screen.getByRole('button', { name: 'Cancel' })) + await waitFor(() => expect(screen.queryByRole('textbox', { name: titleLabel })).not.toBeInTheDocument()) +} + +it('keeps the selected test cases when another tab opens and a new draft is cancelled', async () => { + render() + await openTab(/Test cases/) + await selectRecord('Test cases records', 'Older test cases') + + await openTab(/Findings/) + await shownRecord('Newer finding') + await openTab(/Test cases/) + expect(await shownRecord('Older test cases')).toBeInTheDocument() + + await openAndCancelDraft('Create test cases', 'Test case title') + expect(await shownRecord('Older test cases')).toBeInTheDocument() +}) + +it('keeps the selected finding when the Note opens and a new draft is cancelled', async () => { + render() + await openTab(/Findings/) + await selectRecord('Findings records', 'Older finding') + + await openTab(/Note/) + await openTab(/Findings/) + expect(await shownRecord('Older finding')).toBeInTheDocument() + + await openAndCancelDraft('Create finding', 'Finding title') + expect(await shownRecord('Older finding')).toBeInTheDocument() +}) diff --git a/frontend/src/app/AppShell.tsx b/frontend/src/app/AppShell.tsx index ddb6844a..1670ec8b 100644 --- a/frontend/src/app/AppShell.tsx +++ b/frontend/src/app/AppShell.tsx @@ -184,7 +184,8 @@ export function AppShell(c: AppController) { error={c.error} isBusy={c.isBusy} activeGenerationJob={c.activeTestwareJob} - initialSelectedRecordId={c.focusedRecordId} + initialSelectedRecordId={c.selectedRecordIds.testware} + onSelectedRecordChange={c.selectRecord.testware} createdRecordEditTarget={c.createdRecordEditTarget?.sessionId === c.activeSession?.id && c.createdRecordEditTarget?.kind === 'testware' ? c.createdRecordEditTarget : null} onCreatedRecordEditTargetConsumed={c.consumeCreatedRecordEditTarget} loadState={c.draftLoadState} @@ -215,7 +216,8 @@ export function AppShell(c: AppController) { error={c.error} isBusy={c.isBusy} activeGenerationJob={c.activeFindingJob} - initialSelectedRecordId={c.focusedRecordId} + initialSelectedRecordId={c.selectedRecordIds.findings} + onSelectedRecordChange={c.selectRecord.findings} createdRecordEditTarget={c.createdRecordEditTarget?.sessionId === c.activeSession?.id && c.createdRecordEditTarget?.kind === 'finding' ? c.createdRecordEditTarget : null} onCreatedRecordEditTargetConsumed={c.consumeCreatedRecordEditTarget} loadState={c.findingLoadState} diff --git a/frontend/src/app/sessionNavigationActions.ts b/frontend/src/app/sessionNavigationActions.ts index 67c6078c..efe38b4c 100644 --- a/frontend/src/app/sessionNavigationActions.ts +++ b/frontend/src/app/sessionNavigationActions.ts @@ -24,7 +24,7 @@ type SessionNavigationContext = { requestSessionNavigation: (view: MainView, navigate: () => Promise) => Promise setActiveView: Dispatch> setError: Dispatch> - setFocusedRecordId: Dispatch> + focusRecord: (view: MainView, recordId: string | null) => void setPendingSettingsSection: Dispatch> setSettingsSection: Dispatch> } @@ -48,7 +48,7 @@ export function createSessionNavigationActions(ctx: SessionNavigationContext) { const session = ctx.sessions.find((candidate) => candidate.id === sessionId) ?? await reopenSession(sessionId) if (!ctx.sessionActions.sessionNavigationIsCurrent(epoch)) return await ctx.sessionActions.openSession(session, () => { - ctx.setFocusedRecordId(recordId) + ctx.focusRecord(view, recordId) ctx.setActiveView(view) }, epoch) } catch (cause) { @@ -72,7 +72,7 @@ export function createSessionNavigationActions(ctx: SessionNavigationContext) { return } if (!route.sessionId || ctx.activeSessionId === route.sessionId) { - ctx.setFocusedRecordId(route.recordId) + ctx.focusRecord(route.view, route.recordId) ctx.requestActiveView(route.view) return } @@ -82,7 +82,7 @@ export function createSessionNavigationActions(ctx: SessionNavigationContext) { const session = ctx.sessions.find((candidate) => candidate.id === route.sessionId) ?? await reopenSession(route.sessionId!) if (!ctx.sessionActions.sessionNavigationIsCurrent(epoch)) return await ctx.sessionActions.openSession(session, () => { - ctx.setFocusedRecordId(route.recordId) + ctx.focusRecord(route.view, route.recordId) ctx.setActiveView(route.view) }, epoch) } catch (cause) { diff --git a/frontend/src/app/useAppController.navigation.ts b/frontend/src/app/useAppController.navigation.ts index 6f19fd9c..976bdca8 100644 --- a/frontend/src/app/useAppController.navigation.ts +++ b/frontend/src/app/useAppController.navigation.ts @@ -33,7 +33,7 @@ type AppControllerNavigationContext = { settingsSection: string | null setActiveView: Dispatch> setError: Dispatch> - setFocusedRecordId: Dispatch> + focusRecord: (view: MainView, recordId: string | null) => void setCreatedRecordEditTarget: Dispatch> setPendingNavigationView: Dispatch> setPendingSettingsSection: Dispatch> @@ -58,7 +58,7 @@ export function useAppControllerNavigation(ctx: AppControllerNavigationContext) const view = target.kind === 'testware' ? 'testware' : 'findings' finishActiveViewNavigation(view, () => { if (!isCurrent()) return - ctx.setFocusedRecordId(null) + ctx.focusRecord(view, null) ctx.setCreatedRecordEditTarget(target) ctx.setActiveView(view) }) @@ -190,7 +190,7 @@ export function useAppControllerNavigation(ctx: AppControllerNavigationContext) requestSessionNavigation, setActiveView: ctx.setActiveView, setError: ctx.setError, - setFocusedRecordId: ctx.setFocusedRecordId, + focusRecord: ctx.focusRecord, setPendingSettingsSection: ctx.setPendingSettingsSection, setSettingsSection: ctx.setSettingsSection, }) diff --git a/frontend/src/app/useAppController.ts b/frontend/src/app/useAppController.ts index 28aec389..496e3e59 100644 --- a/frontend/src/app/useAppController.ts +++ b/frontend/src/app/useAppController.ts @@ -30,12 +30,12 @@ import { useSettingsSectionFocus } from './useAppController.settingsFocus' import { useAppSettlement } from './appSettlement' import { useAppControllerNavigation } from './useAppController.navigation' import { useAttachmentCleanup } from './useAttachmentCleanup' +import { useRecordSelection } from './useRecordSelection' import { useSummaryRecovery } from './useSummaryRecovery' import { captureAvailableRecordContext } from './conversationPresentation' import { useSessionChat } from './useSessionChat' import { findingFromTestCase } from '../findings/fromTestCase' import type { TestRunCase } from '../tauri' -export { mergeRecordLists } from './useRecordHydration' export function useAppController() { const { sessions, @@ -68,7 +68,7 @@ export function useAppController() { const pendingNavigationContinuationRef = useRef<(() => void | Promise) | null>(null) const pendingNavigationEpochRef = useRef(0) const [settingsSection, setSettingsSection] = useState(null) - const [focusedRecordId, setFocusedRecordId] = useState(null) + const { focusedRecordId, selectedRecordIds, selectRecord, focusRecord } = useRecordSelection() const [createdRecordEditTarget, setCreatedRecordEditTarget] = useState(null) const [initialNavigationRoute] = useState(() => parseNavigationRoute(window.location.hash)) const settingsReturnViewRef = useRef('sessions') @@ -208,7 +208,7 @@ export function useAppController() { discardSettingsDraft, focusedRecordId, pendingNavigationContinuationRef, pendingNavigationEpochRef, pendingNavigationView, recordActions, sessionActions, sessions, settingsDirty, settingsReturnViewRef, settingsSection, - setActiveView, setError, setFocusedRecordId, setCreatedRecordEditTarget, setPendingNavigationView, setPendingSettingsSection, setSettingsSection, + setActiveView, setError, focusRecord, setCreatedRecordEditTarget, setPendingNavigationView, setPendingSettingsSection, setSettingsSection, }) usePendingChangeProtection({ hasPendingChanges: settlement.hasPendingChanges, @@ -257,7 +257,7 @@ export function useAppController() { busyAction, deleteConfirmation, deleteCopy, error, - focusedRecordId, + focusedRecordId, selectedRecordIds, selectRecord, isBusy, canSwitchSession, latestNoteGenerationUndo, noteBody, noteBodyHtml, diff --git a/frontend/src/app/useRecordSelection.ts b/frontend/src/app/useRecordSelection.ts new file mode 100644 index 00000000..366ffe64 --- /dev/null +++ b/frontend/src/app/useRecordSelection.ts @@ -0,0 +1,28 @@ +import { useCallback, useMemo, useState } from 'react' +import type { MainView } from '../ui/types' + +export type RecordView = 'testware' | 'findings' +type RecordSelection = Record + +const isRecordView = (view: MainView): view is RecordView => view === 'testware' || view === 'findings' + +/** + * The record each record view shows. A view unmounts when another tab is open, so its selection is kept here: + * returning to Test cases or Findings, or leaving a new draft, shows the record that was selected, not the newest. + * `focusedRecordId` is the record navigation last opened, which the route names. + */ +export function useRecordSelection() { + const [focusedRecordId, setFocusedRecordId] = useState(null) + const [selectedRecordIds, setSelectedRecordIds] = useState({ testware: null, findings: null }) + const select = useCallback((view: RecordView, id: string | null) => + setSelectedRecordIds((previous) => previous[view] === id ? previous : { ...previous, [view]: id }), []) + const selectRecord = useMemo(() => ({ + testware: (id: string | null) => select('testware', id), + findings: (id: string | null) => select('findings', id), + }), [select]) + const focusRecord = useCallback((view: MainView, id: string | null) => { + setFocusedRecordId(id) + if (isRecordView(view)) select(view, id) + }, [select]) + return { focusedRecordId, selectedRecordIds, selectRecord, focusRecord } +} diff --git a/frontend/src/ui/format.ts b/frontend/src/ui/format.ts index 585225fe..1d00c5ef 100644 --- a/frontend/src/ui/format.ts +++ b/frontend/src/ui/format.ts @@ -98,10 +98,6 @@ function escapeRegExp(value: string): string { return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') } -export function statusLabel(status: string): string { - return status.replace(/([A-Z])/g, ' $1').toLowerCase() -} - /** A day and month, such as `13 Nov` in a day-first locale. */ export function formatShortDate(value: string): string { const date = new Date(value) diff --git a/frontend/src/views/FindingsView.tsx b/frontend/src/views/FindingsView.tsx index d42fa747..0b9180d7 100644 --- a/frontend/src/views/FindingsView.tsx +++ b/frontend/src/views/FindingsView.tsx @@ -66,6 +66,7 @@ export function FindingsView({ isBusy, activeGenerationJob, initialSelectedRecordId = null, + onSelectedRecordChange, createdRecordEditTarget = null, onCreatedRecordEditTargetConsumed, loadState = 'ready', @@ -90,7 +91,9 @@ export function FindingsView({ error: string | null isBusy: boolean activeGenerationJob: GenerationJobStatus | null + /** The record to show first; told each record the view selects, so it can be shown again after the view returns. */ initialSelectedRecordId?: string | null + onSelectedRecordChange?: (id: string | null) => void createdRecordEditTarget?: CreatedRecordEditTarget | null onCreatedRecordEditTargetConsumed?: (target: CreatedRecordEditTarget) => void loadState?: RecordLoadState @@ -121,6 +124,7 @@ export function FindingsView({ isBusy={isBusy} activeGenerationJob={activeGenerationJob} initialSelectedRecordId={initialSelectedRecordId} + onSelectedRecordChange={onSelectedRecordChange} createdRecordEditTarget={createdRecordEditTarget} onCreatedRecordEditTargetConsumed={onCreatedRecordEditTargetConsumed} loadState={loadState} diff --git a/frontend/src/views/TestwareView.tsx b/frontend/src/views/TestwareView.tsx index a6b19ede..c1386a16 100644 --- a/frontend/src/views/TestwareView.tsx +++ b/frontend/src/views/TestwareView.tsx @@ -1,7 +1,7 @@ import type { RecordChatActions } from '../app/sessionChat.context' import type { RecordCreationCommands } from './RecordCreationActions' import { Box } from 'lucide-react' -import { useEffect, useRef } from 'react' +import { useCallback, useEffect, useRef } from 'react' import { Badge } from '../components/Badge' import type { RichEditorImageUpload } from '../editor/RichTextEditor' import { testwareGenerationSummary } from '../testware/generationPreferences' @@ -48,6 +48,7 @@ export function TestwareView({ isBusy, activeGenerationJob, initialSelectedRecordId = null, + onSelectedRecordChange, createdRecordEditTarget = null, onCreatedRecordEditTargetConsumed, loadState = 'ready', @@ -80,7 +81,9 @@ export function TestwareView({ error: string | null isBusy: boolean activeGenerationJob: GenerationJobStatus | null + /** The record to show first; told each record the view selects, so it can be shown again after the view returns. */ initialSelectedRecordId?: string | null + onSelectedRecordChange?: (id: string | null) => void createdRecordEditTarget?: CreatedRecordEditTarget | null onCreatedRecordEditTargetConsumed?: (target: CreatedRecordEditTarget) => void loadState?: RecordLoadState @@ -97,6 +100,11 @@ export function TestwareView({ }) { const testRuns = useTestRuns(sessionId) useShownTestRun(recordChat, sessionId, testRuns) + const { keepForDraft } = testRuns + const selectDraft = useCallback((id: string | null) => { + keepForDraft(id) + onSelectedRecordChange?.(id) + }, [keepForDraft, onSelectedRecordChange]) const runsOf = (draftId: string) => testRuns.summaries.filter((summary) => summary.draftId === draftId) return ( ) : null} - onSelectedRecordChange={testRuns.keepForDraft} + onSelectedRecordChange={selectDraft} busyActionFor={(draft, kind) => { switch (kind) { case 'delete': From fc365755aa6b0ed59d9dfed027c49df2f70377a3 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sat, 14 Nov 2026 08:55:30 +0100 Subject: [PATCH 2/2] fix(ui): a record opened from the Library stays the target while its tab loads The view reports no selection while its records load, which cleared the record navigation had just focused, so the tab opened on the newest record. An empty report no longer clears the remembered selection. Co-Authored-By: Claude Opus 5.5 --- frontend/src/app/useRecordSelection.test.ts | 12 ++++++++++++ frontend/src/app/useRecordSelection.ts | 5 +++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 frontend/src/app/useRecordSelection.test.ts diff --git a/frontend/src/app/useRecordSelection.test.ts b/frontend/src/app/useRecordSelection.test.ts new file mode 100644 index 00000000..ce70349c --- /dev/null +++ b/frontend/src/app/useRecordSelection.test.ts @@ -0,0 +1,12 @@ +import { act, renderHook } from '@testing-library/react' +import { expect, it } from 'vitest' +import { useRecordSelection } from './useRecordSelection' + +it('keeps the record navigation asked for while the view has no record to show yet', () => { + const { result } = renderHook(() => useRecordSelection()) + act(() => result.current.focusRecord('findings', 'finding-old')) + act(() => result.current.selectRecord.findings(null)) + expect(result.current.selectedRecordIds.findings).toBe('finding-old') + act(() => result.current.selectRecord.findings('finding-new')) + expect(result.current.selectedRecordIds).toEqual({ testware: null, findings: 'finding-new' }) +}) diff --git a/frontend/src/app/useRecordSelection.ts b/frontend/src/app/useRecordSelection.ts index 366ffe64..b14fc35f 100644 --- a/frontend/src/app/useRecordSelection.ts +++ b/frontend/src/app/useRecordSelection.ts @@ -16,9 +16,10 @@ export function useRecordSelection() { const [selectedRecordIds, setSelectedRecordIds] = useState({ testware: null, findings: null }) const select = useCallback((view: RecordView, id: string | null) => setSelectedRecordIds((previous) => previous[view] === id ? previous : { ...previous, [view]: id }), []) + // A view shows no record while its records load, so an empty report keeps the record navigation asked for. const selectRecord = useMemo(() => ({ - testware: (id: string | null) => select('testware', id), - findings: (id: string | null) => select('findings', id), + testware: (id: string | null) => { if (id) select('testware', id) }, + findings: (id: string | null) => { if (id) select('findings', id) }, }), [select]) const focusRecord = useCallback((view: MainView, id: string | null) => { setFocusedRecordId(id)