Skip to content

Commit 907e62f

Browse files
JTBroadclaude
andcommitted
fix(web): keep sidebar thread order across restarts and make new rows draggable
Two separate defects in the v2 sidebar's drag-to-reorder. The persisted order was wiped on every app start. The prune effect only gated on "is an environment connected", but on a cold start the environment descriptor arrives before the thread snapshot does — leaving a window where every environment looks connected with zero live threads. pruneThreadOrder then dropped every persisted key, and the list fell back to the default created-date sort. Gate the effect on useAllEnvironmentShellsBootstrapped() so an absence of threads is only trusted once the snapshots have actually landed. Dragging a freshly created thread did nothing — the row moved and snapped back. reorderThreads can only splice keys already present in the order, and unranked threads (every new one, which render at the top per orderThreadsByPreferredKeys) are absent from it by design. The existing seed only applied when threadOrder was entirely empty, so it never helped once the user had dragged anything. Build the drag baseline by folding unranked rendered keys in at their displayed position, making any visible row draggable — including a thread that is actively working, which is usually a recently created one. Co-Authored-By: Claude <[email protected]>
1 parent 8dd20ba commit 907e62f

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

‎apps/web/src/components/SidebarV2.tsx‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ import { useClientSettings, useUpdateClientSettings } from "../hooks/useSettings
105105
import { useCopyToClipboard } from "../hooks/useCopyToClipboard";
106106
import { useNowMinute } from "../hooks/useNowMinute";
107107
import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments";
108-
import { useProjects, useThreadShells } from "../state/entities";
108+
import {
109+
useAllEnvironmentShellsBootstrapped,
110+
useProjects,
111+
useThreadShells,
112+
} from "../state/entities";
109113
import { environmentServerConfigsAtom, primaryServerKeybindingsAtom } from "../state/server";
110114
import { vcsEnvironment } from "../state/vcs";
111115
import { threadEnvironment } from "../state/threads";
@@ -1539,13 +1543,26 @@ export default function SidebarV2() {
15391543
// over a live connection, which makes the config map the sidebar's
15401544
// definition of "connected".
15411545
const connectedEnvironmentIds = useMemo(() => [...serverConfigs.keys()], [serverConfigs]);
1546+
// "Connected" is not the same as "has told us its threads yet": on a cold
1547+
// start the environment descriptor lands before the thread snapshot does, so
1548+
// for a beat every environment looks connected with zero live threads. Pruning
1549+
// in that window wipes the whole persisted order — the restart bug. Wait for
1550+
// the snapshots themselves before trusting an absence.
1551+
const shellsBootstrapped = useAllEnvironmentShellsBootstrapped();
15421552
useEffect(() => {
15431553
// No connected environment means no trustworthy liveness signal at all —
15441554
// during a reconnect every key would look dead. Skipping keeps a transient
15451555
// disconnect from wiping the order.
15461556
if (threadOrder.length === 0 || connectedEnvironmentIds.length === 0) return;
1557+
if (!shellsBootstrapped) return;
15471558
pruneThreadOrder(liveThreadKeys, connectedEnvironmentIds);
1548-
}, [connectedEnvironmentIds, liveThreadKeys, pruneThreadOrder, threadOrder.length]);
1559+
}, [
1560+
connectedEnvironmentIds,
1561+
liveThreadKeys,
1562+
pruneThreadOrder,
1563+
shellsBootstrapped,
1564+
threadOrder.length,
1565+
]);
15491566

15501567
// Only the active rows are sortable, so the SortableContext gets exactly
15511568
// their keys — the "Snoozed"/"Settled" header <li>s and the shelf rows are
@@ -1601,12 +1618,20 @@ export default function SidebarV2() {
16011618
? activeThreadDragIds.filter((key) => selectedThreadKeys.has(key))
16021619
: [activeKey];
16031620

1604-
// First drag has no persisted baseline: seed the order from the list as
1605-
// it is currently rendered so the splice has stable neighbours to land
1606-
// between. Seeding and moving happen in one reducer call — reading the
1607-
// store back after a set() in the same handler would still see the old
1608-
// (empty) order.
1609-
const currentThreadOrder = threadOrder.length > 0 ? threadOrder : activeThreadDragIds;
1621+
// `reorderThreads` can only splice keys that are already in the order, so
1622+
// the baseline has to describe every row on screen — not just the ranked
1623+
// ones. Unranked threads (every freshly created one, which render at the
1624+
// top per `orderThreadsByPreferredKeys`) were previously absent here, so
1625+
// dragging one found nothing to move and the row snapped back. Folding
1626+
// them in at their rendered position makes any visible row draggable and
1627+
// reproduces the list exactly as displayed. Seeding and moving happen in
1628+
// one reducer call — reading the store back after a set() in the same
1629+
// handler would still see the old order.
1630+
const rankedKeys = new Set(threadOrder);
1631+
const currentThreadOrder = [
1632+
...activeThreadDragIds.filter((key) => !rankedKeys.has(key)),
1633+
...threadOrder,
1634+
];
16101635
reorderThreads(currentThreadOrder, draggedKeys, [overKey]);
16111636
suppressThreadClickAfterDragRef.current = true;
16121637
},

0 commit comments

Comments
 (0)