import { useLocation } from 'react-router-dom'
import { CircleAlert, CircleCheck, CircleX } from 'lucide-react'
import { useActiveWork } from '@/hooks/useActiveWork'
import { SessionStatusIndicator } from '@/components/ui/session-status-indicator'
import { parseRepoRoute, parseServerSessionRoute } from '@/lib/navigation'
import { cn, formatShortRelativeTime, getRepoBranchLabel, getRepoDisplayName } from '@/lib/utils'
import {
ACTIVE_WORK_GROUPS,
MS_PER_MINUTE,
formatActivityEmptyMessage,
type ActiveWorkItem,
} from '@/lib/activeWork'
const UNTITLED_SESSION_TITLE = 'Untitled Session'
const COMPACT_MAX_ITEMS = 5
const FULL_ICON_CLASS = 'h-4 w-4'
const COMPACT_ICON_CLASS = 'h-3.5 w-3.5'
interface ActivityListProps {
/** Activity window in minutes. */
minutes: number
/** Called when a row is chosen; the caller owns navigation so each surface can apply its own history and close rules. */
onNavigate: (item: ActiveWorkItem) => void
hideWhenEmpty?: boolean
title?: string
/** `compact` renders one flat, single-line list capped at {@link COMPACT_MAX_ITEMS}; `full` renders grouped rows. */
variant?: 'full' | 'compact'
}
function isCurrentItem(
item: ActiveWorkItem,
sessionID: string | null,
repoID: number | null,
serverID: string | null,
) {
if (item.session.id !== sessionID) return false
if (item.serverId) return item.serverId === serverID
return item.repo?.id === repoID
}
/** The row's short label: the local repo's name or the owning linked server's name. */
function activityItemLabel(item: ActiveWorkItem): string {
return item.repo ? getRepoDisplayName(item.repo) : item.serverName ?? item.serverId ?? ''
}
/** The row's identifying line: a local repo with its branch, or the owning linked server's name. */
function activityItemLine(item: ActiveWorkItem): string {
if (!item.repo) return activityItemLabel(item)
const branch = item.worktreeBranch ?? getRepoBranchLabel(item.repo)
return branch ? `${getRepoDisplayName(item.repo)} ยท ${branch}` : getRepoDisplayName(item.repo)
}
function activityRowClass(isCurrent: boolean, compact: boolean) {
return cn(
'flex w-full items-center gap-2 rounded-md px-2 text-left text-sm transition-colors hover:bg-accent focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring',
compact ? 'py-1' : 'py-1.5',
isCurrent && 'bg-accent',
)
}
function ActivityStateIcon({ item, iconClassName }: { item: ActiveWorkItem; iconClassName: string }) {
if (item.state === 'needs-input') {
return
}
if (item.state === 'running') {
return
Loading activity...
} if (isError) { if (hideWhenEmpty) return null returnCouldn't load activity.
} const isEmpty = groups.needsInput.length === 0 && groups.running.length === 0 && groups.recent.length === 0 if (isEmpty) { if (hideWhenEmpty) return null return ({formatActivityEmptyMessage(minutes)}
) } const isCompact = variant === 'compact' const compactItems = [...groups.needsInput, ...groups.running, ...groups.recent].slice( 0, COMPACT_MAX_ITEMS, ) return (