From c06075651dfeedce2b29462b260323655b4dfc87 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Mon, 5 Oct 2026 15:32:20 -0400 Subject: [PATCH] feat(sidebar): match repo names and debounce session search Session search now also matches repository names, showing a matching repo's recent sessions. Search runs automatically after a short debounce instead of requiring Enter, and emptying the input restores the full list immediately. --- .../navigation/DesktopSessionTree.test.tsx | 43 ++++++++++-- .../navigation/DesktopSessionTree.tsx | 65 ++++++++++--------- 2 files changed, 69 insertions(+), 39 deletions(-) diff --git a/frontend/src/components/navigation/DesktopSessionTree.test.tsx b/frontend/src/components/navigation/DesktopSessionTree.test.tsx index 0b21dc84..b50eb47d 100644 --- a/frontend/src/components/navigation/DesktopSessionTree.test.tsx +++ b/frontend/src/components/navigation/DesktopSessionTree.test.tsx @@ -292,18 +292,19 @@ describe('DesktopSessionTree', () => { expect(screen.getByTestId('location').textContent).toBe('/repos/4') }) - it('commits the search on Enter across ready repos and clears with Escape', async () => { + it('searches as you type across ready repos and clears with Escape', async () => { const user = userEvent.setup() listReposMock.mockResolvedValue([repoA, repoB, createRepo({ id: 4, fullPath: '/repos/d', name: 'Delta', cloneStatus: 'cloning' })]) render(, { wrapper: createWrapper(['/']) }) const input = await screen.findByLabelText('Search sessions') await screen.findByText('Alpha') - await user.type(input, 'Session{Enter}') + await user.type(input, 'Session') + expect(await screen.findByRole('button', { name: /^Session a1/ })).toBeTruthy() const searchCall = sessionsHookCalls.find((call) => call.options?.search === 'Session') expect(searchCall?.directories).toEqual(['/repos/a', '/repos/b']) - expect(await screen.findByRole('button', { name: /^Session a1/ })).toBeTruthy() + expect(sessionsHookCalls.some((call) => call.options?.search === 'Sess')).toBe(false) expect(screen.getByRole('button', { name: /^Session b1/ })).toBeTruthy() expect(screen.queryByRole('button', { name: /^Show sessions in / })).toBeNull() @@ -314,12 +315,12 @@ describe('DesktopSessionTree', () => { expect(toggleFor('Alpha')).toBeTruthy() }) - it('clears a committed search with the clear button', async () => { + it('clears an active search with the clear button', async () => { const user = userEvent.setup() render(, { wrapper: createWrapper(['/']) }) const input = await screen.findByLabelText('Search sessions') - await user.type(input, 'Session{Enter}') + await user.type(input, 'Session') await screen.findByRole('button', { name: /^Session a1/ }) await user.click(screen.getByLabelText('Clear search')) @@ -329,13 +330,41 @@ describe('DesktopSessionTree', () => { expect(toggleFor('Alpha')).toBeTruthy() }) - it('shows the empty search state when a committed search returns nothing', async () => { + it('matches repo names and shows their recent sessions', async () => { const user = userEvent.setup() emptyOnSearch.current = true render(, { wrapper: createWrapper(['/']) }) const input = await screen.findByLabelText('Search sessions') - await user.type(input, 'nothing{Enter}') + await user.type(input, 'bet') + + expect(await screen.findByRole('button', { name: /^Session b1/ })).toBeTruthy() + expect(screen.queryByRole('button', { name: /^Session a1/ })).toBeNull() + const searchCall = sessionsHookCalls.find((call) => call.options?.search === 'bet') + expect(searchCall?.directories).toEqual(['/repos/c', '/repos/a']) + }) + + it('restores the full list when the input is emptied', async () => { + const user = userEvent.setup() + render(, { wrapper: createWrapper(['/']) }) + + const input = await screen.findByLabelText('Search sessions') + await user.type(input, 'Session') + await screen.findByRole('button', { name: /^Session a1/ }) + + await user.clear(input) + + await waitFor(() => expect(screen.queryByRole('button', { name: /^Session / })).toBeNull()) + expect(toggleFor('Alpha')).toBeTruthy() + }) + + it('shows the empty search state when a search returns nothing', async () => { + const user = userEvent.setup() + emptyOnSearch.current = true + render(, { wrapper: createWrapper(['/']) }) + + const input = await screen.findByLabelText('Search sessions') + await user.type(input, 'nothing') expect(await screen.findByText('No sessions found')).toBeTruthy() }) diff --git a/frontend/src/components/navigation/DesktopSessionTree.tsx b/frontend/src/components/navigation/DesktopSessionTree.tsx index e4d1d369..98753d55 100644 --- a/frontend/src/components/navigation/DesktopSessionTree.tsx +++ b/frontend/src/components/navigation/DesktopSessionTree.tsx @@ -14,46 +14,34 @@ import { } from '@/components/navigation/RepoSessionNav' import { getActiveRepoId, isCurrentSessionItem, isRepoReady } from '@/components/navigation/sidebar-session-tree' import { getRepoPath } from '@/lib/navigation' +import { getRepoDisplayName } from '@/lib/utils' +import { useDebouncedValue } from '@/hooks/useDebouncedValue' -function SessionSearchInput({ - hasActiveSearch, - onSubmit, - onClear, -}: { - hasActiveSearch: boolean - onSubmit: (query: string) => void - onClear: () => void -}) { - const [draft, setDraft] = useState('') +const SESSION_SEARCH_DEBOUNCE_MS = 300 - const clear = () => { - setDraft('') - onClear() - } +function SessionSearchInput({ value, onChange }: { value: string; onChange: (value: string) => void }) { + const clear = () => onChange('') const handleKeyDown = (event: React.KeyboardEvent) => { - if (event.key === 'Enter') { - event.preventDefault() - onSubmit(draft.trim()) - } else if (event.key === 'Escape') { - clear() - } + if (event.key === 'Enter') event.preventDefault() + else if (event.key === 'Escape') clear() } return ( setDraft(event.target.value)} + value={value} + onChange={(event) => onChange(event.target.value)} onKeyDown={handleKeyDown} + enterKeyHint="search" aria-label="Search sessions" placeholder="Search sessions..." autoComplete="off" name="sidebar-session-search" className="h-8 pl-8 pr-9" /> - {(draft.length > 0 || hasActiveSearch) && } + {value.length > 0 && } ) } @@ -61,9 +49,23 @@ function SessionSearchInput({ function SessionSearchResults({ repos, search }: { repos: Repo[]; search: string }) { const navigate = useNavigate() const location = useLocation() - const searchableRepos = useMemo(() => repos.filter(isRepoReady), [repos]) - const { groups, isLoading, isError } = useSidebarRepoGroups({ repos: searchableRepos, search }) - const matchingGroups = groups.filter((group) => group.items.length > 0) + 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 matchingGroups = [ + ...nameMatches.groups, + ...sessionMatches.groups.filter((group) => group.items.length > 0), + ] if (isLoading) return Loading sessions... if (isError) return Failed to load sessions @@ -99,17 +101,16 @@ function SessionSearchResults({ repos, search }: { repos: Repo[]; search: string export function DesktopSessionTree() { const navigate = useNavigate() const location = useLocation() - const [search, setSearch] = useState('') + const [draft, setDraft] = useState('') + const trimmedDraft = draft.trim() + const debouncedSearch = useDebouncedValue(trimmedDraft, SESSION_SEARCH_DEBOUNCE_MS) + const search = trimmedDraft ? debouncedSearch : '' const { repos, isLoading } = useNavigableRepos() return ( - 0} - onSubmit={setSearch} - onClear={() => setSearch('')} - /> +