mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
The Servers section becomes a Local/server switcher shared by the desktop sidebar, collapsed rail and mobile repo sheet. Selecting a linked server lists its projects and sessions with the same navigation as local repos, and a remote project page mirrors the local repo page.
89 lines
2.6 KiB
TypeScript
89 lines
2.6 KiB
TypeScript
import { useMemo, useState } from 'react'
|
|
import { useSessionsAcrossDirectories } from '@/hooks/useOpenCode'
|
|
import { useDebouncedValue } from '@/hooks/useDebouncedValue'
|
|
import { selectRootSessions } from '@/components/session/session-partition'
|
|
import { getSessionKey } from '@/lib/sessionKey'
|
|
|
|
const DEFAULT_LIMIT = 25
|
|
|
|
export interface UseSessionSearchOptions {
|
|
limit?: number
|
|
allDirectories?: boolean
|
|
project?: { id: string; subpath: string }
|
|
/** Searches a linked server instead of the route's server. */
|
|
serverId?: string
|
|
/** Seeds the search box, for callers that mount the hook after a query is already entered. */
|
|
initialQuery?: string
|
|
}
|
|
|
|
/**
|
|
* Owns the sessions-dialog search state: the raw query, its debounced server
|
|
* value, the root-session selection, and the immediate local title filter.
|
|
*/
|
|
export function useSessionSearch(directories: string[], options: UseSessionSearchOptions = {}) {
|
|
const limit = options.limit ?? DEFAULT_LIMIT
|
|
const allDirectories = options.allDirectories ?? false
|
|
const project = options.project
|
|
const serverId = options.serverId
|
|
const [query, setQuery] = useState(options.initialQuery ?? '')
|
|
const trimmedQuery = query.trim()
|
|
const debouncedQuery = useDebouncedValue(trimmedQuery, 150)
|
|
const search = trimmedQuery ? debouncedQuery : ''
|
|
const {
|
|
data: sessions,
|
|
isLoading,
|
|
isError,
|
|
isPlaceholderData,
|
|
fetchNextPage,
|
|
hasNextPage,
|
|
isFetchingNextPage,
|
|
isFetchNextPageError,
|
|
} = useSessionsAcrossDirectories(directories, {
|
|
search,
|
|
limit,
|
|
keepPreviousResults: true,
|
|
allDirectories,
|
|
project,
|
|
serverId,
|
|
})
|
|
|
|
const directorySet = useMemo(() => new Set(directories), [directories])
|
|
|
|
const rootSessions = useMemo(
|
|
() =>
|
|
selectRootSessions(sessions ?? [], {
|
|
directories: project ? undefined : directorySet,
|
|
keyFn: getSessionKey,
|
|
}),
|
|
[sessions, directorySet, project],
|
|
)
|
|
|
|
const filteredSessions = useMemo(() => {
|
|
if (!trimmedQuery) return rootSessions
|
|
const needle = trimmedQuery.toLowerCase()
|
|
return rootSessions.filter((session) => (session.title ?? '').toLowerCase().includes(needle))
|
|
}, [rootSessions, trimmedQuery])
|
|
|
|
const isSearchPending = trimmedQuery !== search || isPlaceholderData
|
|
const canFetchNextPage = Boolean(
|
|
hasNextPage && !isFetchingNextPage && !isFetchNextPageError && !isSearchPending,
|
|
)
|
|
|
|
return {
|
|
query,
|
|
setQuery,
|
|
trimmedQuery,
|
|
sessions,
|
|
filteredSessions,
|
|
isSearchPending,
|
|
isLoading,
|
|
isError,
|
|
isPlaceholderData,
|
|
fetchNextPage,
|
|
hasNextPage,
|
|
isFetchingNextPage,
|
|
isFetchNextPageError,
|
|
canFetchNextPage,
|
|
}
|
|
}
|