fix(sessions): include worktree sessions in the session picker

This commit is contained in:
Chris Scott committed 2026-10-08 23:49:09 -04:00
1 parent d9843a0ff7
commit 08c03235c8
2 files changed
+58 -2

No files matched your search

@@ -14,6 +14,7 @@ const mocks = vi.hoisted(() => ({
fetchNextPage: vi.fn(),
deleteSessionMock: vi.fn(),
togglePinMock: vi.fn(),
siblings: [] as Array<Repo & { worktreeSource?: 'opencode' | 'schedule' | 'git' }>,
sessionPins: [] as Array<{ sessionId: string; directory: string; pinnedAt: number }>,
repos: [] as Repo[],
}))
@@ -47,6 +48,10 @@ vi.mock('@/hooks/useOpenCode', async (importOriginal) => {
}
})
vi.mock('@/hooks/useRepoSiblings', () => ({
useRepoSiblings: () => ({ data: mocks.siblings }),
}))
vi.mock('@/hooks/useSessionPins', () => ({
useSessionPins: () => ({ data: mocks.sessionPins }),
useToggleSessionPin: () => ({ mutate: mocks.togglePinMock }),
@@ -85,6 +90,10 @@ function repo(id: number, name: string, fullPath: string): Repo {
}
}
function worktreeSibling(id: number, name: string, fullPath: string): Repo & { worktreeSource: 'opencode' } {
return { ...repo(id, name, fullPath), isWorktree: true, worktreeSource: 'opencode' }
}
function renderPicker(overrides: Partial<React.ComponentProps<typeof SessionPickerDialog>> = {}) {
const onOpenChange = vi.fn()
const onSelectSession = vi.fn()
@@ -110,6 +119,7 @@ describe('SessionPickerDialog', () => {
stubMatchMedia(true)
localStorage.clear()
mocks.deleteSessionMock.mockResolvedValue(undefined)
mocks.siblings = []
mocks.sessionPins = []
mocks.repos = [repo(1, 'alpha', '/w/a'), repo(2, 'beta', '/w/b')]
mocks.sessionsData = []
@@ -212,6 +222,26 @@ describe('SessionPickerDialog', () => {
expect(localStorage.getItem('oc:session-picker:all-projects')).toBe('false')
})
it('includes the repo worktrees and labels worktree sessions with the folder name', () => {
mocks.repos = [repo(1, 'alpha', '/w/a')]
mocks.siblings = [
repo(1, 'alpha', '/w/a'),
worktreeSibling(2, 'alpha', '/w/worktrees/alpha-feature'),
]
mocks.sessionsData = [
session('ses_root', 'Root session', '/w/a', 2000),
session('ses_wt', 'Worktree session', '/w/worktrees/alpha-feature', 1000),
]
renderPicker()
expect(mocks.lastDirectories).toEqual(['/w/a', '/w/worktrees/alpha-feature'])
expect(screen.getByText('Worktree session')).toBeInTheDocument()
expect(screen.getByText('Root session')).toBeInTheDocument()
expect(screen.getByText('alpha-feature')).toBeInTheDocument()
expect(screen.getByRole('option', { name: /Root session/ }).textContent).not.toContain('alpha-feature')
})
it('does not query sessions while closed and queries after opening', async () => {
mocks.sessionsData = [session('ses_1', 'First', '/w/a', 3000)]
@@ -1,6 +1,7 @@
import { memo, useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'
import { format, startOfDay } from 'date-fns'
import { MoreHorizontal, Pin, PinOff, Search } from 'lucide-react'
import { isWorktreeSibling } from '@opencode-manager/shared/utils'
import type { Repo, Session } from '@/api/types'
import { Button } from '@/components/ui/button'
import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog'
@@ -19,6 +20,7 @@ import { useDeleteSession } from '@/hooks/useOpenCode'
import { useSessionSearch } from '@/hooks/useSessionSearch'
import { useSessionPins, useToggleSessionPin } from '@/hooks/useSessionPins'
import { usePersistentBoolean } from '@/hooks/usePersistentBoolean'
import { useRepoSiblings } from '@/hooks/useRepoSiblings'
import { useNavigableRepos } from '@/hooks/useSidebarRepoGroups'
import { useSessionStatusForSession } from '@/stores/sessionStatusStore'
import { buildPinnedSessionKeys, getSessionKey } from '@/lib/sessionKey'
@@ -91,6 +93,13 @@ function optionId(baseId: string, index: number): string {
return `${baseId}-option-${index}`
}
function worktreeDirectoryLabel(session: Session, repoPath: string | undefined): string | undefined {
const directory = session.location.directory
if (!repoPath || directory === repoPath) return undefined
const segments = directory.replace(/\/+$/, '').split('/').filter(Boolean)
return segments[segments.length - 1]
}
function SessionPickerGutter({ sessionID, isActive }: { sessionID: string; isActive: boolean }) {
const status = useSessionStatusForSession(sessionID)
if (status.type !== 'idle') {
@@ -117,6 +126,7 @@ interface SessionPickerRowItemProps {
isPinned: boolean
isPendingDelete: boolean
showRepo: boolean
label?: string
onOpen: (row: SessionPickerRow) => void
onHover: (index: number) => void
onTogglePin: (row: SessionPickerRow) => void
@@ -133,6 +143,7 @@ const SessionPickerRowItem = memo(function SessionPickerRowItem({
isPinned,
isPendingDelete,
showRepo,
label,
onOpen,
onHover,
onTogglePin,
@@ -190,6 +201,7 @@ const SessionPickerRowItem = memo(function SessionPickerRowItem({
)}
>
{showRepo && <span className="max-w-32 truncate">{getRepoDisplayName(row.repo)}</span>}
{!showRepo && label && <span className="max-w-32 truncate">{label}</span>}
<span>{formatShortRelativeTime(new Date(row.session.time.updated))}</span>
</span>
</div>
@@ -254,12 +266,25 @@ function SessionPickerContent({
return map
}, [repos, currentRepo])
const { data: siblings } = useRepoSiblings(currentRepo?.id)
const worktreeDirectories = useMemo(
() =>
(siblings ?? [])
.filter((sibling) => isWorktreeSibling(sibling) && !!sibling.fullPath)
.map((sibling) => sibling.fullPath),
[siblings],
)
const directories = useMemo(() => {
if (!allProjects) return currentRepo ? [currentRepo.fullPath] : []
if (!allProjects) {
if (!currentRepo) return []
return Array.from(new Set([currentRepo.fullPath, ...worktreeDirectories]))
}
const paths = repos.map((repo) => repo.fullPath)
if (currentRepo) paths.push(currentRepo.fullPath)
return Array.from(new Set(paths))
}, [allProjects, repos, currentRepo])
}, [allProjects, repos, currentRepo, worktreeDirectories])
const {
query,
@@ -562,6 +587,7 @@ function SessionPickerContent({
isPinned={pinnedKeys.has(row.key)}
isPendingDelete={pendingDeleteKey === row.key}
showRepo={allProjects}
label={allProjects ? undefined : worktreeDirectoryLabel(row.session, currentRepo?.fullPath)}
onOpen={openRow}
onHover={moveCursor}
onTogglePin={handleTogglePin}