Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/web/src/components/Sidebar.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type SidebarProject = {
updatedAt?: string | undefined;
};
type SidebarThreadSortInput = Pick<Thread, "createdAt" | "updatedAt"> & {
isPinned?: boolean;
latestUserMessageAt?: string | null;
messages?: Pick<Thread["messages"][number], "createdAt" | "role">[];
};
Expand Down Expand Up @@ -485,6 +486,12 @@ export function sortThreadsForSidebar<
T extends Pick<Thread, "id" | "createdAt" | "updatedAt"> & SidebarThreadSortInput,
>(threads: readonly T[], sortOrder: SidebarThreadSortOrder): T[] {
return threads.toSorted((left, right) => {
const leftPinned = left.isPinned === true;
const rightPinned = right.isPinned === true;
if (leftPinned !== rightPinned) {
return rightPinned ? 1 : -1;
}

const rightTimestamp = getThreadSortTimestamp(right, sortOrder);
const leftTimestamp = getThreadSortTimestamp(left, sortOrder);
const byTimestamp =
Expand Down
208 changes: 189 additions & 19 deletions apps/web/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ChevronRightIcon,
FolderIcon,
GitPullRequestIcon,
PinIcon,
PlusIcon,
SettingsIcon,
SquarePenIcon,
Expand Down Expand Up @@ -258,7 +259,9 @@ function resolveThreadPr(
interface SidebarThreadRowProps {
threadId: ThreadId;
projectCwd: string | null;
orderedProjectThreadIds: readonly ThreadId[];
isPinned: boolean;
folderLabel?: string | null;
orderedSidebarThreadIds: readonly ThreadId[];
routeThreadId: ThreadId | null;
selectedThreadIds: ReadonlySet<ThreadId>;
showThreadJumpHints: boolean;
Expand All @@ -276,7 +279,7 @@ interface SidebarThreadRowProps {
handleThreadClick: (
event: MouseEvent,
threadId: ThreadId,
orderedProjectThreadIds: readonly ThreadId[],
orderedSidebarThreadIds: readonly ThreadId[],
) => void;
navigateToThread: (threadId: ThreadId) => void;
handleMultiSelectContextMenu: (position: { x: number; y: number }) => Promise<void>;
Expand Down Expand Up @@ -353,7 +356,7 @@ function SidebarThreadRow(props: SidebarThreadRowProps) {
isSelected,
})} relative isolate`}
onClick={(event) => {
props.handleThreadClick(event, thread.id, props.orderedProjectThreadIds);
props.handleThreadClick(event, thread.id, props.orderedSidebarThreadIds);
}}
onKeyDown={(event) => {
if (event.key !== "Enter" && event.key !== " ") return;
Expand Down Expand Up @@ -399,6 +402,21 @@ function SidebarThreadRow(props: SidebarThreadRowProps) {
</Tooltip>
)}
{threadStatus && <ThreadStatusLabel status={threadStatus} />}
{props.isPinned ? (
<Tooltip>
<TooltipTrigger
render={
<span
aria-label="Pinned thread"
className="inline-flex shrink-0 items-center justify-center text-amber-600 dark:text-amber-300/90"
>
<PinIcon className="size-3 rotate-45" />
</span>
}
/>
<TooltipPopup side="top">Pinned</TooltipPopup>
</Tooltip>
) : null}
{props.renamingThreadId === thread.id ? (
<input
ref={props.onRenamingInputMount}
Expand All @@ -425,7 +443,14 @@ function SidebarThreadRow(props: SidebarThreadRowProps) {
onClick={(event) => event.stopPropagation()}
/>
) : (
<span className="min-w-0 flex-1 truncate text-xs">{thread.title}</span>
<div className="flex min-w-0 flex-1 items-baseline gap-1.5 overflow-hidden">
<span className="truncate text-xs">{thread.title}</span>
{props.folderLabel ? (
<span className="shrink truncate text-[10px] text-muted-foreground/45">
/{props.folderLabel}
</span>
) : null}
</div>
)}
</div>
<div className="ml-auto flex shrink-0 items-center gap-1.5">
Expand Down Expand Up @@ -677,14 +702,17 @@ export default function Sidebar() {
const projects = useStore((store) => store.projects);
const sidebarThreadsById = useStore((store) => store.sidebarThreadsById);
const threadIdsByProjectId = useStore((store) => store.threadIdsByProjectId);
const { projectExpandedById, projectOrder, threadLastVisitedAtById } = useUiStateStore(
useShallow((store) => ({
projectExpandedById: store.projectExpandedById,
projectOrder: store.projectOrder,
threadLastVisitedAtById: store.threadLastVisitedAtById,
})),
);
const { pinnedThreadIds, projectExpandedById, projectOrder, threadLastVisitedAtById } =
useUiStateStore(
useShallow((store) => ({
pinnedThreadIds: store.pinnedThreadIds,
projectExpandedById: store.projectExpandedById,
projectOrder: store.projectOrder,
threadLastVisitedAtById: store.threadLastVisitedAtById,
})),
);
const markThreadUnread = useUiStateStore((store) => store.markThreadUnread);
const setThreadPinned = useUiStateStore((store) => store.setThreadPinned);
const toggleProject = useUiStateStore((store) => store.toggleProject);
const reorderProjects = useUiStateStore((store) => store.reorderProjects);
const clearComposerDraftForThread = useComposerDraftStore((store) => store.clearDraftThread);
Expand Down Expand Up @@ -757,10 +785,21 @@ export default function Sidebar() {
[orderedProjects, projectExpandedById],
);
const sidebarThreads = useMemo(() => Object.values(sidebarThreadsById), [sidebarThreadsById]);
const pinnedThreadIdSet = useMemo(() => new Set(pinnedThreadIds), [pinnedThreadIds]);
const projectCwdById = useMemo(
() => new Map(projects.map((project) => [project.id, project.cwd] as const)),
[projects],
);
const projectFolderLabelById = useMemo(
() =>
new Map(
projects.map((project) => [
project.id,
project.cwd.split(/[/\\]/).findLast(isNonEmptyString) ?? project.name,
]),
),
[projects],
);
const routeTerminalOpen = routeThreadId
? selectThreadTerminalState(terminalStateByThreadId, routeThreadId).terminalOpen
: false;
Expand Down Expand Up @@ -815,7 +854,14 @@ export default function Sidebar() {
(projectId: ProjectId) => {
const latestThread = sortThreadsForSidebar(
(threadIdsByProjectId[projectId] ?? [])
.map((threadId) => sidebarThreadsById[threadId])
.map((threadId) => {
const thread = sidebarThreadsById[threadId];
return thread
? Object.assign({}, thread, {
isPinned: pinnedThreadIdSet.has(thread.id),
})
: undefined;
})
.filter((thread): thread is NonNullable<typeof thread> => thread !== undefined)
.filter((thread) => thread.archivedAt === null),
appSettings.sidebarThreadSortOrder,
Expand All @@ -827,7 +873,13 @@ export default function Sidebar() {
params: { threadId: latestThread.id },
});
},
[appSettings.sidebarThreadSortOrder, navigate, sidebarThreadsById, threadIdsByProjectId],
[
appSettings.sidebarThreadSortOrder,
navigate,
pinnedThreadIdSet,
sidebarThreadsById,
threadIdsByProjectId,
],
);

const addProjectFromPath = useCallback(
Expand Down Expand Up @@ -1099,6 +1151,10 @@ export default function Sidebar() {
const clicked = await api.contextMenu.show(
[
{ id: "rename", label: "Rename thread" },
{
id: "toggle-pin",
label: pinnedThreadIdSet.has(threadId) ? "Unpin thread" : "Pin thread",
},
{ id: "mark-unread", label: "Mark unread" },
{ id: "copy-path", label: "Copy Path" },
{ id: "copy-thread-id", label: "Copy Thread ID" },
Expand All @@ -1116,6 +1172,11 @@ export default function Sidebar() {
return;
}

if (clicked === "toggle-pin") {
setThreadPinned(threadId, !pinnedThreadIdSet.has(threadId));
return;
}

if (clicked === "mark-unread") {
markThreadUnread(threadId, thread.latestTurn?.completedAt);
return;
Expand Down Expand Up @@ -1156,7 +1217,9 @@ export default function Sidebar() {
copyThreadIdToClipboard,
deleteThread,
markThreadUnread,
pinnedThreadIdSet,
projectCwdById,
setThreadPinned,
sidebarThreadsById,
],
);
Expand All @@ -1171,12 +1234,26 @@ export default function Sidebar() {

const clicked = await api.contextMenu.show(
[
{
id: "toggle-pin",
label: ids.every((id) => pinnedThreadIdSet.has(id))
? `Unpin (${count})`
: `Pin (${count})`,
},
{ id: "mark-unread", label: `Mark unread (${count})` },
{ id: "delete", label: `Delete (${count})`, destructive: true },
],
position,
);

if (clicked === "toggle-pin") {
const shouldPin = !ids.every((id) => pinnedThreadIdSet.has(id));
for (const id of ids) {
setThreadPinned(id, shouldPin);
}
return;
}

if (clicked === "mark-unread") {
for (const id of ids) {
const thread = sidebarThreadsById[id];
Expand Down Expand Up @@ -1209,8 +1286,10 @@ export default function Sidebar() {
clearSelection,
deleteThread,
markThreadUnread,
pinnedThreadIdSet,
removeFromSelection,
selectedThreadIds,
setThreadPinned,
sidebarThreadsById,
],
);
Expand Down Expand Up @@ -1427,6 +1506,20 @@ export default function Sidebar() {
() => sidebarThreads.filter((thread) => thread.archivedAt === null),
[sidebarThreads],
);
const pinnedThreads = useMemo(
() =>
sortThreadsForSidebar(
visibleThreads
.filter((thread) => pinnedThreadIdSet.has(thread.id))
.map((thread) =>
Object.assign({}, thread, {
isPinned: true,
}),
),
appSettings.sidebarThreadSortOrder,
),
[appSettings.sidebarThreadSortOrder, pinnedThreadIdSet, visibleThreads],
);
const sortedProjects = useMemo(
() =>
sortProjectsForSidebar(sidebarProjects, visibleThreads, appSettings.sidebarProjectSortOrder),
Expand All @@ -1445,7 +1538,14 @@ export default function Sidebar() {
});
const projectThreads = sortThreadsForSidebar(
(threadIdsByProjectId[project.id] ?? [])
.map((threadId) => sidebarThreadsById[threadId])
.map((threadId) => {
const thread = sidebarThreadsById[threadId];
return thread
? Object.assign({}, thread, {
isPinned: pinnedThreadIdSet.has(thread.id),
})
: undefined;
})
.filter((thread): thread is NonNullable<typeof thread> => thread !== undefined)
.filter((thread) => thread.archivedAt === null),
appSettings.sidebarThreadSortOrder,
Expand Down Expand Up @@ -1496,15 +1596,33 @@ export default function Sidebar() {
expandedThreadListsByProject,
routeThreadId,
sortedProjects,
pinnedThreadIdSet,
sidebarThreadsById,
threadIdsByProjectId,
threadLastVisitedAtById,
],
);
const visibleSidebarThreadIds = useMemo(
() => getVisibleSidebarThreadIds(renderedProjects),
[renderedProjects],
);
const visibleSidebarThreadIds = useMemo(() => {
const renderedThreadIds = getVisibleSidebarThreadIds([
{
shouldShowThreadPanel: pinnedThreads.length > 0,
renderedThreadIds: pinnedThreads.map((thread) => thread.id),
},
...renderedProjects,
]);

const uniqueThreadIds: ThreadId[] = [];
const seenThreadIds = new Set<ThreadId>();
for (const threadId of renderedThreadIds) {
if (seenThreadIds.has(threadId)) {
continue;
}
seenThreadIds.add(threadId);
uniqueThreadIds.push(threadId);
}

return uniqueThreadIds;
}, [pinnedThreads, renderedProjects]);
const threadJumpCommandById = useMemo(() => {
const mapping = new Map<ThreadId, NonNullable<ReturnType<typeof threadJumpCommandForIndex>>>();
for (const [visibleThreadIndex, threadId] of visibleSidebarThreadIds.entries()) {
Expand Down Expand Up @@ -1797,7 +1915,8 @@ export default function Sidebar() {
key={threadId}
threadId={threadId}
projectCwd={project.cwd}
orderedProjectThreadIds={orderedProjectThreadIds}
isPinned={pinnedThreadIdSet.has(threadId)}
orderedSidebarThreadIds={orderedProjectThreadIds}
routeThreadId={routeThreadId}
selectedThreadIds={selectedThreadIds}
showThreadJumpHints={showThreadJumpHints}
Expand Down Expand Up @@ -2111,6 +2230,57 @@ export default function Sidebar() {
</Alert>
</SidebarGroup>
) : null}
{pinnedThreads.length > 0 ? (
<SidebarGroup className="px-2 pt-2 pb-0">
<div className="mb-1 flex items-center justify-between pl-2 pr-1.5">
<span className="text-[10px] font-medium uppercase tracking-wider text-muted-foreground/60">
Pinned
</span>
</div>
<SidebarMenu>
<SidebarMenuItem className="rounded-md">
<SidebarMenuSub
ref={attachThreadListAutoAnimateRef}
className="mx-1 my-0 w-full translate-x-0 gap-0.5 overflow-hidden px-1.5 py-0"
>
{pinnedThreads.map((thread) => (
<SidebarThreadRow
key={thread.id}
threadId={thread.id}
projectCwd={projectCwdById.get(thread.projectId) ?? null}
isPinned
folderLabel={projectFolderLabelById.get(thread.projectId) ?? null}
orderedSidebarThreadIds={orderedSidebarThreadIds}
routeThreadId={routeThreadId}
selectedThreadIds={selectedThreadIds}
showThreadJumpHints={showThreadJumpHints}
jumpLabel={threadJumpLabelById.get(thread.id) ?? null}
appSettingsConfirmThreadArchive={appSettings.confirmThreadArchive}
renamingThreadId={renamingThreadId}
renamingTitle={renamingTitle}
setRenamingTitle={setRenamingTitle}
onRenamingInputMount={handleRenamingInputMount}
hasRenameCommitted={hasRenameCommitted}
markRenameCommitted={markRenameCommitted}
confirmingArchiveThreadId={confirmingArchiveThreadId}
setConfirmingArchiveThreadId={setConfirmingArchiveThreadId}
confirmArchiveButtonRefs={confirmArchiveButtonRefs}
handleThreadClick={handleThreadClick}
navigateToThread={navigateToThread}
handleMultiSelectContextMenu={handleMultiSelectContextMenu}
handleThreadContextMenu={handleThreadContextMenu}
clearSelection={clearSelection}
commitRename={commitRename}
cancelRename={cancelRename}
attemptArchiveThread={attemptArchiveThread}
openPrLink={openPrLink}
/>
))}
</SidebarMenuSub>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroup>
) : null}
<SidebarGroup className="px-2 py-2">
<div className="mb-1 flex items-center justify-between pl-2 pr-1.5">
<span className="text-[10px] font-medium uppercase tracking-wider text-muted-foreground/60">
Expand Down
Loading
Loading