mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
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:
1 parent
85288d6393
commit
2d14972fad
24 files changed
+814
-412
No files matched your search
@@ -34,6 +34,7 @@ const {
|
||||
getSessionMock,
|
||||
sessionPinsData,
|
||||
sessionsHasNextPage,
|
||||
searchError,
|
||||
} = vi.hoisted(() => ({
|
||||
listReposMock: vi.fn(),
|
||||
sessionsData: [] as SessionFixture[],
|
||||
@@ -45,6 +46,7 @@ const {
|
||||
getSessionMock: vi.fn(),
|
||||
sessionPinsData: { current: [] as SessionPinFixture[] },
|
||||
sessionsHasNextPage: { current: false },
|
||||
searchError: { current: false },
|
||||
}))
|
||||
|
||||
vi.mock('@/api/repos', async (importOriginal) => {
|
||||
@@ -62,12 +64,17 @@ vi.mock('@/hooks/useOpenCode', async (importOriginal) => {
|
||||
return {
|
||||
...actual,
|
||||
useSessionsAcrossDirectories: (directories: string[], options?: { search?: string; limit?: number }) => {
|
||||
sessionsHookCalls.push({ directories, options })
|
||||
if (directories.length > 0) sessionsHookCalls.push({ directories, options })
|
||||
const data =
|
||||
options?.search && emptyOnSearch.current
|
||||
? []
|
||||
: sessionsData.filter((session) => directories.includes(session.location.directory))
|
||||
return { data, isLoading: false, isError: false, hasNextPage: sessionsHasNextPage.current }
|
||||
return {
|
||||
data,
|
||||
isLoading: false,
|
||||
isError: Boolean(options?.search) && searchError.current,
|
||||
hasNextPage: sessionsHasNextPage.current,
|
||||
}
|
||||
},
|
||||
useCreateSession: (directory?: string) => ({
|
||||
mutate: (vars?: unknown) => {
|
||||
@@ -161,6 +168,7 @@ describe('DesktopSessionTree', () => {
|
||||
getSessionMock.mockResolvedValue(undefined)
|
||||
sessionPinsData.current = []
|
||||
sessionsHasNextPage.current = false
|
||||
searchError.current = false
|
||||
})
|
||||
|
||||
it('lists every repo by last access, even inside a repo, with no Recent section', async () => {
|
||||
@@ -302,6 +310,9 @@ describe('DesktopSessionTree', () => {
|
||||
await user.type(input, 'Session')
|
||||
|
||||
expect(await screen.findByRole('button', { name: /^Session a1/ })).toBeTruthy()
|
||||
await waitFor(() =>
|
||||
expect(sessionsHookCalls.some((call) => call.options?.search === 'Session')).toBe(true),
|
||||
)
|
||||
const searchCall = sessionsHookCalls.find((call) => call.options?.search === 'Session')
|
||||
expect(searchCall?.directories).toEqual(['/repos/a', '/repos/b'])
|
||||
expect(sessionsHookCalls.some((call) => call.options?.search === 'Sess')).toBe(false)
|
||||
@@ -315,6 +326,19 @@ describe('DesktopSessionTree', () => {
|
||||
expect(toggleFor('Alpha')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('filters loaded sessions by title immediately', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
const input = await screen.findByLabelText('Search sessions')
|
||||
await screen.findByText('Alpha')
|
||||
await user.type(input, 'a1')
|
||||
|
||||
expect(await screen.findByRole('button', { name: /^Session a1/ })).toBeTruthy()
|
||||
expect(screen.queryByRole('button', { name: /^Session b1/ })).toBeNull()
|
||||
expect(screen.queryByRole('button', { name: /^Session c1/ })).toBeNull()
|
||||
})
|
||||
|
||||
it('clears an active search with the clear button', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
|
||||
@@ -340,6 +364,7 @@ describe('DesktopSessionTree', () => {
|
||||
|
||||
expect(await screen.findByRole('button', { name: /^Session b1/ })).toBeTruthy()
|
||||
expect(screen.queryByRole('button', { name: /^Session a1/ })).toBeNull()
|
||||
await waitFor(() => expect(sessionsHookCalls.some((call) => call.options?.search === 'bet')).toBe(true))
|
||||
const searchCall = sessionsHookCalls.find((call) => call.options?.search === 'bet')
|
||||
expect(searchCall?.directories).toEqual(['/repos/c', '/repos/a'])
|
||||
})
|
||||
@@ -369,6 +394,17 @@ describe('DesktopSessionTree', () => {
|
||||
expect(await screen.findByText('No sessions found')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('shows the error state when the session search fails', async () => {
|
||||
const user = userEvent.setup()
|
||||
searchError.current = true
|
||||
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
const input = await screen.findByLabelText('Search sessions')
|
||||
await user.type(input, 'Session')
|
||||
|
||||
expect(await screen.findByText('Failed to load sessions')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('creates a session in the repo full path', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import { useLocation, useNavigate } from 'react-router-dom'
|
||||
import { Search } from 'lucide-react'
|
||||
import type { Repo } from '@/api/types'
|
||||
import type { Repo, Session } from '@/api/types'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { useNavigableRepos, useSidebarRepoGroups } from '@/hooks/useSidebarRepoGroups'
|
||||
import { useSessionSearch } from '@/hooks/useSessionSearch'
|
||||
import { useSessionPins } from '@/hooks/useSessionPins'
|
||||
import {
|
||||
NewSessionButton,
|
||||
RepoNavGroup,
|
||||
@@ -12,12 +14,16 @@ import {
|
||||
SessionNavRow,
|
||||
SessionNavStatus,
|
||||
} from '@/components/navigation/RepoSessionNav'
|
||||
import { getActiveRepoId, isCurrentSessionItem, isRepoReady } from '@/components/navigation/sidebar-session-tree'
|
||||
import {
|
||||
SIDEBAR_SESSIONS_PER_REPO,
|
||||
buildSidebarRepoGroups,
|
||||
getActiveRepoId,
|
||||
isCurrentSessionItem,
|
||||
isRepoReady,
|
||||
} from '@/components/navigation/sidebar-session-tree'
|
||||
import { getRepoPath } from '@/lib/navigation'
|
||||
import { getRepoDisplayName } from '@/lib/utils'
|
||||
import { useDebouncedValue } from '@/hooks/useDebouncedValue'
|
||||
|
||||
const SESSION_SEARCH_DEBOUNCE_MS = 300
|
||||
import { buildPinnedSessionKeys } from '@/lib/sessionKey'
|
||||
|
||||
function SessionSearchInput({ value, onChange }: { value: string; onChange: (value: string) => void }) {
|
||||
const clear = () => onChange('')
|
||||
@@ -46,29 +52,37 @@ function SessionSearchInput({ value, onChange }: { value: string; onChange: (val
|
||||
)
|
||||
}
|
||||
|
||||
function SessionSearchResults({ repos, search }: { repos: Repo[]; search: string }) {
|
||||
interface SessionSearchResultsProps {
|
||||
nameMatchedRepos: Repo[]
|
||||
otherRepos: Repo[]
|
||||
sessions: Session[]
|
||||
isSearchLoading: boolean
|
||||
isSearchError: boolean
|
||||
}
|
||||
|
||||
function SessionSearchResults({
|
||||
nameMatchedRepos,
|
||||
otherRepos,
|
||||
sessions,
|
||||
isSearchLoading,
|
||||
isSearchError,
|
||||
}: SessionSearchResultsProps) {
|
||||
const navigate = useNavigate()
|
||||
const location = useLocation()
|
||||
const { nameMatchedRepos, otherRepos } = useMemo(() => {
|
||||
const query = search.toLowerCase()
|
||||
const readyRepos = repos.filter(isRepoReady)
|
||||
const isNameMatch = (repo: Repo) => getRepoDisplayName(repo).toLowerCase().includes(query)
|
||||
return {
|
||||
nameMatchedRepos: readyRepos.filter(isNameMatch),
|
||||
otherRepos: readyRepos.filter((repo) => !isNameMatch(repo)),
|
||||
}
|
||||
}, [repos, search])
|
||||
const nameMatches = useSidebarRepoGroups({ repos: nameMatchedRepos })
|
||||
const sessionMatches = useSidebarRepoGroups({ repos: otherRepos, search })
|
||||
const isLoading = nameMatches.isLoading || sessionMatches.isLoading
|
||||
const isError = nameMatches.isError || sessionMatches.isError
|
||||
const { data: sessionPins } = useSessionPins()
|
||||
const pinnedKeys = useMemo(() => buildPinnedSessionKeys(sessionPins ?? []), [sessionPins])
|
||||
const sessionGroups = useMemo(
|
||||
() => buildSidebarRepoGroups({ repos: otherRepos, sessions, pinnedKeys, now: Date.now() }),
|
||||
[otherRepos, sessions, pinnedKeys],
|
||||
)
|
||||
const matchingGroups = [
|
||||
...nameMatches.groups,
|
||||
...sessionMatches.groups.filter((group) => group.items.length > 0),
|
||||
...sessionGroups.filter((group) => group.items.length > 0),
|
||||
]
|
||||
|
||||
if (isLoading) return <SessionNavStatus>Loading sessions...</SessionNavStatus>
|
||||
if (isError) return <SessionNavStatus>Failed to load sessions</SessionNavStatus>
|
||||
if (nameMatches.isLoading || isSearchLoading) return <SessionNavStatus>Loading sessions...</SessionNavStatus>
|
||||
if (nameMatches.isError || isSearchError) return <SessionNavStatus>Failed to load sessions</SessionNavStatus>
|
||||
if (matchingGroups.length === 0) return <SessionNavStatus>No sessions found</SessionNavStatus>
|
||||
|
||||
return (
|
||||
@@ -101,23 +115,53 @@ function SessionSearchResults({ repos, search }: { repos: Repo[]; search: string
|
||||
export function DesktopSessionTree() {
|
||||
const navigate = useNavigate()
|
||||
const location = useLocation()
|
||||
const { repos, isLoading } = useNavigableRepos()
|
||||
const readyRepos = useMemo(() => repos.filter(isRepoReady), [repos])
|
||||
|
||||
const [draft, setDraft] = useState('')
|
||||
const trimmedDraft = draft.trim()
|
||||
const debouncedSearch = useDebouncedValue(trimmedDraft, SESSION_SEARCH_DEBOUNCE_MS)
|
||||
const search = trimmedDraft ? debouncedSearch : ''
|
||||
const { repos, isLoading } = useNavigableRepos()
|
||||
|
||||
const { nameMatchedRepos, otherRepos } = useMemo(() => {
|
||||
const query = trimmedDraft.toLowerCase()
|
||||
const isNameMatch = (repo: Repo) => getRepoDisplayName(repo).toLowerCase().includes(query)
|
||||
return {
|
||||
nameMatchedRepos: readyRepos.filter(isNameMatch),
|
||||
otherRepos: readyRepos.filter((repo) => !isNameMatch(repo)),
|
||||
}
|
||||
}, [readyRepos, trimmedDraft])
|
||||
|
||||
const otherDirectories = useMemo(() => otherRepos.map((repo) => repo.fullPath), [otherRepos])
|
||||
const searchDirectories = useMemo(
|
||||
() => (trimmedDraft ? otherDirectories : []),
|
||||
[trimmedDraft, otherDirectories],
|
||||
)
|
||||
const { setQuery, filteredSessions, isLoading: isSearchLoading, isError: isSearchError } = useSessionSearch(
|
||||
searchDirectories,
|
||||
{ limit: SIDEBAR_SESSIONS_PER_REPO },
|
||||
)
|
||||
|
||||
const handleSearchChange = (value: string) => {
|
||||
setDraft(value)
|
||||
setQuery(value)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex min-h-0 flex-1 flex-col">
|
||||
<div className="px-2 pb-2">
|
||||
<SessionSearchInput value={draft} onChange={setDraft} />
|
||||
<SessionSearchInput value={draft} onChange={handleSearchChange} />
|
||||
</div>
|
||||
|
||||
<div role="region" aria-label="Session navigator" className="min-h-0 flex-1 overflow-y-auto pb-2">
|
||||
{isLoading ? (
|
||||
<SessionNavStatus>Loading repos...</SessionNavStatus>
|
||||
) : search ? (
|
||||
<SessionSearchResults repos={repos} search={search} />
|
||||
) : trimmedDraft ? (
|
||||
<SessionSearchResults
|
||||
nameMatchedRepos={nameMatchedRepos}
|
||||
otherRepos={otherRepos}
|
||||
sessions={filteredSessions}
|
||||
isSearchLoading={isSearchLoading}
|
||||
isSearchError={isSearchError}
|
||||
/>
|
||||
) : (
|
||||
<RepoSessionNavList
|
||||
repos={repos}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import type { Repo, Session } from '@/api/types'
|
||||
import {
|
||||
buildRepoByDirectory,
|
||||
buildSidebarRepoGroups,
|
||||
getActiveRepoId,
|
||||
isCurrentSessionItem,
|
||||
@@ -112,6 +113,19 @@ describe('selectNavigableRepos', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('buildRepoByDirectory', () => {
|
||||
it('indexes each repo by full path', () => {
|
||||
const repoA = createRepo({ id: 1, fullPath: '/repos/a' })
|
||||
const repoB = createRepo({ id: 2, fullPath: '/repos/b' })
|
||||
|
||||
const byDirectory = buildRepoByDirectory([repoA, repoB])
|
||||
|
||||
expect(byDirectory.get('/repos/a')).toBe(repoA)
|
||||
expect(byDirectory.get('/repos/b')).toBe(repoB)
|
||||
expect(byDirectory.size).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('buildSidebarRepoGroups', () => {
|
||||
const repoA = createRepo({ id: 1, fullPath: '/repos/a', name: 'Alpha' })
|
||||
const repoB = createRepo({ id: 2, fullPath: '/repos/b', name: 'Beta' })
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Repo, Session } from '@/api/types'
|
||||
import { ASSISTANT_REPO_ID } from '@opencode-manager/shared/utils'
|
||||
import { getRepoBranchLabel, getRepoDisplayName } from '@/lib/utils'
|
||||
import { buildSessionKey } from '@/lib/sessionKey'
|
||||
import { getSessionKey } from '@/lib/sessionKey'
|
||||
import { getSessionPath, isAssistantPath, parseRepoRoute } from '@/lib/navigation'
|
||||
import { partitionSessions, selectRootSessions } from '@/components/session/session-partition'
|
||||
|
||||
@@ -46,6 +46,10 @@ export function selectNavigableRepos(repos: Repo[]): Repo[] {
|
||||
)
|
||||
}
|
||||
|
||||
export function buildRepoByDirectory(repos: Repo[]): Map<string, Repo> {
|
||||
return new Map(repos.map((repo) => [repo.fullPath, repo]))
|
||||
}
|
||||
|
||||
export function buildSidebarRepoGroups(input: {
|
||||
repos: Repo[]
|
||||
sessions: Session[]
|
||||
@@ -53,8 +57,8 @@ export function buildSidebarRepoGroups(input: {
|
||||
now: number
|
||||
}): SidebarRepoGroup[] {
|
||||
const { repos, sessions, pinnedKeys, now } = input
|
||||
const keyFn = (session: Session) => buildSessionKey(session.location.directory, session.id)
|
||||
const repoByDirectory = new Map(repos.map((repo) => [repo.fullPath, repo]))
|
||||
const keyFn = getSessionKey
|
||||
const repoByDirectory = buildRepoByDirectory(repos)
|
||||
|
||||
const roots = selectRootSessions(sessions, {
|
||||
directories: new Set(repos.map((repo) => repo.fullPath)),
|
||||
|
||||
Reference in new issue
Block a user