Prioritize pinned threads in sidebar sorting
- Sort pinned threads ahead of unpinned ones in auto-sort modes - Persist thread pin state and surface pin/unpin actions in the sidebar - Keep delete fallback selection aware of pinned threads
This commit is contained in:
1 parent
835d693a92
commit
d32da3cb9a
6 files changed
+235
-15
No files matched your search
@@ -760,6 +760,73 @@ describe("sortThreadsForSidebar", () => {
|
||||
ThreadId.makeUnsafe("thread-2"),
|
||||
]);
|
||||
});
|
||||
|
||||
it("puts pinned threads ahead of unpinned threads in auto-sort modes", () => {
|
||||
const sorted = sortThreadsForSidebar(
|
||||
[
|
||||
{
|
||||
...makeThread({
|
||||
id: ThreadId.makeUnsafe("thread-1"),
|
||||
createdAt: "2026-03-09T10:00:00.000Z",
|
||||
updatedAt: "2026-03-09T10:01:00.000Z",
|
||||
}),
|
||||
pinned: true,
|
||||
},
|
||||
{
|
||||
...makeThread({
|
||||
id: ThreadId.makeUnsafe("thread-2"),
|
||||
createdAt: "2026-03-09T10:05:00.000Z",
|
||||
updatedAt: "2026-03-09T10:05:00.000Z",
|
||||
}),
|
||||
pinned: false,
|
||||
},
|
||||
],
|
||||
"updated_at",
|
||||
);
|
||||
|
||||
expect(sorted.map((thread) => thread.id)).toEqual([
|
||||
ThreadId.makeUnsafe("thread-1"),
|
||||
ThreadId.makeUnsafe("thread-2"),
|
||||
]);
|
||||
});
|
||||
|
||||
it("still auto-sorts within the pinned thread bucket", () => {
|
||||
const sorted = sortThreadsForSidebar(
|
||||
[
|
||||
{
|
||||
...makeThread({
|
||||
id: ThreadId.makeUnsafe("thread-1"),
|
||||
createdAt: "2026-03-09T10:01:00.000Z",
|
||||
updatedAt: "2026-03-09T10:01:00.000Z",
|
||||
}),
|
||||
pinned: true,
|
||||
},
|
||||
{
|
||||
...makeThread({
|
||||
id: ThreadId.makeUnsafe("thread-2"),
|
||||
createdAt: "2026-03-09T10:05:00.000Z",
|
||||
updatedAt: "2026-03-09T10:05:00.000Z",
|
||||
}),
|
||||
pinned: true,
|
||||
},
|
||||
{
|
||||
...makeThread({
|
||||
id: ThreadId.makeUnsafe("thread-3"),
|
||||
createdAt: "2026-03-09T10:10:00.000Z",
|
||||
updatedAt: "2026-03-09T10:10:00.000Z",
|
||||
}),
|
||||
pinned: false,
|
||||
},
|
||||
],
|
||||
"updated_at",
|
||||
);
|
||||
|
||||
expect(sorted.map((thread) => thread.id)).toEqual([
|
||||
ThreadId.makeUnsafe("thread-2"),
|
||||
ThreadId.makeUnsafe("thread-1"),
|
||||
ThreadId.makeUnsafe("thread-3"),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getFallbackThreadIdAfterDelete", () => {
|
||||
@@ -798,6 +865,41 @@ describe("getFallbackThreadIdAfterDelete", () => {
|
||||
expect(fallbackThreadId).toBe(ThreadId.makeUnsafe("thread-newest"));
|
||||
});
|
||||
|
||||
it("respects pinning when choosing the fallback thread after delete", () => {
|
||||
const fallbackThreadId = getFallbackThreadIdAfterDelete({
|
||||
threads: [
|
||||
{
|
||||
...makeThread({
|
||||
id: ThreadId.makeUnsafe("thread-active"),
|
||||
projectId: ProjectId.makeUnsafe("project-1"),
|
||||
updatedAt: "2026-03-09T10:05:00.000Z",
|
||||
}),
|
||||
pinned: false,
|
||||
},
|
||||
{
|
||||
...makeThread({
|
||||
id: ThreadId.makeUnsafe("thread-pinned"),
|
||||
projectId: ProjectId.makeUnsafe("project-1"),
|
||||
updatedAt: "2026-03-09T10:03:00.000Z",
|
||||
}),
|
||||
pinned: true,
|
||||
},
|
||||
{
|
||||
...makeThread({
|
||||
id: ThreadId.makeUnsafe("thread-newer"),
|
||||
projectId: ProjectId.makeUnsafe("project-1"),
|
||||
updatedAt: "2026-03-09T10:10:00.000Z",
|
||||
}),
|
||||
pinned: false,
|
||||
},
|
||||
],
|
||||
deletedThreadId: ThreadId.makeUnsafe("thread-active"),
|
||||
sortOrder: "updated_at",
|
||||
});
|
||||
|
||||
expect(fallbackThreadId).toBe(ThreadId.makeUnsafe("thread-pinned"));
|
||||
});
|
||||
|
||||
it("skips other threads being deleted in the same action", () => {
|
||||
const fallbackThreadId = getFallbackThreadIdAfterDelete({
|
||||
threads: [
|
||||
|
||||
@@ -21,6 +21,7 @@ type SidebarProject = {
|
||||
type SidebarThreadSortInput = Pick<Thread, "createdAt" | "updatedAt"> & {
|
||||
latestUserMessageAt?: string | null;
|
||||
messages?: Pick<Thread["messages"][number], "createdAt" | "role">[];
|
||||
pinned?: boolean | undefined;
|
||||
};
|
||||
|
||||
export type ThreadTraversalDirection = "previous" | "next";
|
||||
@@ -453,6 +454,11 @@ 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.pinned === true;
|
||||
const rightPinned = right.pinned === true;
|
||||
if (leftPinned !== rightPinned) {
|
||||
return rightPinned ? 1 : -1;
|
||||
}
|
||||
const rightTimestamp = getThreadSortTimestamp(right, sortOrder);
|
||||
const leftTimestamp = getThreadSortTimestamp(left, sortOrder);
|
||||
const byTimestamp =
|
||||
|
||||
@@ -162,6 +162,7 @@ type SidebarThreadSnapshot = Pick<
|
||||
> & {
|
||||
lastVisitedAt?: string | undefined;
|
||||
latestUserMessageAt: string | null;
|
||||
pinned: boolean;
|
||||
};
|
||||
|
||||
type SidebarProjectSnapshot = Project & {
|
||||
@@ -192,9 +193,10 @@ function getLatestUserMessageAt(thread: Thread): string | null {
|
||||
function toSidebarThreadSnapshot(
|
||||
thread: Thread,
|
||||
lastVisitedAt: string | undefined,
|
||||
pinned: boolean,
|
||||
): SidebarThreadSnapshot {
|
||||
const cached = sidebarThreadSnapshotCache.get(thread);
|
||||
if (cached && cached.lastVisitedAt === lastVisitedAt) {
|
||||
if (cached && cached.lastVisitedAt === lastVisitedAt && cached.snapshot.pinned === pinned) {
|
||||
return cached.snapshot;
|
||||
}
|
||||
|
||||
@@ -214,6 +216,7 @@ function toSidebarThreadSnapshot(
|
||||
activities: thread.activities,
|
||||
proposedPlans: thread.proposedPlans,
|
||||
latestUserMessageAt: getLatestUserMessageAt(thread),
|
||||
pinned,
|
||||
};
|
||||
sidebarThreadSnapshotCache.set(thread, { lastVisitedAt, snapshot });
|
||||
return snapshot;
|
||||
@@ -442,16 +445,23 @@ function SortableProjectItem({
|
||||
export default function Sidebar() {
|
||||
const projects = useStore((store) => store.projects);
|
||||
const serverThreads = useStore((store) => store.threads);
|
||||
const { projectExpandedById, projectOrder, projectPinnedById, threadLastVisitedAtById } =
|
||||
useUiStateStore(
|
||||
useShallow((store) => ({
|
||||
projectExpandedById: store.projectExpandedById,
|
||||
projectOrder: store.projectOrder,
|
||||
projectPinnedById: store.projectPinnedById,
|
||||
threadLastVisitedAtById: store.threadLastVisitedAtById,
|
||||
})),
|
||||
);
|
||||
const {
|
||||
projectExpandedById,
|
||||
projectOrder,
|
||||
projectPinnedById,
|
||||
threadLastVisitedAtById,
|
||||
threadPinnedById,
|
||||
} = useUiStateStore(
|
||||
useShallow((store) => ({
|
||||
projectExpandedById: store.projectExpandedById,
|
||||
projectOrder: store.projectOrder,
|
||||
projectPinnedById: store.projectPinnedById,
|
||||
threadLastVisitedAtById: store.threadLastVisitedAtById,
|
||||
threadPinnedById: store.threadPinnedById,
|
||||
})),
|
||||
);
|
||||
const markThreadUnread = useUiStateStore((store) => store.markThreadUnread);
|
||||
const toggleThreadPinned = useUiStateStore((store) => store.toggleThreadPinned);
|
||||
const toggleProject = useUiStateStore((store) => store.toggleProject);
|
||||
const toggleProjectPinned = useUiStateStore((store) => store.toggleProjectPinned);
|
||||
const reorderProjects = useUiStateStore((store) => store.reorderProjects);
|
||||
@@ -527,9 +537,13 @@ export default function Sidebar() {
|
||||
const threads = useMemo(
|
||||
() =>
|
||||
serverThreads.map((thread) =>
|
||||
toSidebarThreadSnapshot(thread, threadLastVisitedAtById[thread.id]),
|
||||
toSidebarThreadSnapshot(
|
||||
thread,
|
||||
threadLastVisitedAtById[thread.id],
|
||||
threadPinnedById[thread.id] ?? false,
|
||||
),
|
||||
),
|
||||
[serverThreads, threadLastVisitedAtById],
|
||||
[serverThreads, threadLastVisitedAtById, threadPinnedById],
|
||||
);
|
||||
const projectCwdById = useMemo(
|
||||
() => new Map(projects.map((project) => [project.id, project.cwd] as const)),
|
||||
@@ -848,6 +862,7 @@ export default function Sidebar() {
|
||||
thread.worktreePath ?? projectCwdById.get(thread.projectId) ?? null;
|
||||
const clicked = await api.contextMenu.show(
|
||||
[
|
||||
{ id: "toggle-pin", label: threadPinnedById[threadId] ? "Unpin thread" : "Pin thread" },
|
||||
{ id: "rename", label: "Rename thread" },
|
||||
{ id: "mark-unread", label: "Mark unread" },
|
||||
{ id: "copy-path", label: "Copy Path" },
|
||||
@@ -857,6 +872,11 @@ export default function Sidebar() {
|
||||
position,
|
||||
);
|
||||
|
||||
if (clicked === "toggle-pin") {
|
||||
toggleThreadPinned(threadId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (clicked === "rename") {
|
||||
setRenamingThreadId(threadId);
|
||||
setRenamingTitle(thread.title);
|
||||
@@ -905,7 +925,9 @@ export default function Sidebar() {
|
||||
deleteThread,
|
||||
markThreadUnread,
|
||||
projectCwdById,
|
||||
threadPinnedById,
|
||||
threads,
|
||||
toggleThreadPinned,
|
||||
],
|
||||
);
|
||||
|
||||
@@ -1459,6 +1481,11 @@ export default function Sidebar() {
|
||||
}}
|
||||
>
|
||||
<div className="flex min-w-0 flex-1 items-center gap-1.5 text-left">
|
||||
{thread.pinned ? (
|
||||
<span className="inline-flex shrink-0" title="Pinned thread">
|
||||
<PinIcon className="size-3 text-muted-foreground/55" />
|
||||
</span>
|
||||
) : null}
|
||||
{prStatus && (
|
||||
<Tooltip>
|
||||
<TooltipTrigger
|
||||
|
||||
@@ -11,6 +11,7 @@ import { newCommandId } from "../lib/utils";
|
||||
import { readNativeApi } from "../nativeApi";
|
||||
import { useStore } from "../store";
|
||||
import { useTerminalStateStore } from "../terminalStateStore";
|
||||
import { useUiStateStore } from "../uiStateStore";
|
||||
import { formatWorktreePathForDisplay, getOrphanedWorktreePathForThread } from "../worktreeCleanup";
|
||||
import { toastManager } from "../components/ui/toast";
|
||||
import { useSettings } from "./useSettings";
|
||||
@@ -112,8 +113,11 @@ export function useThreadActions() {
|
||||
|
||||
const deletedThreadIds = opts.deletedThreadIds ?? new Set<ThreadId>();
|
||||
const shouldNavigateToFallback = routeThreadId === threadId;
|
||||
const threadPinnedById = useUiStateStore.getState().threadPinnedById;
|
||||
const fallbackThreadId = getFallbackThreadIdAfterDelete({
|
||||
threads,
|
||||
threads: threads.map((entry) =>
|
||||
Object.assign({}, entry, { pinned: threadPinnedById[entry.id] ?? false }),
|
||||
),
|
||||
deletedThreadId: threadId,
|
||||
deletedThreadIds,
|
||||
sortOrder: appSettings.sidebarThreadSortOrder,
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
syncProjects,
|
||||
syncThreads,
|
||||
toggleProjectPinned,
|
||||
toggleThreadPinned,
|
||||
type UiState,
|
||||
} from "./uiStateStore";
|
||||
|
||||
@@ -18,6 +19,7 @@ function makeUiState(overrides: Partial<UiState> = {}): UiState {
|
||||
projectOrder: [],
|
||||
projectPinnedById: {},
|
||||
threadLastVisitedAtById: {},
|
||||
threadPinnedById: {},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
@@ -190,6 +192,23 @@ describe("uiStateStore pure functions", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("syncThreads preserves pinned state for retained threads", () => {
|
||||
const thread1 = ThreadId.makeUnsafe("thread-1");
|
||||
const thread2 = ThreadId.makeUnsafe("thread-2");
|
||||
const initialState = makeUiState({
|
||||
threadPinnedById: {
|
||||
[thread1]: true,
|
||||
[thread2]: true,
|
||||
},
|
||||
});
|
||||
|
||||
const next = syncThreads(initialState, [{ id: thread1 }]);
|
||||
|
||||
expect(next.threadPinnedById).toEqual({
|
||||
[thread1]: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("setProjectExpanded updates expansion without touching order", () => {
|
||||
const project1 = ProjectId.makeUnsafe("project-1");
|
||||
const initialState = makeUiState({
|
||||
@@ -216,16 +235,31 @@ describe("uiStateStore pure functions", () => {
|
||||
expect(unpinned.projectPinnedById).toEqual({});
|
||||
});
|
||||
|
||||
it("toggleThreadPinned adds and removes pinned state", () => {
|
||||
const thread1 = ThreadId.makeUnsafe("thread-1");
|
||||
const pinned = toggleThreadPinned(makeUiState(), thread1);
|
||||
|
||||
expect(pinned.threadPinnedById).toEqual({ [thread1]: true });
|
||||
|
||||
const unpinned = toggleThreadPinned(pinned, thread1);
|
||||
|
||||
expect(unpinned.threadPinnedById).toEqual({});
|
||||
});
|
||||
|
||||
it("clearThreadUi removes visit state for deleted threads", () => {
|
||||
const thread1 = ThreadId.makeUnsafe("thread-1");
|
||||
const initialState = makeUiState({
|
||||
threadLastVisitedAtById: {
|
||||
[thread1]: "2026-02-25T12:35:00.000Z",
|
||||
},
|
||||
threadPinnedById: {
|
||||
[thread1]: true,
|
||||
},
|
||||
});
|
||||
|
||||
const next = clearThreadUi(initialState, thread1);
|
||||
|
||||
expect(next.threadLastVisitedAtById).toEqual({});
|
||||
expect(next.threadPinnedById).toEqual({});
|
||||
});
|
||||
});
|
||||
@@ -20,6 +20,7 @@ interface PersistedUiState {
|
||||
expandedProjectCwds?: string[];
|
||||
projectOrderCwds?: string[];
|
||||
pinnedProjectCwds?: string[];
|
||||
pinnedThreadIds?: string[];
|
||||
}
|
||||
|
||||
export interface UiProjectState {
|
||||
@@ -30,6 +31,7 @@ export interface UiProjectState {
|
||||
|
||||
export interface UiThreadState {
|
||||
threadLastVisitedAtById: Record<string, string>;
|
||||
threadPinnedById: Record<string, boolean>;
|
||||
}
|
||||
|
||||
export interface UiState extends UiProjectState, UiThreadState {}
|
||||
@@ -49,11 +51,13 @@ const initialState: UiState = {
|
||||
projectOrder: [],
|
||||
projectPinnedById: {},
|
||||
threadLastVisitedAtById: {},
|
||||
threadPinnedById: {},
|
||||
};
|
||||
|
||||
const persistedExpandedProjectCwds = new Set<string>();
|
||||
const persistedProjectOrderCwds: string[] = [];
|
||||
const persistedPinnedProjectCwds = new Set<string>();
|
||||
const persistedPinnedThreadIds = new Set<ThreadId>();
|
||||
const currentProjectCwdById = new Map<ProjectId, string>();
|
||||
let legacyKeysCleanedUp = false;
|
||||
|
||||
@@ -85,6 +89,7 @@ function hydratePersistedProjectState(parsed: PersistedUiState): void {
|
||||
persistedExpandedProjectCwds.clear();
|
||||
persistedProjectOrderCwds.length = 0;
|
||||
persistedPinnedProjectCwds.clear();
|
||||
persistedPinnedThreadIds.clear();
|
||||
for (const cwd of parsed.expandedProjectCwds ?? []) {
|
||||
if (typeof cwd === "string" && cwd.length > 0) {
|
||||
persistedExpandedProjectCwds.add(cwd);
|
||||
@@ -100,6 +105,11 @@ function hydratePersistedProjectState(parsed: PersistedUiState): void {
|
||||
persistedPinnedProjectCwds.add(cwd);
|
||||
}
|
||||
}
|
||||
for (const threadId of parsed.pinnedThreadIds ?? []) {
|
||||
if (typeof threadId === "string" && threadId.length > 0) {
|
||||
persistedPinnedThreadIds.add(threadId as ThreadId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function persistState(state: UiState): void {
|
||||
@@ -123,12 +133,16 @@ function persistState(state: UiState): void {
|
||||
const cwd = currentProjectCwdById.get(projectId as ProjectId);
|
||||
return cwd ? [cwd] : [];
|
||||
});
|
||||
const pinnedThreadIds = Object.entries(state.threadPinnedById)
|
||||
.filter(([, pinned]) => pinned)
|
||||
.map(([threadId]) => threadId);
|
||||
window.localStorage.setItem(
|
||||
PERSISTED_STATE_KEY,
|
||||
JSON.stringify({
|
||||
expandedProjectCwds,
|
||||
projectOrderCwds,
|
||||
pinnedProjectCwds,
|
||||
pinnedThreadIds,
|
||||
} satisfies PersistedUiState),
|
||||
);
|
||||
if (!legacyKeysCleanedUp) {
|
||||
@@ -280,6 +294,11 @@ export function syncThreads(state: UiState, threads: readonly SyncThreadInput[])
|
||||
retainedThreadIds.has(threadId as ThreadId),
|
||||
),
|
||||
);
|
||||
const nextThreadPinnedById = Object.fromEntries(
|
||||
Object.entries(state.threadPinnedById).filter(([threadId]) =>
|
||||
retainedThreadIds.has(threadId as ThreadId),
|
||||
),
|
||||
);
|
||||
for (const thread of threads) {
|
||||
if (
|
||||
nextThreadLastVisitedAtById[thread.id] === undefined &&
|
||||
@@ -288,13 +307,20 @@ export function syncThreads(state: UiState, threads: readonly SyncThreadInput[])
|
||||
) {
|
||||
nextThreadLastVisitedAtById[thread.id] = thread.seedVisitedAt;
|
||||
}
|
||||
if (nextThreadPinnedById[thread.id] === undefined && persistedPinnedThreadIds.has(thread.id)) {
|
||||
nextThreadPinnedById[thread.id] = true;
|
||||
}
|
||||
}
|
||||
if (recordsEqual(state.threadLastVisitedAtById, nextThreadLastVisitedAtById)) {
|
||||
if (
|
||||
recordsEqual(state.threadLastVisitedAtById, nextThreadLastVisitedAtById) &&
|
||||
recordsEqual(state.threadPinnedById, nextThreadPinnedById)
|
||||
) {
|
||||
return state;
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
threadLastVisitedAtById: nextThreadLastVisitedAtById,
|
||||
threadPinnedById: nextThreadPinnedById,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -345,14 +371,33 @@ export function markThreadUnread(
|
||||
}
|
||||
|
||||
export function clearThreadUi(state: UiState, threadId: ThreadId): UiState {
|
||||
if (!(threadId in state.threadLastVisitedAtById)) {
|
||||
if (!(threadId in state.threadLastVisitedAtById) && !(threadId in state.threadPinnedById)) {
|
||||
return state;
|
||||
}
|
||||
const nextThreadLastVisitedAtById = { ...state.threadLastVisitedAtById };
|
||||
const nextThreadPinnedById = { ...state.threadPinnedById };
|
||||
delete nextThreadLastVisitedAtById[threadId];
|
||||
delete nextThreadPinnedById[threadId];
|
||||
return {
|
||||
...state,
|
||||
threadLastVisitedAtById: nextThreadLastVisitedAtById,
|
||||
threadPinnedById: nextThreadPinnedById,
|
||||
};
|
||||
}
|
||||
|
||||
export function toggleThreadPinned(state: UiState, threadId: ThreadId): UiState {
|
||||
const nextThreadPinnedById = { ...state.threadPinnedById };
|
||||
if (nextThreadPinnedById[threadId]) {
|
||||
delete nextThreadPinnedById[threadId];
|
||||
} else {
|
||||
nextThreadPinnedById[threadId] = true;
|
||||
}
|
||||
if (recordsEqual(state.threadPinnedById, nextThreadPinnedById)) {
|
||||
return state;
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
threadPinnedById: nextThreadPinnedById,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -431,6 +476,7 @@ interface UiStateStore extends UiState {
|
||||
markThreadVisited: (threadId: ThreadId, visitedAt?: string) => void;
|
||||
markThreadUnread: (threadId: ThreadId, latestTurnCompletedAt: string | null | undefined) => void;
|
||||
clearThreadUi: (threadId: ThreadId) => void;
|
||||
toggleThreadPinned: (threadId: ThreadId) => void;
|
||||
toggleProject: (projectId: ProjectId) => void;
|
||||
toggleProjectPinned: (projectId: ProjectId) => void;
|
||||
setProjectExpanded: (projectId: ProjectId, expanded: boolean) => void;
|
||||
@@ -446,6 +492,7 @@ export const useUiStateStore = create<UiStateStore>((set) => ({
|
||||
markThreadUnread: (threadId, latestTurnCompletedAt) =>
|
||||
set((state) => markThreadUnread(state, threadId, latestTurnCompletedAt)),
|
||||
clearThreadUi: (threadId) => set((state) => clearThreadUi(state, threadId)),
|
||||
toggleThreadPinned: (threadId) => set((state) => toggleThreadPinned(state, threadId)),
|
||||
toggleProject: (projectId) => set((state) => toggleProject(state, projectId)),
|
||||
toggleProjectPinned: (projectId) => set((state) => toggleProjectPinned(state, projectId)),
|
||||
setProjectExpanded: (projectId, expanded) =>
|
||||
|
||||
Reference in new issue
Block a user