import { useMemo, useState } from 'react' import { useLocation, useNavigate } from 'react-router-dom' import { Search } from 'lucide-react' 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, RepoSessionNavList, SearchClearButton, SessionNavRow, SessionNavStatus, } from '@/components/navigation/RepoSessionNav' 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 { buildPinnedSessionKeys } from '@/lib/sessionKey' function SessionSearchInput({ value, onChange }: { value: string; onChange: (value: string) => void }) { const clear = () => onChange('') const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter') event.preventDefault() else if (event.key === 'Escape') clear() } return (
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" /> {value.length > 0 && }
) } 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 nameMatches = useSidebarRepoGroups({ repos: nameMatchedRepos }) 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, ...sessionGroups.filter((group) => group.items.length > 0), ] if (nameMatches.isLoading || isSearchLoading) return Loading sessions... if (nameMatches.isError || isSearchError) return Failed to load sessions if (matchingGroups.length === 0) return No sessions found return ( <> {matchingGroups.map((group) => ( navigate(getRepoPath(group.repo.id))} actions={} > {group.items.map((item) => ( ))} ))} ) } 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 { 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 (
{isLoading ? ( Loading repos... ) : trimmedDraft ? ( ) : ( navigate(getRepoPath(repoId))} onSelectSession={navigate} renderActions={(repo) => } /> )}
) }