import { useEffect, useId, useMemo, useState } from 'react' import type { Repo } from '@/api/types' import { useNavigableRepos } from '@/hooks/useSidebarRepoGroups' import { LocalRepoNewSessionButton } from '@/components/navigation/RepoSessionNav' import { isRepoReady } from '@/components/navigation/sidebar-session-tree' import { cn, getRepoBranchLabel, getRepoDisplayName } from '@/lib/utils' import { PickerSearchBar, optionId, usePickerCursor, usePickerKeyNav, } from '@/components/session/usePickerKeyboardNav' interface ProjectPickerContentProps { open: boolean currentRepo: Repo | undefined onSelectRepo: (repo: Repo) => void onNavigate: (path: string) => void switchHint: string searchInputRef: React.RefObject searchTrailing?: React.ReactNode } interface ProjectPickerRow { key: string repo: Repo } export function ProjectPickerContent({ open, currentRepo, onSelectRepo, onNavigate, switchHint, searchInputRef, searchTrailing, }: ProjectPickerContentProps) { const { repos, isLoading } = useNavigableRepos() const listId = useId() const optionBaseId = useId() const [query, setQuery] = useState('') const trimmedQuery = query.trim() const filteredRepos = useMemo(() => { const needle = trimmedQuery.toLowerCase() if (!needle) return repos return repos.filter((repo) => { const name = getRepoDisplayName(repo).toLowerCase() const branch = (getRepoBranchLabel(repo) ?? '').toLowerCase() return name.includes(needle) || branch.includes(needle) }) }, [repos, trimmedQuery]) const rows = useMemo( () => filteredRepos.map((repo) => ({ key: String(repo.id), repo })), [filteredRepos], ) const defaultCursorKey = useMemo(() => { if (!trimmedQuery && currentRepo) { const current = rows.find((row) => row.repo.id === currentRepo.id) if (current) return current.key } return rows[0]?.key ?? null }, [trimmedQuery, currentRepo, rows]) const { cursorIndex, cursorRow, registerRef, moveCursor, resetCursor } = usePickerCursor( rows, defaultCursorKey, ) useEffect(() => { resetCursor() }, [trimmedQuery, resetCursor]) const handleSearchKeyDown = usePickerKeyNav({ rows, cursorIndex, cursorRow, moveCursor, onOpenRow: (row) => onSelectRepo(row.repo), }) return (
= 0 ? optionId(optionBaseId, cursorIndex) : undefined} value={query} onChange={setQuery} onKeyDown={handleSearchKeyDown} placeholder="Search projects..." name="project-picker-search" trailing={searchTrailing} />
{rows.length === 0 ? (
{isLoading ? 'Loading projects...' : 'No projects found'}
) : ( rows.map((row, index) => { const { repo } = row const isCursor = index === cursorIndex const isCurrent = repo.id === currentRepo?.id const branch = getRepoBranchLabel(repo) return (
registerRef(row.key, node)} role="presentation" onPointerMove={(event) => { if (event.pointerType === 'mouse') moveCursor(index) }} className={cn( 'group flex items-center rounded-md text-sm min-h-8 pointer-coarse:min-h-11', isCursor ? 'bg-primary text-primary-foreground' : 'hover:bg-accent', )} >
onSelectRepo(repo)} className="flex min-w-0 flex-1 cursor-pointer items-center gap-2 px-2" > {isCurrent && } {getRepoDisplayName(repo)} {branch && ( {branch} )}
{isRepoReady(repo) ? ( ) : ( Not ready )}
) }) )}
↑↓ move · Enter open {switchHint ? ` · ${switchHint} active` : ''}
) }