Compare commits

...
Author SHA1 Message Date
Cursor Agent 08e5181717 Fix unpinned items being re-pinned on next server sync
Toggle functions now store false instead of deleting keys, and
syncProjects/syncThreads unconditionally store pinned state for all
items. This prevents the nullish coalescing fallback from reaching
stale persisted sets and re-pinning items that the user explicitly
unpinned.
2026-03-30 20:48:50 +00:00
Julius Marminge d32da3cb9a 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
2026-03-30 13:30:06 -07:00
Julius Marminge 835d693a92 Prioritize pinned projects in sidebar sorting
- Add project pin state persistence and toggle actions
- Show pin affordance in the sidebar and keep pinned projects first
2026-03-30 13:04:14 -07:00
6 changed files with 408 additions and 9 deletions

No files matched your search

+164 -1
View File
@@ -586,7 +586,9 @@ describe("getVisibleThreadsForProject", () => {
});
});
function makeProject(overrides: Partial<Project> = {}): Project {
function makeProject(
overrides: Partial<Project> & { pinned?: boolean } = {},
): Project & { pinned?: boolean } {
const { defaultModelSelection, ...rest } = overrides;
return {
id: ProjectId.makeUnsafe("project-1"),
@@ -758,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", () => {
@@ -796,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: [
@@ -900,6 +1004,65 @@ describe("sortProjectsForSidebar", () => {
]);
});
it("puts pinned projects ahead of unpinned projects in auto-sort modes", () => {
const sorted = sortProjectsForSidebar(
[
makeProject({
id: ProjectId.makeUnsafe("project-1"),
name: "Pinned older project",
pinned: true,
updatedAt: "2026-03-09T10:01:00.000Z",
}),
makeProject({
id: ProjectId.makeUnsafe("project-2"),
name: "Unpinned newer project",
pinned: false,
updatedAt: "2026-03-09T10:05:00.000Z",
}),
],
[],
"updated_at",
);
expect(sorted.map((project) => project.id)).toEqual([
ProjectId.makeUnsafe("project-1"),
ProjectId.makeUnsafe("project-2"),
]);
});
it("still auto-sorts within the pinned bucket", () => {
const sorted = sortProjectsForSidebar(
[
makeProject({
id: ProjectId.makeUnsafe("project-1"),
name: "Pinned older project",
pinned: true,
updatedAt: "2026-03-09T10:01:00.000Z",
}),
makeProject({
id: ProjectId.makeUnsafe("project-2"),
name: "Pinned newer project",
pinned: true,
updatedAt: "2026-03-09T10:05:00.000Z",
}),
makeProject({
id: ProjectId.makeUnsafe("project-3"),
name: "Unpinned project",
pinned: false,
updatedAt: "2026-03-09T10:10:00.000Z",
}),
],
[],
"updated_at",
);
expect(sorted.map((project) => project.id)).toEqual([
ProjectId.makeUnsafe("project-2"),
ProjectId.makeUnsafe("project-1"),
ProjectId.makeUnsafe("project-3"),
]);
});
it("falls back to name and id ordering when projects have no sortable timestamps", () => {
const sorted = sortProjectsForSidebar(
[
+12
View File
@@ -16,10 +16,12 @@ type SidebarProject = {
name: string;
createdAt?: string | undefined;
updatedAt?: string | undefined;
pinned?: boolean | undefined;
};
type SidebarThreadSortInput = Pick<Thread, "createdAt" | "updatedAt"> & {
latestUserMessageAt?: string | null;
messages?: Pick<Thread["messages"][number], "createdAt" | "role">[];
pinned?: boolean | undefined;
};
export type ThreadTraversalDirection = "previous" | "next";
@@ -452,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 =
@@ -526,6 +533,11 @@ export function sortProjectsForSidebar<
}
return [...projects].toSorted((left, right) => {
const leftPinned = left.pinned === true;
const rightPinned = right.pinned === true;
if (leftPinned !== rightPinned) {
return rightPinned ? 1 : -1;
}
const rightTimestamp = getProjectSortTimestamp(
right,
threadsByProjectId.get(right.id) ?? [],
+60 -5
View File
@@ -4,6 +4,7 @@ import {
ChevronRightIcon,
FolderIcon,
GitPullRequestIcon,
PinIcon,
PlusIcon,
SettingsIcon,
SquarePenIcon,
@@ -161,10 +162,12 @@ type SidebarThreadSnapshot = Pick<
> & {
lastVisitedAt?: string | undefined;
latestUserMessageAt: string | null;
pinned: boolean;
};
type SidebarProjectSnapshot = Project & {
expanded: boolean;
pinned: boolean;
};
const sidebarThreadSnapshotCache = new WeakMap<
@@ -190,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;
}
@@ -212,6 +216,7 @@ function toSidebarThreadSnapshot(
activities: thread.activities,
proposedPlans: thread.proposedPlans,
latestUserMessageAt: getLatestUserMessageAt(thread),
pinned,
};
sidebarThreadSnapshotCache.set(thread, { lastVisitedAt, snapshot });
return snapshot;
@@ -440,15 +445,25 @@ function SortableProjectItem({
export default function Sidebar() {
const projects = useStore((store) => store.projects);
const serverThreads = useStore((store) => store.threads);
const { projectExpandedById, projectOrder, threadLastVisitedAtById } = useUiStateStore(
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);
const clearComposerDraftForThread = useComposerDraftStore((store) => store.clearDraftThread);
const getDraftThreadByProjectId = useComposerDraftStore(
@@ -515,15 +530,20 @@ export default function Sidebar() {
orderedProjects.map((project) => ({
...project,
expanded: projectExpandedById[project.id] ?? true,
pinned: projectPinnedById[project.id] ?? false,
})),
[orderedProjects, projectExpandedById],
[orderedProjects, projectExpandedById, projectPinnedById],
);
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)),
@@ -842,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" },
@@ -851,6 +872,11 @@ export default function Sidebar() {
position,
);
if (clicked === "toggle-pin") {
toggleThreadPinned(threadId);
return;
}
if (clicked === "rename") {
setRenamingThreadId(threadId);
setRenamingTitle(thread.title);
@@ -899,7 +925,9 @@ export default function Sidebar() {
deleteThread,
markThreadUnread,
projectCwdById,
threadPinnedById,
threads,
toggleThreadPinned,
],
);
@@ -1018,11 +1046,19 @@ export default function Sidebar() {
const clicked = await api.contextMenu.show(
[
{
id: "toggle-pin",
label: projectPinnedById[projectId] ? "Unpin project" : "Pin project",
},
{ id: "copy-path", label: "Copy Project Path" },
{ id: "delete", label: "Remove project", destructive: true },
],
position,
);
if (clicked === "toggle-pin") {
toggleProjectPinned(projectId);
return;
}
if (clicked === "copy-path") {
copyPathToClipboard(project.cwd, { path: project.cwd });
return;
@@ -1068,8 +1104,10 @@ export default function Sidebar() {
clearProjectDraftThreadId,
copyPathToClipboard,
getDraftThreadByProjectId,
projectPinnedById,
projects,
threads,
toggleProjectPinned,
],
);
@@ -1443,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
@@ -1669,6 +1712,18 @@ export default function Sidebar() {
<span className="flex-1 truncate text-xs font-medium text-foreground/90">
{project.name}
</span>
{project.pinned ? (
<span
className="inline-flex shrink-0"
title={
isManualProjectSorting
? "Pinned project (inactive in manual sorting)"
: "Pinned project"
}
>
<PinIcon className="size-3 text-muted-foreground/55" />
</span>
) : null}
</SidebarMenuButton>
<Tooltip>
<TooltipTrigger
+5 -1
View File
@@ -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,
+74
View File
@@ -8,6 +8,8 @@ import {
setProjectExpanded,
syncProjects,
syncThreads,
toggleProjectPinned,
toggleThreadPinned,
type UiState,
} from "./uiStateStore";
@@ -15,7 +17,9 @@ function makeUiState(overrides: Partial<UiState> = {}): UiState {
return {
projectExpandedById: {},
projectOrder: [],
projectPinnedById: {},
threadLastVisitedAtById: {},
threadPinnedById: {},
...overrides,
};
}
@@ -110,6 +114,33 @@ describe("uiStateStore pure functions", () => {
expect(next.projectExpandedById[recreatedProject2]).toBe(false);
});
it("syncProjects preserves pinned state when a project is recreated with the same cwd", () => {
const oldProject1 = ProjectId.makeUnsafe("project-1");
const oldProject2 = ProjectId.makeUnsafe("project-2");
const recreatedProject2 = ProjectId.makeUnsafe("project-2b");
const initialState = syncProjects(
makeUiState({
projectPinnedById: {
[oldProject2]: true,
},
}),
[
{ id: oldProject1, cwd: "/tmp/project-1" },
{ id: oldProject2, cwd: "/tmp/project-2" },
],
);
const next = syncProjects(initialState, [
{ id: oldProject1, cwd: "/tmp/project-1" },
{ id: recreatedProject2, cwd: "/tmp/project-2" },
]);
expect(next.projectPinnedById).toEqual({
[oldProject1]: false,
[recreatedProject2]: true,
});
});
it("syncProjects returns a new state when only project cwd changes", () => {
const project1 = ProjectId.makeUnsafe("project-1");
const initialState = syncProjects(
@@ -162,6 +193,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({
@@ -177,16 +225,42 @@ describe("uiStateStore pure functions", () => {
expect(next.projectOrder).toEqual([project1]);
});
it("toggleProjectPinned adds and removes pinned state", () => {
const project1 = ProjectId.makeUnsafe("project-1");
const pinned = toggleProjectPinned(makeUiState(), project1);
expect(pinned.projectPinnedById).toEqual({ [project1]: true });
const unpinned = toggleProjectPinned(pinned, project1);
expect(unpinned.projectPinnedById).toEqual({ [project1]: false });
});
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({ [thread1]: false });
});
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({});
});
});
+93 -2
View File
@@ -19,15 +19,19 @@ const LEGACY_PERSISTED_STATE_KEYS = [
interface PersistedUiState {
expandedProjectCwds?: string[];
projectOrderCwds?: string[];
pinnedProjectCwds?: string[];
pinnedThreadIds?: string[];
}
export interface UiProjectState {
projectExpandedById: Record<string, boolean>;
projectOrder: ProjectId[];
projectPinnedById: Record<string, boolean>;
}
export interface UiThreadState {
threadLastVisitedAtById: Record<string, string>;
threadPinnedById: Record<string, boolean>;
}
export interface UiState extends UiProjectState, UiThreadState {}
@@ -45,11 +49,15 @@ export interface SyncThreadInput {
const initialState: UiState = {
projectExpandedById: {},
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;
@@ -80,6 +88,8 @@ function readPersistedState(): UiState {
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);
@@ -90,6 +100,16 @@ function hydratePersistedProjectState(parsed: PersistedUiState): void {
persistedProjectOrderCwds.push(cwd);
}
}
for (const cwd of parsed.pinnedProjectCwds ?? []) {
if (typeof cwd === "string" && cwd.length > 0) {
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 {
@@ -107,11 +127,22 @@ function persistState(state: UiState): void {
const cwd = currentProjectCwdById.get(projectId);
return cwd ? [cwd] : [];
});
const pinnedProjectCwds = Object.entries(state.projectPinnedById)
.filter(([, pinned]) => pinned)
.flatMap(([projectId]) => {
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) {
@@ -161,7 +192,9 @@ export function syncProjects(state: UiState, projects: readonly SyncProjectInput
projects.some((project) => previousProjectCwdById.get(project.id) !== project.cwd);
const nextExpandedById: Record<string, boolean> = {};
const nextPinnedById: Record<string, boolean> = {};
const previousExpandedById = state.projectExpandedById;
const previousPinnedById = state.projectPinnedById;
const persistedOrderByCwd = new Map(
persistedProjectOrderCwds.map((cwd, index) => [cwd, index] as const),
);
@@ -173,7 +206,12 @@ export function syncProjects(state: UiState, projects: readonly SyncProjectInput
(persistedExpandedProjectCwds.size > 0
? persistedExpandedProjectCwds.has(project.cwd)
: true);
const pinned =
previousPinnedById[project.id] ??
(previousProjectIdForCwd ? previousPinnedById[previousProjectIdForCwd] : undefined) ??
persistedPinnedProjectCwds.has(project.cwd);
nextExpandedById[project.id] = expanded;
nextPinnedById[project.id] = pinned;
return {
id: project.id,
cwd: project.cwd,
@@ -233,6 +271,7 @@ export function syncProjects(state: UiState, projects: readonly SyncProjectInput
if (
recordsEqual(state.projectExpandedById, nextExpandedById) &&
projectOrdersEqual(state.projectOrder, nextProjectOrder) &&
recordsEqual(state.projectPinnedById, nextPinnedById) &&
!cwdMappingChanged
) {
return state;
@@ -242,6 +281,7 @@ export function syncProjects(state: UiState, projects: readonly SyncProjectInput
...state,
projectExpandedById: nextExpandedById,
projectOrder: nextProjectOrder,
projectPinnedById: nextPinnedById,
};
}
@@ -252,6 +292,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 &&
@@ -260,13 +305,20 @@ export function syncThreads(state: UiState, threads: readonly SyncThreadInput[])
) {
nextThreadLastVisitedAtById[thread.id] = thread.seedVisitedAt;
}
if (nextThreadPinnedById[thread.id] === undefined) {
nextThreadPinnedById[thread.id] = persistedPinnedThreadIds.has(thread.id);
}
}
if (recordsEqual(state.threadLastVisitedAtById, nextThreadLastVisitedAtById)) {
if (
recordsEqual(state.threadLastVisitedAtById, nextThreadLastVisitedAtById) &&
recordsEqual(state.threadPinnedById, nextThreadPinnedById)
) {
return state;
}
return {
...state,
threadLastVisitedAtById: nextThreadLastVisitedAtById,
threadPinnedById: nextThreadPinnedById,
};
}
@@ -317,14 +369,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]) {
nextThreadPinnedById[threadId] = false;
} else {
nextThreadPinnedById[threadId] = true;
}
if (recordsEqual(state.threadPinnedById, nextThreadPinnedById)) {
return state;
}
return {
...state,
threadPinnedById: nextThreadPinnedById,
};
}
@@ -381,13 +452,31 @@ export function reorderProjects(
};
}
export function toggleProjectPinned(state: UiState, projectId: ProjectId): UiState {
const nextProjectPinnedById = { ...state.projectPinnedById };
if (nextProjectPinnedById[projectId]) {
nextProjectPinnedById[projectId] = false;
} else {
nextProjectPinnedById[projectId] = true;
}
if (recordsEqual(state.projectPinnedById, nextProjectPinnedById)) {
return state;
}
return {
...state,
projectPinnedById: nextProjectPinnedById,
};
}
interface UiStateStore extends UiState {
syncProjects: (projects: readonly SyncProjectInput[]) => void;
syncThreads: (threads: readonly SyncThreadInput[]) => void;
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;
reorderProjects: (draggedProjectId: ProjectId, targetProjectId: ProjectId) => void;
}
@@ -401,7 +490,9 @@ 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) =>
set((state) => setProjectExpanded(state, projectId, expanded)),
reorderProjects: (draggedProjectId, targetProjectId) =>