mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
* feat(sidebar): add repo-grouped session navigation with live status - Add a searchable, collapsible session tree to the desktop sidebar, grouped by repository with pinned and time-bucketed ordering. - Track per-session busy, retry, and compact status in a shared store fed by the SSE event stream and scoped by directory. - Extract shared navigation components reused by the desktop tree and the mobile repo quick-switch sheet. - Show a Git tab and hide Schedules inside a repo on the mobile tab bar; add Repos and Assistant entries to the session More drawer. - Replace the CommitsTab local relative-time formatter with a shared formatShortRelativeTime util. * refactor(sidebar): unify session status tracking and navigation helpers * feat(sidebar): add collapsible sessions and menu sections * fix(sessions): keep specific live status over snapshot busy
82 lines
3.0 KiB
TypeScript
82 lines
3.0 KiB
TypeScript
import { useMemo } from 'react'
|
|
import { useQueries, useQuery } from '@tanstack/react-query'
|
|
import { listRepos } from '@/api/repos'
|
|
import { sessionQueryOptions, useSessionsAcrossDirectories } from '@/hooks/useOpenCode'
|
|
import { useSessionPins } from '@/hooks/useSessionPins'
|
|
import { buildPinnedSessionKeys, buildSessionKey } from '@/lib/sessionKey'
|
|
import type { Repo, Session } from '@/api/types'
|
|
import {
|
|
SIDEBAR_SESSIONS_PER_REPO,
|
|
buildSidebarRepoGroups,
|
|
selectNavigableRepos,
|
|
type SidebarRepoGroup,
|
|
} from '@/components/navigation/sidebar-session-tree'
|
|
|
|
const combinePinnedSessions = (results: Array<{ data?: Session }>): Session[] =>
|
|
results.flatMap((result) => (result.data ? [result.data] : []))
|
|
|
|
const mergeSessions = (sessions: Session[], pinnedSessions: Session[]): Session[] => {
|
|
if (pinnedSessions.length === 0) return sessions
|
|
const seen = new Set(sessions.map((session) => session.id))
|
|
const extras: Session[] = []
|
|
for (const session of pinnedSessions) {
|
|
if (seen.has(session.id)) continue
|
|
seen.add(session.id)
|
|
extras.push(session)
|
|
}
|
|
return extras.length === 0 ? sessions : [...sessions, ...extras]
|
|
}
|
|
|
|
export function useSidebarRepoGroups(input: {
|
|
repos: Repo[]
|
|
search?: string
|
|
}): { groups: SidebarRepoGroup[]; isLoading: boolean; isError: boolean; hasMore: boolean } {
|
|
const { repos, search } = input
|
|
|
|
const directories = useMemo(() => repos.map((repo) => repo.fullPath), [repos])
|
|
|
|
const { data: sessions, isLoading, isError, hasNextPage } = useSessionsAcrossDirectories(directories, {
|
|
search,
|
|
limit: SIDEBAR_SESSIONS_PER_REPO,
|
|
})
|
|
|
|
const { data: sessionPins } = useSessionPins()
|
|
const pinnedKeys = useMemo(() => buildPinnedSessionKeys(sessionPins ?? []), [sessionPins])
|
|
|
|
const missingPins = useMemo(() => {
|
|
if (search) return []
|
|
const repoDirectories = new Set(directories)
|
|
const fetchedKeys = new Set(
|
|
sessions.map((session) => buildSessionKey(session.location.directory, session.id)),
|
|
)
|
|
return (sessionPins ?? []).filter(
|
|
(pin) =>
|
|
repoDirectories.has(pin.directory) &&
|
|
!fetchedKeys.has(buildSessionKey(pin.directory, pin.sessionId)),
|
|
)
|
|
}, [directories, search, sessionPins, sessions])
|
|
|
|
const pinnedSessions = useQueries({
|
|
queries: missingPins.map((pin) => sessionQueryOptions(pin.sessionId, pin.directory)),
|
|
combine: combinePinnedSessions,
|
|
})
|
|
|
|
const mergedSessions = useMemo(
|
|
() => mergeSessions(sessions, pinnedSessions),
|
|
[sessions, pinnedSessions],
|
|
)
|
|
|
|
const groups = useMemo(
|
|
() => buildSidebarRepoGroups({ repos, sessions: mergedSessions, pinnedKeys, now: Date.now() }),
|
|
[repos, mergedSessions, pinnedKeys],
|
|
)
|
|
|
|
return { groups, isLoading, isError, hasMore: Boolean(hasNextPage) }
|
|
}
|
|
|
|
export function useNavigableRepos(enabled = true): { repos: Repo[]; isLoading: boolean; refetch: () => void } {
|
|
const { data, isLoading, refetch } = useQuery({ queryKey: ['repos'], queryFn: listRepos, enabled })
|
|
const repos = useMemo(() => selectNavigableRepos(data ?? []), [data])
|
|
return { repos, isLoading, refetch: () => { void refetch() } }
|
|
}
|