feat(worktree): worktree tabs, server env vars, and multi-directory SSE (#240)

* loop: worktree-tabs-2 completed after 4 iterations

* loop: server-env-vars completed after 7 iterations

* feat(worktree): add worktree tabs and server env vars settings

* refactor(worktree): multi-directory SSE support and workspace chip row

* feat(workspace): collapsible workspace manager and sibling filtering

* feat(workspace): workspace creation endpoint, refactored workspace manager, and tests

* refactor(frontend): unify workspace label and remove dead code

- extract shared workspaceLabel helper into api/repos and reuse in
  WorkspaceManager and RepoDetail directoryLabels
- remove unused useSessions hook and subscribeToDirectory subscription
- drop redundant frontend workspace dedup now guaranteed by backend
- align WorktreeTabs empty-state test with create-workspace button
This commit is contained in:
Chris Scott authored and GitHub committed 2026-05-30 19:44:07 -04:00
1 parent 60ed756a0b
commit b9236f7f20
38 files changed
+2423 -133

No files matched your search

@@ -12,6 +12,7 @@ interface SessionCardProps {
isSelected: boolean;
isActive: boolean;
manageMode: boolean;
workspaceLabel?: string;
onSelect: (sessionID: string) => void;
onToggleSelection: (selected: boolean) => void;
onDelete: (e: React.MouseEvent<HTMLButtonElement>) => void;
@@ -22,6 +23,7 @@ export const SessionCard = ({
isSelected,
isActive,
manageMode,
workspaceLabel,
onSelect,
onToggleSelection,
onDelete,
@@ -107,6 +109,9 @@ export const SessionCard = ({
addSuffix: true,
})}
</span>
{workspaceLabel ? (
<span className="text-purple-400 truncate max-w-[140px]">{workspaceLabel}</span>
) : null}
</div>
</div>
</div>
@@ -122,6 +127,9 @@ export const SessionCard = ({
addSuffix: true,
})}
</span>
{workspaceLabel ? (
<span className="text-purple-400 truncate max-w-[120px]">{workspaceLabel}</span>
) : null}
<SessionStatusIndicator sessionID={session.id} size="sm" />
</div>
</div>
@@ -0,0 +1,113 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { render, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { SessionList } from './SessionList'
const { createSessionMock, deleteSessionMock, sessionsData, createSessionState } = vi.hoisted(() => ({
createSessionMock: vi.fn(),
deleteSessionMock: vi.fn(),
sessionsData: [] as Array<{ id: string; title: string; directory: string; workspaceID?: string; time: { updated: number } }>,
createSessionState: { directory: undefined as string | undefined },
}))
vi.mock('@/hooks/useOpenCode', () => ({
useSessionsAcrossDirectories: () => ({
data: sessionsData,
isLoading: false,
}),
useCreateSession: (_opcodeUrl: string, directory?: string) => {
createSessionState.directory = directory
return { mutate: createSessionMock }
},
useDeleteSession: () => ({ mutateAsync: deleteSessionMock, isPending: false }),
}))
describe('SessionList', () => {
beforeEach(() => {
sessionsData.splice(0, sessionsData.length,
{ id: 'ses_same', title: 'audit: mic-warmup 1/2 #2', directory: '/w/a', workspaceID: 'wrk_a', time: { updated: Date.now() } },
{ id: 'ses_same', title: 'audit: mic-warmup 1/2 #2', directory: '/w/b', workspaceID: 'wrk_b', time: { updated: Date.now() } },
{ id: 'ses_same', title: 'audit: mic-warmup 1/2 #2', directory: '/w/c', workspaceID: 'wrk_c', time: { updated: Date.now() } },
)
createSessionMock.mockReset()
createSessionState.directory = undefined
deleteSessionMock.mockReset()
deleteSessionMock.mockResolvedValue(undefined)
})
it('selects duplicate session IDs independently by workspace directory', async () => {
const user = userEvent.setup()
render(
<SessionList
opcodeUrl="/api/opencode"
directories={['/w/a', '/w/b', '/w/c']}
onSelectSession={vi.fn()}
/>,
)
await user.click(screen.getByRole('button', { name: 'Manage sessions' }))
await user.click(screen.getByRole('button', { name: 'Select All' }))
expect(screen.getByText('3 selected')).toBeTruthy()
await user.click(screen.getByRole('button', { name: 'Delete' }))
const dialog = screen.getByRole('dialog', { name: 'Delete Sessions' })
expect(dialog).toBeTruthy()
expect(screen.getByText('3')).toBeTruthy()
await user.click(within(dialog).getByRole('button', { name: 'Delete' }))
await waitFor(() => {
expect(deleteSessionMock).toHaveBeenCalledWith([
{ id: 'ses_same', directory: '/w/a', workspaceID: 'wrk_a' },
{ id: 'ses_same', directory: '/w/b', workspaceID: 'wrk_b' },
{ id: 'ses_same', directory: '/w/c', workspaceID: 'wrk_c' },
])
})
})
it('deduplicates repeated session records from the same workspace directory', async () => {
const user = userEvent.setup()
sessionsData.splice(0, sessionsData.length,
{ id: 'ses_same', title: 'audit: mic-warmup 1/2 #2', directory: '/w/a', time: { updated: Date.now() } },
{ id: 'ses_same', title: 'audit: mic-warmup 1/2 #2', directory: '/w/a', time: { updated: Date.now() } },
{ id: 'ses_same', title: 'audit: mic-warmup 1/2 #2', directory: '/w/a', time: { updated: Date.now() } },
)
render(
<SessionList
opcodeUrl="/api/opencode"
directories={['/w/a', '/w/a', '/w/a']}
onSelectSession={vi.fn()}
/>,
)
expect(screen.getAllByText('audit: mic-warmup 1/2 #2')).toHaveLength(1)
await user.click(screen.getByRole('button', { name: 'Manage sessions' }))
await user.click(screen.getByRole('button', { name: 'Select All' }))
expect(screen.getByText('1 selected')).toBeTruthy()
})
it('creates sessions in the explicit create directory', async () => {
const user = userEvent.setup()
sessionsData.splice(0, sessionsData.length)
render(
<SessionList
opcodeUrl="/api/opencode"
directories={['/w/a', '/w/b']}
createDirectory="/w/b"
onSelectSession={vi.fn()}
/>,
)
await user.click(screen.getByText('No sessions yet'))
expect(createSessionState.directory).toBe('/w/b')
expect(createSessionMock).toHaveBeenCalledWith({ agent: undefined })
})
})
+69 -25
View File
@@ -1,5 +1,6 @@
import { useState, useMemo } from "react";
import { useSessions, useDeleteSession, useCreateSession } from "@/hooks/useOpenCode";
import { useCallback, useState, useMemo } from "react";
import { useSessionsAcrossDirectories, useDeleteSession, useCreateSession } from "@/hooks/useOpenCode";
import type { DeleteSessionTarget } from "@/hooks/useOpenCode";
import { DeleteSessionDialog } from "./DeleteSessionDialog";
import { SessionCard } from "./SessionCard";
import { Card } from "@/components/ui/card";
@@ -10,6 +11,9 @@ import { Search, Trash2, Pencil, X } from "lucide-react";
interface SessionListProps {
opcodeUrl: string;
directory?: string;
directories?: string[];
createDirectory?: string;
directoryLabels?: Record<string, string>;
activeSessionID?: string;
onSelectSession: (sessionID: string) => void;
}
@@ -17,16 +21,29 @@ interface SessionListProps {
export const SessionList = ({
opcodeUrl,
directory,
directories,
createDirectory,
directoryLabels,
activeSessionID,
onSelectSession,
}: SessionListProps) => {
const { data: sessions, isLoading } = useSessions(opcodeUrl, directory);
const deleteSession = useDeleteSession(opcodeUrl, directory);
const createSession = useCreateSession(opcodeUrl, directory, (newSession) => {
const directoriesList = useMemo(() => {
const source = directories && directories.length > 0 ? directories : directory ? [directory] : [];
return Array.from(new Set(source.filter(Boolean)));
}, [directory, directories]);
const directorySet = useMemo(() => new Set(directoriesList), [directoriesList]);
const primaryDirectory = directoriesList[0];
const sessionCreateDirectory = createDirectory ?? primaryDirectory;
const getSessionSelectionKey = useCallback((session: { id: string; directory?: string }) =>
`${session.directory ?? primaryDirectory ?? ''}:${session.id}`,
[primaryDirectory]);
const { data: sessions, isLoading } = useSessionsAcrossDirectories(opcodeUrl, directoriesList);
const deleteSession = useDeleteSession(opcodeUrl, directoriesList);
const createSession = useCreateSession(opcodeUrl, sessionCreateDirectory, (newSession) => {
onSelectSession(newSession.id);
});
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const [sessionToDelete, setSessionToDelete] = useState<string | string[] | null>(null);
const [sessionToDelete, setSessionToDelete] = useState<DeleteSessionTarget | DeleteSessionTarget[] | null>(null);
const [searchQuery, setSearchQuery] = useState("");
const [selectedSessions, setSelectedSessions] = useState<Set<string>>(new Set());
const [manageMode, setManageMode] = useState(false);
@@ -36,7 +53,7 @@ export const SessionList = ({
let filtered = sessions.filter((session) => {
if (session.parentID) return false;
if (directory && session.directory && session.directory !== directory) return false;
if (directorySet.size > 0 && session.directory && !directorySet.has(session.directory)) return false;
return true;
});
@@ -47,8 +64,16 @@ export const SessionList = ({
);
}
return filtered.sort((a, b) => b.time.updated - a.time.updated);
}, [sessions, searchQuery, directory]);
const uniqueSessions = new Map<string, (typeof filtered)[number]>();
filtered.forEach((session) => {
const key = getSessionSelectionKey(session);
if (!uniqueSessions.has(key)) {
uniqueSessions.set(key, session);
}
});
return Array.from(uniqueSessions.values()).sort((a, b) => b.time.updated - a.time.updated);
}, [sessions, searchQuery, directorySet, getSessionSelectionKey]);
const todaySessions = useMemo(() => {
const today = new Date();
@@ -84,9 +109,20 @@ export const SessionList = ({
);
}
const handleDelete = (sessionId: string, e: React.MouseEvent<HTMLButtonElement>) => {
const getDeleteTarget = (session: { id: string; directory?: string; workspaceID?: string }): DeleteSessionTarget => {
const target: Extract<DeleteSessionTarget, { id: string }> = {
id: session.id,
directory: session.directory ?? primaryDirectory,
};
if (session.workspaceID) {
target.workspaceID = session.workspaceID;
}
return target;
};
const handleDelete = (session: { id: string; directory?: string; workspaceID?: string }, e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
setSessionToDelete(sessionId);
setSessionToDelete(getDeleteTarget(session));
setDeleteDialogOpen(true);
};
@@ -107,31 +143,36 @@ export const SessionList = ({
setManageMode(false);
};
const toggleSessionSelection = (sessionId: string, selected: boolean) => {
const toggleSessionSelection = (session: { id: string; directory?: string }, selected: boolean) => {
const selectionKey = getSessionSelectionKey(session);
const newSelected = new Set(selectedSessions);
if (selected) {
newSelected.add(sessionId);
newSelected.add(selectionKey);
} else {
newSelected.delete(sessionId);
newSelected.delete(selectionKey);
}
setSelectedSessions(newSelected);
};
const allVisibleSelected =
filteredSessions.length > 0 &&
filteredSessions.every((s) => selectedSessions.has(s.id));
filteredSessions.every((session) => selectedSessions.has(getSessionSelectionKey(session)));
const toggleSelectAll = () => {
if (allVisibleSelected) {
setSelectedSessions(new Set());
} else {
setSelectedSessions(new Set(filteredSessions.map((s) => s.id)));
setSelectedSessions(new Set(filteredSessions.map(getSessionSelectionKey)));
}
};
const handleBulkDelete = () => {
if (selectedSessions.size > 0) {
setSessionToDelete(Array.from(selectedSessions));
const selectedTargets = filteredSessions
.filter((session) => selectedSessions.has(getSessionSelectionKey(session)))
.map(getDeleteTarget);
if (selectedTargets.length === 0) return;
setSessionToDelete(selectedTargets);
setDeleteDialogOpen(true);
}
};
@@ -183,6 +224,7 @@ export const SessionList = ({
<Button
variant="outline"
size="icon"
aria-label="Manage sessions"
className="shrink-0 size-9"
onClick={() => {
setManageMode(true);
@@ -209,14 +251,15 @@ export const SessionList = ({
</div>
{todaySessions.map((session) => (
<SessionCard
key={session.id}
key={getSessionSelectionKey(session)}
session={session}
isSelected={selectedSessions.has(session.id)}
isSelected={selectedSessions.has(getSessionSelectionKey(session))}
isActive={activeSessionID === session.id}
manageMode={manageMode}
workspaceLabel={session.directory ? directoryLabels?.[session.directory] : undefined}
onSelect={onSelectSession}
onToggleSelection={(selected) => toggleSessionSelection(session.id, selected)}
onDelete={(e) => handleDelete(session.id, e)}
onToggleSelection={(selected) => toggleSessionSelection(session, selected)}
onDelete={(e) => handleDelete(session, e)}
/>
))}
</>
@@ -227,14 +270,15 @@ export const SessionList = ({
)}
{olderSessions.map((session) => (
<SessionCard
key={session.id}
key={getSessionSelectionKey(session)}
session={session}
isSelected={selectedSessions.has(session.id)}
isSelected={selectedSessions.has(getSessionSelectionKey(session))}
isActive={activeSessionID === session.id}
manageMode={manageMode}
workspaceLabel={session.directory ? directoryLabels?.[session.directory] : undefined}
onSelect={onSelectSession}
onToggleSelection={(selected) => toggleSessionSelection(session.id, selected)}
onDelete={(e) => handleDelete(session.id, e)}
onToggleSelection={(selected) => toggleSessionSelection(session, selected)}
onDelete={(e) => handleDelete(session, e)}
/>
))}
</>