import { useEffect, useMemo, useState, type CSSProperties, type ReactNode } from 'react'
import { useLocation } from 'react-router-dom'
import { Bell, ChevronDown, ChevronRight, HelpCircle, Loader2, Pin, Plus, X } from 'lucide-react'
import { SessionStatusIndicator } from '@/components/ui/session-status-indicator'
import { RepoActivityIndicator } from '@/components/activity/RepoActivityIndicator'
import { useForms, usePermissions } from '@/contexts/EventContext'
import { useCreateSession } from '@/hooks/useOpenCode'
import { useRepoSiblings } from '@/hooks/useRepoSiblings'
import { useSidebarRepoGroups } from '@/hooks/useSidebarRepoGroups'
import { buildWorktreeActivity, type ActiveWorktree } from '@/lib/activeWork'
import { cn, formatShortRelativeTime, getRepoBranchLabel, getRepoDisplayName } from '@/lib/utils'
import { getRepoPath, getSessionPath } from '@/lib/navigation'
import {
isCurrentSessionItem,
isRepoReady,
type SidebarSessionItem,
} from '@/components/navigation/sidebar-session-tree'
import type { Repo } from '@/api/types'
const OPEN_REPO_HEADER_STYLE: CSSProperties = {
background: 'color-mix(in srgb, var(--color-primary) 15%, var(--color-card))',
}
function OpenRepoRail() {
return
}
interface RepoNavGroupProps {
name: string
branch?: string | null
isWorktree?: boolean
isOpen: boolean
isCurrent: boolean
onOpenRepo: () => void
onToggle?: () => void
indicator?: ReactNode
actions?: ReactNode
children?: ReactNode
}
export function RepoNavGroup({
name,
branch,
isWorktree,
isOpen,
isCurrent,
onOpenRepo,
onToggle,
indicator,
actions,
children,
}: RepoNavGroupProps) {
return (
{isOpen &&
}
{onToggle && (
)}
{actions &&
{actions}
}
{isOpen && children && (
{children}
)}
)
}
interface SessionNavRowProps {
item: SidebarSessionItem
isCurrent: boolean
onSelect: (path: string) => void
repoLabel?: string
}
export function SessionNavRow({ item, isCurrent, onSelect, repoLabel }: SessionNavRowProps) {
const { hasForSession: hasPermission } = usePermissions()
const { hasForSession: hasForm } = useForms()
const branchLabel = item.worktreeBranch ?? item.branchLabel
return (
)
}
export function SessionNavStatus({ children }: { children: ReactNode }) {
return {children}
}
export function SessionNavLink({ children, onClick }: { children: ReactNode; onClick: () => void }) {
return (
)
}
export function SearchClearButton({ onClear }: { onClear: () => void }) {
return (
)
}
interface RepoSessionListProps {
repo: Repo
onSelectSession: (path: string) => void
}
function RepoSessionList({ repo, onSelectSession }: RepoSessionListProps) {
const siblings = useRepoSiblings(repo.isWorktree ? undefined : repo.id)
const worktrees = useMemo(
() =>
repo.isWorktree
? undefined
: buildWorktreeActivity([repo], [{ data: siblings.data }]).filter(
(worktree) => !worktree.isSchedule,
),
[repo, siblings.data],
)
if (siblings.isLoading) return Loading sessions...
if (siblings.isError && siblings.data === undefined)
return Failed to load sessions
return
}
interface RepoSessionListItemsProps {
repo: Repo
worktrees?: ActiveWorktree[]
onSelectSession: (path: string) => void
}
function RepoSessionListItems({ repo, worktrees, onSelectSession }: RepoSessionListItemsProps) {
const location = useLocation()
const repos = useMemo(() => [repo], [repo])
const { groups, isLoading, isError, hasMore } = useSidebarRepoGroups({ repos, worktrees })
const items = groups[0]?.items ?? []
if (isLoading) return Loading sessions...
if (isError) return Failed to load sessions
if (items.length === 0) return No sessions
return (
<>
{items.map((item) => (
))}
{hasMore && (
onSelectSession(getRepoPath(repo.id))}>All sessions
)}
>
)
}
interface RepoSessionNavListProps {
repos: Repo[]
activeRepoId: number | null
isVisible: boolean
onOpenRepo: (repoId: number) => void
onSelectSession: (path: string) => void
renderActions?: (repo: Repo) => ReactNode
}
export function RepoSessionNavList({
repos,
activeRepoId,
isVisible,
onOpenRepo,
onSelectSession,
renderActions,
}: RepoSessionNavListProps) {
const [expandedRepoId, setExpandedRepoId] = useState(null)
useEffect(() => {
if (isVisible && activeRepoId !== null) setExpandedRepoId(activeRepoId)
}, [isVisible, activeRepoId])
return (
{repos.map((repo) => {
const isExpanded = expandedRepoId === repo.id
const ready = isRepoReady(repo)
return (
onOpenRepo(repo.id)}
onToggle={() => setExpandedRepoId((current) => (current === repo.id ? null : repo.id))}
indicator={}
actions={ready ? renderActions?.(repo) : undefined}
>
{ready ? (
) : (
Repository not ready
)}
)
})}
)
}
interface NewSessionButtonProps {
directory: string
label: string
onOpenSession: (path: string) => void
sessionPath: (sessionId: string) => string
serverId?: string
}
export function NewSessionButton({
directory,
label,
onOpenSession,
sessionPath,
serverId,
}: NewSessionButtonProps) {
const createSession = useCreateSession(
directory,
(session) => {
onOpenSession(sessionPath(session.id))
},
serverId,
)
const isCreating = createSession.isPending
return (
)
}
interface LocalRepoNewSessionButtonProps {
repo: Repo
onOpenSession: (path: string) => void
}
/**
* Creates a session in a local repository, deriving the session directory, accessible label, and
* session route from the repository so callers only choose what to render and where to navigate.
*/
export function LocalRepoNewSessionButton({ repo, onOpenSession }: LocalRepoNewSessionButtonProps) {
return (
getSessionPath(repo.id, sessionId)}
onOpenSession={onOpenSession}
/>
)
}