feat(web): drag pinned threads into your own order #5581
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 5 commits into
main fromrefs/pull/5581/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/web/src/components/Sidebar.logic.ts line 570OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -512,0 +550,21 @@550+ return PIN_ORDER_DIGITS.charAt(digitA) + pinOrderMidpoint(a.slice(1), "");551+}552+553+/** Key that sorts strictly between two neighbors; null bounds mean "top of554+ the pinned block" / "bottom of the keyed run". Returns null instead of555+ throwing when existing keys are corrupt or out of order — callers fall556+ back to rewriting the section. */557+export function pinOrderKeyBetween(before: string | null, after: string | null): string | null {558+ const a = before ?? "";559+ const b = after ?? "";560+ if (a !== "" && !isValidPinOrderKey(a)) return null;561+ if (b !== "" && !isValidPinOrderKey(b)) return null;562+ if (b !== "" && a >= b) return null;563+ return pinOrderMidpoint(a, b);564+}565+566+/** Evenly spaced keys for rewriting a whole pinned section (used when a567+ drop lands next to keyless threads, so single-key insertion has nothing568+ to anchor on). Two base-26 digits give 675 slots — far beyond any real569+ pinned section — with monotonicity enforced as a belt-and-braces. */570+export function generateSpreadPinOrderKeys(count: number): string[] {
- apps/web/src/components/SidebarV2.tsx line 2314OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2193,0 +2294,21 @@2294+ if (optimisticPinnedOrder === null) return pinnedThreads;2295+ return orderItemsByPreferredIds({2296+ items: pinnedThreads,2297+ preferredIds: optimisticPinnedOrder,2298+ getId: (thread) => scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),2299+ });2300+ }, [optimisticPinnedOrder, pinnedThreads]);2301+ useEffect(() => {2302+ if (optimisticPinnedOrder === null) return;2303+ const canonical = pinnedThreads2304+ .map((thread) => scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)))2305+ .filter((key) => reorderablePinnedKeys.has(key));2306+ const canonicalSet = new Set(canonical);2307+ const optimistic = optimisticPinnedOrder.filter((key) => canonicalSet.has(key));2308+ if (2309+ canonical.length === optimistic.length &&2310+ canonical.every((key, index) => key === optimistic[index])2311+ ) {2312+ setOptimisticPinnedOrder(null);2313+ }2314+ }, [optimisticPinnedOrder, pinnedThreads, reorderablePinnedKeys]);apps/mobile/src/features/threads/threadListV2.ts line 203OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -177,0 +183,21 @@183+ * order the user arranged on web.184+ */185+export function sortPinnedThreadsForListV2<186+ T extends {187+ readonly id: string;188+ readonly createdAt: string;189+ readonly pinOrderKey?: string | null | undefined;190+ },191+>(threads: readonly T[]): T[] {192+ const keyed: T[] = [];193+ const keyless: T[] = [];194+ for (const thread of threads) {195+ (thread.pinOrderKey != null ? keyed : keyless).push(thread);196+ }197+ keyed.sort((left, right) => {198+ const leftKey = left.pinOrderKey!;199+ const rightKey = right.pinOrderKey!;200+ return leftKey < rightKey ? -1 : leftKey > rightKey ? 1 : left.id.localeCompare(right.id);201+ });202+ return [...keyed, ...sortThreadsForListV2(keyless)];203+}apps/web/src/components/SidebarV2.tsx line 2419Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2193,0 +2345,21 @@2345+ return reorderPinnedThread(2346+ scopeThreadRef(thread.environmentId, thread.id),2347+ assignment.orderKey,2348+ );2349+ }),2350+ );2351+ const failure = results.find(2352+ (result): result is Extract<NonNullable<typeof result>, { _tag: "Failure" }> =>2353+ result?._tag === "Failure" && !isAtomCommandInterrupted(result),2354+ );2355+ if (failure !== undefined) {2356+ setOptimisticPinnedOrder(null);2357+ const error = squashAtomCommandFailure(failure);2358+ toastManager.add(2359+ stackedThreadToast({2360+ type: "error",2361+ title: "Failed to reorder pinned threads",2362+ description: error instanceof Error ? error.message : "An error occurred.",2363+ }),2364+ );2365+ }
- apps/web/src/components/SidebarV2.tsx line 2324OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2196,4 +2305,20 @@2305+ canonical.some((key) => !optimisticPinnedOrder.includes(key));2306+ const orderConfirmed =2307+ !membershipChanged && canonical.every((key, index) => key === optimisticPinnedOrder[index]);2308+ if (membershipChanged || orderConfirmed) {2309+ setOptimisticPinnedOrder(null);2310+ }2311+ }, [optimisticPinnedOrder, pinnedThreads, reorderablePinnedKeys]);21962312 const attemptPin = useCallback(21972313 (threadRef: ScopedThreadRef) => {21982314 void (async () => {2199- const result = await pinThread(threadRef);2315+ // Fresh pins take the top of the arranged run (newest pin most2316+ // prominent, matching the keyless creation-order feel). Anchored to2317+ // the DISPLAYED order so a pin during an in-flight drag lands above2318+ // what the user is looking at. Existing keys stay put; a null2319+ // fallback just means "keyless", which sorts with the legacy block,2320+ // so pinning never fails on key math.2321+ const firstKey =2322+ orderedPinnedThreads.find((thread) => thread.pinOrderKey != null)?.pinOrderKey ?? null;2323+ const orderKey = pinOrderKeyBetween(null, firstKey);2324+ const result = await pinThread(threadRef, orderKey === null ? {} : { orderKey });
- apps/web/src/hooks/useThreadActions.ts lines 596 to 603Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -546,3 +575,17 @@546575 [unpinThreadMutation],547576 );548577578+ const reorderPinnedThread = useCallback(579+ async (target: ScopedThreadRef, orderKey: string) => {580+ // Callers (the sidebar drag handler) only enable dragging on581+ // reorder-capable environments; this guard covers races around582+ // capability changes mid-drag.583+ if (!readEnvironmentSupportsPinReorder(target.environmentId)) {584+ return AsyncResult.failure(585+ Cause.fail(586+ new ThreadPinningUnsupportedError({587+ environmentId: target.environmentId,588+ threadId: target.threadId,589+ }),590+ ),591+ );
- apps/mobile/src/features/home/useThreadListActions.ts line 421Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -378,3 +406,12 @@378406 [unpinMutation],379407 );380408409+ // Move up / Move down for the pinned block. Computed against the CANONICAL410+ // keyed pinned order (not the rendered list), so the move is valid even411+ // while search or a project scope filters rows: the same fractional-key412+ // scheme web dragging uses, one write to one thread per move (plus a413+ // one-time section materialization when legacy keyless pins are involved).414+ const reorderPinnedMutation = useAtomCommand(threadEnvironment.reorderPin, {415+ reportFailure: false,416+ });417+ const movePinnedThread = useCallback(apps/mobile/src/features/threads/threadListV2.ts line 448Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -444,4 +445,4 @@444445 );445446446447 const items: ThreadListV2Item[] = [];447- for (const thread of sortThreadsForListV2(pinned)) {448+ for (const thread of sortPinnedThreadsByOrderKey(pinned)) {
- apps/server/src/orchestration/Layers/ProjectionPipeline.ts line 615Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -612,3 +612,4 @@612612 snoozedUntil: null,613613 snoozedAt: null,614614 pinnedAt: null,615+ pinOrderKey: null,apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts line 321OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -318,3 +318,4 @@318318 snoozedUntil: null,319319 snoozedAt: null,320320 pinnedAt: null,321+ pinOrderKey: null,apps/web/src/components/SidebarV2.tsx lines 411 to 429Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -391,2 +409,21 @@391409 }392410411+// Subset of useSortable applied to a pinned card's root <li>. Listeners go412+// on the whole card (no dedicated handle): the pointer sensor's distance413+// constraint keeps plain clicks working, and we skip dnd-kit's aria414+// attributes since there is no keyboard sensor and the card body already415+// carries its own button semantics.416+type SortablePinnedRowBag = Pick<417+ ReturnType<typeof useSortable>,418+ "listeners" | "setNodeRef" | "transform" | "transition" | "isDragging"419+>;420+421+function SortablePinnedThreadRow(props: {422+ id: string;423+ children: (bag: SortablePinnedRowBag) => ReactNode;424+}) {425+ const { listeners, setNodeRef, transform, transition, isDragging } = useSortable({426+ id: props.id,427+ });428+ return props.children({ listeners, setNodeRef, transform, transition, isDragging });429+}
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share