diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 50cb64faa8..5c54c54fe3 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -4,7 +4,6 @@ import { ChevronRightIcon, FolderIcon, GitPullRequestIcon, - PinIcon, PlusIcon, SettingsIcon, SquarePenIcon, @@ -1214,6 +1213,32 @@ export default function Sidebar() { () => threads.filter((thread) => thread.archivedAt === null), [threads], ); + const threadStatusById = useMemo( + () => + new Map( + visibleThreads.map((thread) => [ + thread.id, + resolveThreadStatusPill({ + thread, + hasPendingApprovals: derivePendingApprovals(thread.activities).length > 0, + hasPendingUserInput: derivePendingUserInputs(thread.activities).length > 0, + }), + ]), + ), + [visibleThreads], + ); + const globalPinnedThreads = useMemo( + () => + sortThreadsForSidebar( + visibleThreads.filter((thread) => thread.pinned), + appSettings.sidebarThreadSortOrder, + ), + [appSettings.sidebarThreadSortOrder, visibleThreads], + ); + const globalPinnedThreadIds = useMemo( + () => globalPinnedThreads.map((thread) => thread.id), + [globalPinnedThreads], + ); const sortedProjects = useMemo( () => sortProjectsForSidebar(sidebarProjects, visibleThreads, appSettings.sidebarProjectSortOrder), @@ -1223,30 +1248,17 @@ export default function Sidebar() { const renderedProjects = useMemo( () => sortedProjects.map((project) => { - const projectThreads = sortThreadsForSidebar( + const allProjectThreads = sortThreadsForSidebar( visibleThreads.filter((thread) => thread.projectId === project.id), appSettings.sidebarThreadSortOrder, ); - const threadStatuses = new Map( - projectThreads.map((thread) => [ - thread.id, - resolveThreadStatusPill({ - thread, - hasPendingApprovals: derivePendingApprovals(thread.activities).length > 0, - hasPendingUserInput: derivePendingUserInputs(thread.activities).length > 0, - }), - ]), - ); + const projectThreads = allProjectThreads.filter((thread) => !thread.pinned); const projectStatus = resolveProjectStatusIndicator( - projectThreads.map((thread) => threadStatuses.get(thread.id) ?? null), + allProjectThreads.map((thread) => threadStatusById.get(thread.id) ?? null), ); const activeThreadId = routeThreadId ?? undefined; const isThreadListExpanded = expandedThreadListsByProject.has(project.id); - const pinnedCollapsedThread = - !project.expanded && activeThreadId - ? (projectThreads.find((thread) => thread.id === activeThreadId) ?? null) - : null; - const shouldShowThreadPanel = project.expanded || pinnedCollapsedThread !== null; + const shouldShowThreadPanel = project.expanded; const { hasHiddenThreads, hiddenThreads, @@ -1258,13 +1270,11 @@ export default function Sidebar() { previewLimit: THREAD_PREVIEW_LIMIT, }); const hiddenThreadStatus = resolveProjectStatusIndicator( - hiddenThreads.map((thread) => threadStatuses.get(thread.id) ?? null), + hiddenThreads.map((thread) => threadStatusById.get(thread.id) ?? null), ); const orderedProjectThreadIds = projectThreads.map((thread) => thread.id); - const renderedThreads = pinnedCollapsedThread - ? [pinnedCollapsedThread] - : visibleProjectThreads; - const showEmptyThreadState = project.expanded && projectThreads.length === 0; + const renderedThreads = visibleProjectThreads; + const showEmptyThreadState = project.expanded && allProjectThreads.length === 0; return { hasHiddenThreads, @@ -1272,8 +1282,6 @@ export default function Sidebar() { orderedProjectThreadIds, project, projectStatus, - projectThreads, - threadStatuses, renderedThreads, showEmptyThreadState, shouldShowThreadPanel, @@ -1285,12 +1293,13 @@ export default function Sidebar() { expandedThreadListsByProject, routeThreadId, sortedProjects, + threadStatusById, visibleThreads, ], ); const visibleSidebarThreadIds = useMemo( - () => getVisibleSidebarThreadIds(renderedProjects), - [renderedProjects], + () => [...globalPinnedThreadIds, ...getVisibleSidebarThreadIds(renderedProjects)], + [globalPinnedThreadIds, renderedProjects], ); const threadJumpCommandById = useMemo(() => { const mapping = new Map>>(); @@ -1407,6 +1416,258 @@ export default function Sidebar() { updateThreadJumpHintsVisibility, ]); + const renderThreadRow = ( + thread: SidebarThreadSnapshot, + options: { + leadingVisual?: "project-favicon"; + orderedThreadIds: readonly ThreadId[]; + }, + ) => { + const isActive = routeThreadId === thread.id; + const isSelected = selectedThreadIds.has(thread.id); + const isHighlighted = isActive || isSelected; + const jumpLabel = threadJumpLabelById.get(thread.id) ?? null; + const isThreadRunning = + thread.session?.status === "running" && thread.session.activeTurnId != null; + const threadStatus = threadStatusById.get(thread.id) ?? null; + const prStatus = prStatusIndicator(prByThreadId.get(thread.id) ?? null); + const terminalStatus = terminalStatusFromRunningIds( + selectThreadTerminalState(terminalStateByThreadId, thread.id).runningTerminalIds, + ); + const isConfirmingArchive = confirmingArchiveThreadId === thread.id && !isThreadRunning; + const threadMetaClassName = isConfirmingArchive + ? "pointer-events-none opacity-0" + : !isThreadRunning + ? "pointer-events-none transition-opacity duration-150 group-hover/menu-sub-item:opacity-0 group-focus-within/menu-sub-item:opacity-0" + : "pointer-events-none"; + const hoverActionClassName = + "inline-flex size-5 cursor-pointer items-center justify-center text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-ring"; + const hoverActionGroupClassName = + "pointer-events-none absolute top-1/2 right-1 flex -translate-y-1/2 items-center gap-0.5 opacity-0 transition-opacity duration-150 group-hover/menu-sub-item:pointer-events-auto group-hover/menu-sub-item:opacity-100 group-focus-within/menu-sub-item:pointer-events-auto group-focus-within/menu-sub-item:opacity-100"; + const projectCwd = projectCwdById.get(thread.projectId); + + return ( + { + setConfirmingArchiveThreadId((current) => (current === thread.id ? null : current)); + }} + onBlurCapture={(event) => { + const currentTarget = event.currentTarget; + requestAnimationFrame(() => { + if (currentTarget.contains(document.activeElement)) { + return; + } + setConfirmingArchiveThreadId((current) => (current === thread.id ? null : current)); + }); + }} + > + } + size="sm" + isActive={isActive} + data-testid={`thread-row-${thread.id}`} + className={`${resolveThreadRowClassName({ + isActive, + isSelected, + })} relative isolate`} + onClick={(event) => { + handleThreadClick(event, thread.id, options.orderedThreadIds); + }} + onKeyDown={(event) => { + if (event.key !== "Enter" && event.key !== " ") return; + event.preventDefault(); + navigateToThread(thread.id); + }} + onContextMenu={(event) => { + event.preventDefault(); + if (selectedThreadIds.size > 0 && selectedThreadIds.has(thread.id)) { + void handleMultiSelectContextMenu({ + x: event.clientX, + y: event.clientY, + }); + } else { + if (selectedThreadIds.size > 0) { + clearSelection(); + } + void handleThreadContextMenu(thread.id, { + x: event.clientX, + y: event.clientY, + }); + } + }} + > +
+ {options.leadingVisual === "project-favicon" ? ( + projectCwd ? ( + + ) : ( + + ) + ) : null} + {prStatus && ( + + { + openPrLink(event, prStatus.url); + }} + > + + + } + /> + {prStatus.tooltip} + + )} + {threadStatus && } + {renamingThreadId === thread.id ? ( + { + if (el && renamingInputRef.current !== el) { + renamingInputRef.current = el; + el.focus(); + el.select(); + } + }} + className="min-w-0 flex-1 truncate text-xs bg-transparent outline-none border border-ring rounded px-0.5" + value={renamingTitle} + onChange={(e) => setRenamingTitle(e.target.value)} + onKeyDown={(e) => { + e.stopPropagation(); + if (e.key === "Enter") { + e.preventDefault(); + renamingCommittedRef.current = true; + void commitRename(thread.id, renamingTitle, thread.title); + } else if (e.key === "Escape") { + e.preventDefault(); + renamingCommittedRef.current = true; + cancelRename(); + } + }} + onBlur={() => { + if (!renamingCommittedRef.current) { + void commitRename(thread.id, renamingTitle, thread.title); + } + }} + onClick={(e) => e.stopPropagation()} + /> + ) : ( + {thread.title} + )} +
+
+ {terminalStatus && ( + + + + )} +
+ {isConfirmingArchive ? ( + + ) : null} + {!isConfirmingArchive ? ( +
+ {!isThreadRunning ? ( + + { + event.preventDefault(); + event.stopPropagation(); + }} + onClick={(event) => { + event.preventDefault(); + event.stopPropagation(); + if (appSettings.confirmThreadArchive) { + setConfirmingArchiveThreadId(thread.id); + requestAnimationFrame(() => { + confirmArchiveButtonRefs.current.get(thread.id)?.focus(); + }); + return; + } + void attemptArchiveThread(thread.id); + }} + > + + + } + /> + Archive + + ) : null} +
+ ) : null} + + {showThreadJumpHints && jumpLabel ? ( + + {jumpLabel} + + ) : ( + + {formatRelativeTimeLabel(thread.updatedAt ?? thread.createdAt)} + + )} + +
+
+
+
+ ); + }; + function renderProjectItem( renderedProject: (typeof renderedProjects)[number], dragHandleProps: SortableProjectHandleProps | null, @@ -1417,283 +1678,11 @@ export default function Sidebar() { orderedProjectThreadIds, project, projectStatus, - projectThreads, - threadStatuses, renderedThreads, showEmptyThreadState, shouldShowThreadPanel, isThreadListExpanded, } = renderedProject; - const renderThreadRow = (thread: (typeof projectThreads)[number]) => { - const isActive = routeThreadId === thread.id; - const isSelected = selectedThreadIds.has(thread.id); - const isHighlighted = isActive || isSelected; - const jumpLabel = threadJumpLabelById.get(thread.id) ?? null; - const isThreadRunning = - thread.session?.status === "running" && thread.session.activeTurnId != null; - const threadStatus = threadStatuses.get(thread.id) ?? null; - const prStatus = prStatusIndicator(prByThreadId.get(thread.id) ?? null); - const terminalStatus = terminalStatusFromRunningIds( - selectThreadTerminalState(terminalStateByThreadId, thread.id).runningTerminalIds, - ); - const isConfirmingArchive = confirmingArchiveThreadId === thread.id && !isThreadRunning; - const threadMetaClassName = isConfirmingArchive - ? "pointer-events-none opacity-0" - : !isThreadRunning - ? "pointer-events-none transition-opacity duration-150 group-hover/menu-sub-item:opacity-0 group-focus-within/menu-sub-item:opacity-0" - : "pointer-events-none"; - const hoverActionClassName = - "inline-flex size-5 cursor-pointer items-center justify-center text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-ring"; - const hoverActionGroupClassName = - "pointer-events-none absolute top-1/2 right-1 flex -translate-y-1/2 items-center gap-0.5 opacity-0 transition-opacity duration-150 group-hover/menu-sub-item:pointer-events-auto group-hover/menu-sub-item:opacity-100 group-focus-within/menu-sub-item:pointer-events-auto group-focus-within/menu-sub-item:opacity-100"; - const pinActionLabel = thread.pinned ? "Unpin" : "Pin"; - - return ( - { - setConfirmingArchiveThreadId((current) => (current === thread.id ? null : current)); - }} - onBlurCapture={(event) => { - const currentTarget = event.currentTarget; - requestAnimationFrame(() => { - if (currentTarget.contains(document.activeElement)) { - return; - } - setConfirmingArchiveThreadId((current) => (current === thread.id ? null : current)); - }); - }} - > - } - size="sm" - isActive={isActive} - data-testid={`thread-row-${thread.id}`} - className={`${resolveThreadRowClassName({ - isActive, - isSelected, - })} relative isolate`} - onClick={(event) => { - handleThreadClick(event, thread.id, orderedProjectThreadIds); - }} - onKeyDown={(event) => { - if (event.key !== "Enter" && event.key !== " ") return; - event.preventDefault(); - navigateToThread(thread.id); - }} - onContextMenu={(event) => { - event.preventDefault(); - if (selectedThreadIds.size > 0 && selectedThreadIds.has(thread.id)) { - void handleMultiSelectContextMenu({ - x: event.clientX, - y: event.clientY, - }); - } else { - if (selectedThreadIds.size > 0) { - clearSelection(); - } - void handleThreadContextMenu(thread.id, { - x: event.clientX, - y: event.clientY, - }); - } - }} - > -
- {thread.pinned ? ( - - - - ) : null} - {prStatus && ( - - { - openPrLink(event, prStatus.url); - }} - > - - - } - /> - {prStatus.tooltip} - - )} - {threadStatus && } - {renamingThreadId === thread.id ? ( - { - if (el && renamingInputRef.current !== el) { - renamingInputRef.current = el; - el.focus(); - el.select(); - } - }} - className="min-w-0 flex-1 truncate text-xs bg-transparent outline-none border border-ring rounded px-0.5" - value={renamingTitle} - onChange={(e) => setRenamingTitle(e.target.value)} - onKeyDown={(e) => { - e.stopPropagation(); - if (e.key === "Enter") { - e.preventDefault(); - renamingCommittedRef.current = true; - void commitRename(thread.id, renamingTitle, thread.title); - } else if (e.key === "Escape") { - e.preventDefault(); - renamingCommittedRef.current = true; - cancelRename(); - } - }} - onBlur={() => { - if (!renamingCommittedRef.current) { - void commitRename(thread.id, renamingTitle, thread.title); - } - }} - onClick={(e) => e.stopPropagation()} - /> - ) : ( - {thread.title} - )} -
-
- {terminalStatus && ( - - - - )} -
- {isConfirmingArchive ? ( - - ) : null} - {!isConfirmingArchive ? ( -
- - { - event.preventDefault(); - event.stopPropagation(); - }} - onClick={(event) => { - event.preventDefault(); - event.stopPropagation(); - void setThreadPinned(thread, !thread.pinned); - }} - > - - - } - /> - {pinActionLabel} - - {!isThreadRunning ? ( - - { - event.preventDefault(); - event.stopPropagation(); - }} - onClick={(event) => { - event.preventDefault(); - event.stopPropagation(); - if (appSettings.confirmThreadArchive) { - setConfirmingArchiveThreadId(thread.id); - requestAnimationFrame(() => { - confirmArchiveButtonRefs.current.get(thread.id)?.focus(); - }); - return; - } - void attemptArchiveThread(thread.id); - }} - > - - - } - /> - Archive - - ) : null} -
- ) : null} - - {showThreadJumpHints && jumpLabel ? ( - - {jumpLabel} - - ) : ( - - {formatRelativeTimeLabel(thread.updatedAt ?? thread.createdAt)} - - )} - -
-
-
-
- ); - }; return ( <> @@ -1741,18 +1730,6 @@ export default function Sidebar() { /> )} - {project.pinned ? ( - - - - ) : null} {project.name} @@ -1808,7 +1785,10 @@ export default function Sidebar() { ) : null} - {shouldShowThreadPanel && renderedThreads.map((thread) => renderThreadRow(thread))} + {shouldShowThreadPanel && + renderedThreads.map((thread) => + renderThreadRow(thread, { orderedThreadIds: orderedProjectThreadIds }), + )} {project.expanded && hasHiddenThreads && !isThreadListExpanded && ( @@ -2093,6 +2073,21 @@ export default function Sidebar() { ) : null} + {globalPinnedThreads.length > 0 ? ( + + + {globalPinnedThreads.map((thread) => + renderThreadRow(thread, { + leadingVisual: "project-favicon", + orderedThreadIds: globalPinnedThreadIds, + }), + )} + + + ) : null}