fix(sessions): address PR #403 review findings

- fetch picker sessions only while the dialog is open
- list all-projects sessions with one request per page
- block infinite-scroll paging while a search is pending
- memoize picker rows with stable callbacks
- remove deleted sessions from list caches instead of refetching
- keep the repo tab when the open session is deleted
- reuse useSessionSearch in the sidebar session tree
- drop dead activeSessionID/isActive and share delete, session-key and repo-map helpers
- move usePersistentBoolean to its own hook
This commit is contained in:
Chris Scott committed 2026-10-08 14:36:26 -04:00
1 parent 85288d6393
commit 2d14972fad
24 files changed
+814 -412

No files matched your search

+74 -3
View File
@@ -134,12 +134,58 @@ describe('useSessionsAcrossDirectories', () => {
})
})
it('deletes each session through the facade and invalidates the session list cache', async () => {
it('issues one call per page across all directories and paginates by cursor', async () => {
mocks.listSessionPage.mockImplementation(async ({ cursor }: { cursor?: string }) => {
if (cursor === 'cursor_all') {
return { items: [sessionInfo('ses_all2', '/w/b', 2000)] }
}
return { items: [sessionInfo('ses_all1', '/w/a')], nextCursor: 'cursor_all' }
})
const queryClient = createQueryClient()
const { result } = renderHook(
() => useSessionsAcrossDirectories(['/w/a', '/w/b'], { allDirectories: true }),
{ wrapper: createWrapper(queryClient) },
)
await waitFor(() => {
expect(result.current.isLoading).toBe(false)
})
expect(mocks.listSessionPage).toHaveBeenCalledTimes(1)
expect(mocks.listSessionPage).toHaveBeenCalledWith({
limit: 25,
order: 'desc',
search: undefined,
})
expect(result.current.data.map((session) => session.id)).toEqual(['ses_all1'])
expect(result.current.hasNextPage).toBe(true)
await act(async () => {
await result.current.fetchNextPage()
})
await waitFor(() => {
expect(result.current.data).toHaveLength(2)
})
expect(mocks.listSessionPage).toHaveBeenCalledTimes(2)
expect(mocks.listSessionPage).toHaveBeenLastCalledWith({ cursor: 'cursor_all' })
expect(result.current.data.map((session) => session.id)).toEqual(['ses_all1', 'ses_all2'])
})
it('deletes each session through the facade and removes it from cached lists without invalidating', async () => {
mocks.deleteSession.mockResolvedValue(undefined)
const queryClient = createQueryClient()
const listKey = ['opencode', 'sessions', '/w/a', { search: undefined, limit: 25 }]
queryClient.setQueryData(listKey, { pages: [{ items: [], cursors: {} }], pageParams: [undefined] })
const listKey = ['opencode', 'sessions', '/w/a', { search: undefined, limit: 25, allDirectories: false }]
queryClient.setQueryData(listKey, {
pages: [{
items: [sessionInfo('ses_a1', '/w/a'), sessionInfo('ses_a2', '/w/a')],
nextParam: undefined,
}],
pageParams: [undefined],
})
const { result } = renderHook(() => useDeleteSession(['/w/a']), {
wrapper: createWrapper(queryClient),
@@ -150,6 +196,31 @@ describe('useSessionsAcrossDirectories', () => {
})
expect(mocks.deleteSession).toHaveBeenCalledWith('ses_a1')
const cached = queryClient.getQueryData<{ pages: Array<{ items: SessionInfo[] }> }>(listKey)
expect(cached?.pages[0].items.map((session) => session.id)).toEqual(['ses_a2'])
expect(queryClient.getQueryState(listKey)?.isInvalidated).toBe(false)
})
it('invalidates the session list cache when a delete fails', async () => {
mocks.deleteSession.mockRejectedValue(new Error('nope'))
const queryClient = createQueryClient()
const listKey = ['opencode', 'sessions', '/w/a', { search: undefined, limit: 25, allDirectories: false }]
queryClient.setQueryData(listKey, {
pages: [{ items: [sessionInfo('ses_a1', '/w/a')], nextParam: undefined }],
pageParams: [undefined],
})
const { result } = renderHook(() => useDeleteSession(['/w/a']), {
wrapper: createWrapper(queryClient),
})
await act(async () => {
await expect(
result.current.mutateAsync({ id: 'ses_a1', directory: '/w/a' }),
).rejects.toThrow('Failed to delete 1 session(s)')
})
await waitFor(() => {
expect(queryClient.getQueryState(listKey)?.isInvalidated).toBe(true)
})