mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
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.
This commit is contained in:
1 parent
1efd1c6a3b
commit
c06075651d
2 files changed
+69
-39
No files matched your search
@@ -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(<DesktopSessionTree />, { 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(<DesktopSessionTree />, { 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(<DesktopSessionTree />, { 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(<DesktopSessionTree />, { 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(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
const input = await screen.findByLabelText('Search sessions')
|
||||
await user.type(input, 'nothing')
|
||||
|
||||
expect(await screen.findByText('No sessions found')).toBeTruthy()
|
||||
})
|
||||
|
||||
@@ -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<HTMLInputElement>) => {
|
||||
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 (
|
||||
<div className="relative">
|
||||
<Search className="absolute left-2 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
||||
<Input
|
||||
value={draft}
|
||||
onChange={(event) => 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) && <SearchClearButton onClear={clear} />}
|
||||
{value.length > 0 && <SearchClearButton onClear={clear} />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -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 <SessionNavStatus>Loading sessions...</SessionNavStatus>
|
||||
if (isError) return <SessionNavStatus>Failed to load sessions</SessionNavStatus>
|
||||
@@ -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 (
|
||||
<div className="flex min-h-0 flex-1 flex-col">
|
||||
<div className="px-2 pb-2">
|
||||
<SessionSearchInput
|
||||
hasActiveSearch={search.length > 0}
|
||||
onSubmit={setSearch}
|
||||
onClear={() => setSearch('')}
|
||||
/>
|
||||
<SessionSearchInput value={draft} onChange={setDraft} />
|
||||
</div>
|
||||
|
||||
<div role="region" aria-label="Session navigator" className="min-h-0 flex-1 overflow-y-auto pb-2">
|
||||
|
||||
Reference in new issue
Block a user