From d72c52343bc11ea55ffc4761cfe48b8154f4a43c Mon Sep 17 00:00:00 2001 From: Badcuban <108198679+badcuban@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:48:12 -0500 Subject: [PATCH] fix(web): custom-sorted inbox groups keep their place when their top thread goes In Custom sort with the inbox grouped by project or computer, a group the user never dragged sat wherever its first thread fell in the custom order, so wrapping, archiving or pinning that thread could make another group jump ahead of it (#1015). Custom sort now remembers where it first showed each group, in a new device setting (inboxCustomGroupOrder) kept apart from the dragged order, so the other sorts still move undragged groups with their first row. A group new to the list goes last. Dragged groups still win. "Use automatic order" (now offered when Custom sort remembers places) and "Reset custom order" clear the remembered places so groups take them afresh. Closes #1015 --- .../settings/DesktopClientSettings.test.ts | 1 + apps/server/src/guide/topics.ts | 2 +- apps/web/src/components/Sidebar.logic.test.ts | 44 ++++++++++++ apps/web/src/components/Sidebar.logic.ts | 45 +++++++++++- apps/web/src/components/Sidebar.tsx | 72 +++++++++++++++++-- apps/web/src/localApi.test.ts | 2 + packages/contracts/src/settings.ts | 7 ++ 7 files changed, 164 insertions(+), 9 deletions(-) diff --git a/apps/desktop/src/settings/DesktopClientSettings.test.ts b/apps/desktop/src/settings/DesktopClientSettings.test.ts index 67848d35e..e785543b6 100644 --- a/apps/desktop/src/settings/DesktopClientSettings.test.ts +++ b/apps/desktop/src/settings/DesktopClientSettings.test.ts @@ -48,6 +48,7 @@ const clientSettings: ClientSettings = { inboxSort: "recent", inboxCustomOrder: [], inboxGroupOrder: [], + inboxCustomGroupOrder: [], sidebarProjectGroupingMode: "repository_path", sidebarProjectGroupingOverrides: { "environment-1:/tmp/project-a": "separate", diff --git a/apps/server/src/guide/topics.ts b/apps/server/src/guide/topics.ts index 372b84a0b..9e52e5cb1 100644 --- a/apps/server/src/guide/topics.ts +++ b/apps/server/src/guide/topics.ts @@ -45,7 +45,7 @@ export const GUIDE_TOPICS: ReadonlyArray = [ ], body: `A thread is one conversation with an agent in a project. Threads are listed in the sidebar; one that needs you, finished or failed says so there. In a narrow window the sidebar floats over the thread instead of sitting beside it: the sidebar button at the top left, or Cmd+B (Ctrl+B on Windows and Linux), opens it, and picking a thread or clicking the thread puts it away. A thread you scrolled up in opens where you left it when you come back, until Threadlines reloads; one that finished, failed or needs you since then opens at the end. -The sliders button beside the project picker at the top of the sidebar groups the list by project or computer and sorts it: recent activity, newest, oldest, title, or a custom order. In the custom order you drag rows to arrange them, or use Move up and Move down in a thread's menu, and only you move a row; new threads go to the bottom, and the same button can reset the order. Pinned threads stay above a thin line in every sort, under a project's lead chat, which comes first. When the list is grouped, drag a group's header to move the whole group, in any sort; its menu has Move up, Move down and Use automatic order. These orders are kept per device, so a phone has its own. +The sliders button beside the project picker at the top of the sidebar groups the list by project or computer and sorts it: recent activity, newest, oldest, title, or a custom order. In the custom order you drag rows to arrange them, or use Move up and Move down in a thread's menu, and only you move a row; new threads go to the bottom, a group keeps its place when its top thread goes, a new group goes last, and the same button can reset the order. Pinned threads stay above a thin line in every sort, under a project's lead chat, which comes first. When the list is grouped, drag a group's header to move the whole group, in any sort; its menu has Move up, Move down and Use automatic order. These orders are kept per device, so a phone has its own. A new thread starts either in the project folder as it is, or in a worktree of its own: a separate git checkout on a new branch, so its changes stay apart until you merge them. You pick per thread in the composer; the default is {place:threads.startIn}, and new worktrees branch from {place:threads.worktreesFrom}. A project's setup script runs once in each new worktree. diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.ts index b6adeec2e..a1e77b831 100644 --- a/apps/web/src/components/Sidebar.logic.test.ts +++ b/apps/web/src/components/Sidebar.logic.test.ts @@ -50,6 +50,8 @@ import { moveInInboxCustomOrder, trimInboxCustomOrder, orderInboxLiveRowsByGroup, + appendInboxGroupKeys, + inboxDrawnGroupOrder, countInboxPinDividers, moveInInboxGroupOrder, inboxCustomOrderPeers, @@ -1824,6 +1826,48 @@ describe("the inbox's sorts", () => { ]); }); + it("keeps a custom-sorted group in place when its top row goes, and puts a new group last", () => { + const groupOf = (row: { key: string; group: string }) => row.group; + const groupsOn = (rows: readonly { key: string; group: string }[]) => + groupInboxLiveRows(rows, groupOf).map((group) => group.key); + const draw = ( + rows: readonly { key: string; group: string }[], + sort: "custom" | "recent", + draggedOrder: readonly string[], + seen: readonly string[], + ) => + orderInboxLiveRowsByGroup( + rows, + groupOf, + inboxDrawnGroupOrder({ sort, draggedOrder, customSeenOrder: seen }), + ).map((row) => row.key); + + const first = [ + { key: "a1", group: "project:a" }, + { key: "b1", group: "project:b" }, + { key: "a2", group: "project:a" }, + ]; + const seen = appendInboxGroupKeys([], groupsOn(first)); + expect(seen).toEqual(["project:a", "project:b"]); + // a1 is wrapped, and a thread in a new project shows up. + const later = [ + { key: "b1", group: "project:b" }, + { key: "a2", group: "project:a" }, + { key: "c1", group: "project:c" }, + ]; + const seenLater = appendInboxGroupKeys(seen, groupsOn(later)); + expect(appendInboxGroupKeys(seenLater, groupsOn(later))).toBe(seenLater); + // A saved list with a repeat still takes a new group. + expect(appendInboxGroupKeys(["project:a", "project:a"], ["project:b"])).toEqual([ + "project:a", + "project:b", + ]); + expect(draw(later, "custom", [], seenLater)).toEqual(["a2", "b1", "c1"]); + // A dragged order still wins, and the other sorts follow each group's first row. + expect(draw(later, "custom", ["project:b"], seenLater)).toEqual(["b1", "a2", "c1"]); + expect(draw(later, "recent", [], seenLater)).toEqual(["b1", "a2", "c1"]); + }); + it("counts one line for each group that has pinned and unpinned rows", () => { const row = (group: string, pinned: boolean) => ({ group, diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index 12715f4a5..0bfab043f 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -2681,6 +2681,42 @@ export function orderInboxLiveRowsByGroup( .flatMap((group) => group.rows); } +/** + * `order` with the keys it lacks added at the end, in the order given. The same + * array when nothing is new, so a caller can tell a change by identity. + */ +export function appendInboxGroupKeys( + order: readonly string[], + keys: readonly string[], +): readonly string[] { + const placed = new Set(order); + let grew = false; + for (const key of keys) { + if (placed.has(key)) continue; + placed.add(key); + grew = true; + } + return grew ? [...placed] : order; +} + +/** + * The group order the inbox draws. In the custom sort, a group the user never + * dragged keeps the place it held when the sort first showed it, so it stays + * put when its top row is wrapped, archived or moved. In the other sorts it + * follows its first row, as {@link orderInboxLiveRowsByGroup} does. + */ +export function inboxDrawnGroupOrder(input: { + readonly sort: InboxSort; + /** The order the user dragged the headers into. */ + readonly draggedOrder: readonly string[]; + /** Where the custom sort first showed each group. */ + readonly customSeenOrder: readonly string[]; +}): readonly string[] { + return input.sort === "custom" + ? appendInboxGroupKeys(input.draggedOrder, input.customSeenOrder) + : input.draggedOrder; +} + /** * The saved group order with one header moved among the ones on screen. Every * group on screen gets a place, so moving one never makes another jump, and a @@ -2693,9 +2729,12 @@ export function moveInInboxGroupOrder(input: { readonly groupKey: string; readonly toIndex: number; }): string[] { - const placed = new Set(input.groupOrder); - const order = [...placed, ...input.shownGroups.filter((key) => !placed.has(key))]; - return tradeSavedPlaces(order, input.shownGroups, input.groupKey, input.toIndex); + return tradeSavedPlaces( + appendInboxGroupKeys(input.groupOrder, input.shownGroups), + input.shownGroups, + input.groupKey, + input.toIndex, + ); } /** diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index bf3748ab5..4e9633292 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -103,6 +103,8 @@ import { countInboxPinDividers, moveInInboxGroupOrder, orderInboxLiveRowsByGroup, + appendInboxGroupKeys, + inboxDrawnGroupOrder, inboxCustomOrderPeers, moveInInboxCustomOrder, trimInboxCustomOrder, @@ -175,7 +177,7 @@ import { SidebarVersionTag } from "./sidebar/SidebarVersionTag"; import { readEnvironmentApi } from "../environmentApi"; import { useRoomsEnabled } from "~/hooks/useRoomsEnabled"; import { NO_REMOTE_CHILDREN, useRemoteFamilyChildren } from "../hooks/useRemoteFamilyChildren"; -import { useSettings, useUpdateSettings } from "~/hooks/useSettings"; +import { useClientSettingsHydrated, useSettings, useUpdateSettings } from "~/hooks/useSettings"; import { useServerKeybindings } from "../rpc/serverState"; import { @@ -613,6 +615,8 @@ export default function Sidebar() { const inboxSort = useSettings((s) => s.inboxSort); const inboxCustomOrder = useSettings((s) => s.inboxCustomOrder); const inboxGroupOrder = useSettings((s) => s.inboxGroupOrder); + const savedCustomSeenGroups = useSettings((s) => s.inboxCustomGroupOrder); + const settingsHydrated = useClientSettingsHydrated(); // A family's children on computers this app can't see, as the family tabs read them too. const remoteFamilyChildren = useRemoteFamilyChildren(nowIso); const { updateSettings } = useUpdateSettings(); @@ -928,8 +932,53 @@ export default function Sidebar() { sidebarGroupingSetting === "computer" && environmentScopeOptions.length <= 1 ? "none" : sidebarGroupingSetting; + // The custom sort remembers where it first showed each group, so a group + // keeps its place when its top row goes; a group new to it goes last. + const customSeenGroups = useMemo( + () => + inboxSort !== "custom" || sidebarGrouping === "none" + ? savedCustomSeenGroups + : appendInboxGroupKeys( + savedCustomSeenGroups, + groupInboxLiveRows(liveEntriesBySort, (row) => + inboxLiveGroupKey(sidebarGrouping, row), + ).map((group) => group.key), + ), + [inboxSort, liveEntriesBySort, savedCustomSeenGroups, sidebarGrouping], + ); + // Saved once the computer's threads have loaded, so a half-loaded list doesn't + // fix the first places. The last attempt is kept so a failed save isn't + // retried on every render. + const customSeenGroupsSaveAttempt = useRef(null); + useEffect(() => { + if ( + !settingsHydrated || + !bootstrapComplete || + customSeenGroups === savedCustomSeenGroups || + customSeenGroups === customSeenGroupsSaveAttempt.current + ) { + return; + } + customSeenGroupsSaveAttempt.current = customSeenGroups; + updateSettings({ inboxCustomGroupOrder: customSeenGroups }); + }, [ + bootstrapComplete, + customSeenGroups, + savedCustomSeenGroups, + settingsHydrated, + updateSettings, + ]); // Groups the user dragged keep that order; the fold and the jump labels read // the rows in it. + const drawnGroupOrder = useMemo( + () => + inboxDrawnGroupOrder({ + sort: inboxSort, + draggedOrder: inboxGroupOrder, + customSeenOrder: customSeenGroups, + }), + [customSeenGroups, inboxGroupOrder, inboxSort], + ); const liveEntries = useMemo( () => sidebarGrouping === "none" @@ -937,9 +986,9 @@ export default function Sidebar() { : orderInboxLiveRowsByGroup( liveEntriesBySort, (row) => inboxLiveGroupKey(sidebarGrouping, row), - inboxGroupOrder, + drawnGroupOrder, ), - [inboxGroupOrder, liveEntriesBySort, sidebarGrouping], + [drawnGroupOrder, liveEntriesBySort, sidebarGrouping], ); // What a closed Wrapped shelf keeps under its header: the thread open in // the chat, and the wrapped thread it sits under. The live list's fill @@ -1301,7 +1350,11 @@ export default function Sidebar() { "Reset your custom order?\n\nThreads go back to most recent first. You can't undo this.", ); if (confirmed) { - updateSettings({ inboxCustomOrder: seedInboxCustomOrder(inboxThreads, "recent") }); + // Groups take their places afresh from the reset rows. + updateSettings({ + inboxCustomOrder: seedInboxCustomOrder(inboxThreads, "recent"), + inboxCustomGroupOrder: [], + }); } }, [inboxThreads, updateSettings]); // The scope menu is memoized; these change with the inbox's threads, which @@ -1318,6 +1371,7 @@ export default function Sidebar() { liveEntries, inboxCustomOrder, inboxGroupOrder, + customSeenGroups, inboxThreads, sidebarThreads, sidebarGrouping, @@ -1375,7 +1429,12 @@ export default function Sidebar() { describeInboxGroupMenu({ canMoveUp: index > 0, canMoveDown: index < before.shownGroups.length - 1, - hasSavedOrder: before.inboxGroupOrder.some((key) => key.startsWith(kind)), + // In the custom sort, remembered places count too: resetting takes + // them afresh from where each group's first row stands now. + hasSavedOrder: [ + ...before.inboxGroupOrder, + ...(inboxSortRef.current === "custom" ? before.customSeenGroups : []), + ].some((key) => key.startsWith(kind)), }), position, ); @@ -1383,8 +1442,11 @@ export default function Sidebar() { const source = inboxMoveSourceRef.current; const current = source.shownGroups.indexOf(groupKey); if (clicked === "reset") { + // The custom sort's remembered places go too, so it takes them afresh + // from where each group's first row stands now. updateSettings({ inboxGroupOrder: source.inboxGroupOrder.filter((key) => !key.startsWith(kind)), + inboxCustomGroupOrder: source.customSeenGroups.filter((key) => !key.startsWith(kind)), }); } else if (clicked === "move-up" || clicked === "move-down") { const toIndex = current + (clicked === "move-up" ? -1 : 1); diff --git a/apps/web/src/localApi.test.ts b/apps/web/src/localApi.test.ts index decf27858..1e1e79d94 100644 --- a/apps/web/src/localApi.test.ts +++ b/apps/web/src/localApi.test.ts @@ -534,6 +534,7 @@ describe("wsApi", () => { inboxSort: "recent" as const, inboxCustomOrder: [], inboxGroupOrder: [], + inboxCustomGroupOrder: [], sidebarProjectGroupingMode: "repository_path" as const, sidebarProjectGroupingOverrides: { "environment-local:/tmp/project": "separate" as const, @@ -941,6 +942,7 @@ describe("wsApi", () => { inboxSort: "recent" as const, inboxCustomOrder: [], inboxGroupOrder: [], + inboxCustomGroupOrder: [], sidebarProjectGroupingMode: "repository_path" as const, sidebarProjectGroupingOverrides: { "environment-local:/tmp/project": "separate" as const, diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index 59bf11f5a..2e2bed007 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -288,6 +288,12 @@ export const ClientSettingsSchema = Schema.Struct({ // The order the user dragged the inbox's project and computer headers into, // as group keys. Empty: each group sits where its first row would. inboxGroupOrder: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(Effect.succeed([]))), + // Where each group stood when the custom sort first showed it, as group keys. + // The custom sort places groups the user never dragged by this, so a group + // stays put when its top row goes away; the other sorts ignore it. + inboxCustomGroupOrder: Schema.Array(Schema.String).pipe( + Schema.withDecodingDefault(Effect.succeed([])), + ), sidebarProjectGroupingMode: SidebarProjectGroupingMode.pipe( Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_PROJECT_GROUPING_MODE)), ), @@ -1404,6 +1410,7 @@ export const ClientSettingsPatch = Schema.Struct({ inboxSort: Schema.optionalKey(InboxSort), inboxCustomOrder: Schema.optionalKey(Schema.Array(Schema.String)), inboxGroupOrder: Schema.optionalKey(Schema.Array(Schema.String)), + inboxCustomGroupOrder: Schema.optionalKey(Schema.Array(Schema.String)), sidebarProjectGroupingMode: Schema.optionalKey(SidebarProjectGroupingMode), sidebarProjectGroupingOverrides: Schema.optionalKey( Schema.Record(TrimmedNonEmptyString, SidebarProjectGroupingMode),