feat(sidebar): sync pinned thread order #5528
Closedf-trycua(GitHub profile, opens in a new tab)wants to merge 9 commits into
main fromrefs/pull/5528/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/web/src/hooks/useThreadActions.ts lines 549 to 549OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -532,3 +541,9 @@532541 [unpinThreadMutation],533542 );534543544+ const reorderPinnedThread = useCallback(545+ async (target: ScopedThreadRef, pinnedOrder: PinnedThreadOrder) => {546+ if (!readEnvironmentSupportsPinReordering(target.environmentId)) {547+ return AsyncResult.failure(548+ Cause.fail(549+ new ThreadPinningUnsupportedError({
- packages/client-runtime/src/state/threadSort.ts line 244Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -104,0 +192,21 @@192+ */193+export function pinnedThreadOrderForMove<T extends PinnedThreadSortInput>(194+ orderedThreads: readonly T[],195+ threadId: string,196+ overThreadId: string,197+): PinnedThreadOrder | null {198+ const fromIndex = orderedThreads.findIndex((thread) => pinnedThreadIdentity(thread) === threadId);199+ const toIndex = orderedThreads.findIndex(200+ (thread) => pinnedThreadIdentity(thread) === overThreadId,201+ );202+ if (fromIndex < 0 || toIndex < 0 || fromIndex === toIndex) return null;203+204+ const reordered = [...orderedThreads];205+ const [moved] = reordered.splice(fromIndex, 1);206+ if (!moved) return null;207+ reordered.splice(toIndex, 0, moved);208+ const movedIndex = reordered.indexOf(moved);209+ const previous = movedIndex > 0 ? reordered[movedIndex - 1] : undefined;210+ const next = movedIndex + 1 < reordered.length ? reordered[movedIndex + 1] : undefined;211+212+ if (previous && next) {apps/server/src/orchestration/projector.ts line 404OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -402,2 +403,2 @@402403 ...nextBase,403404 threads: updateThread(nextBase.threads, payload.threadId, {apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx line 561Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -536,3 +547,4 @@536547 threads,537548 selectedProjectScope,538549 ]);550+ const orderedPinnedThreads = useMemo(apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx line 586Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -536,3 +547,8 @@536547 threads,537548 selectedProjectScope,538549 ]);550+ const orderedPinnedThreads = useMemo(551+ () => threadListV2Layout.items.filter((item) => item.pinned).map((item) => item.thread),552+ [threadListV2Layout.items],553+ );554+ const movePinnedThread = useCallback(
- apps/server/src/orchestration/projector.ts line 406OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -402,3 +403,4 @@402403 ...nextBase,403404 threads: updateThread(nextBase.threads, payload.threadId, {404405 pinnedAt: payload.pinnedAt,406+ pinnedOrder: null,
- packages/client-runtime/src/state/threadSort.ts line 244Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -104,0 +218,21 @@218+ const toIndex = orderedThreads.findIndex(219+ (thread) => pinnedThreadIdentity(thread) === overThreadId,220+ );221+ if (fromIndex < 0 || toIndex < 0 || fromIndex === toIndex) return null;222+223+ const reordered = [...orderedThreads];224+ const [moved] = reordered.splice(fromIndex, 1);225+ if (!moved) return null;226+ reordered.splice(toIndex, 0, moved);227+ return { reordered, moved };228+}229+230+function boundedOrderForMovedThread<T extends PinnedThreadSortInput>(231+ reordered: readonly T[],232+ moved: T,233+): PinnedThreadOrder | null {234+ const movedIndex = reordered.indexOf(moved);235+ const previous = movedIndex > 0 ? reordered[movedIndex - 1] : undefined;236+ const next = movedIndex + 1 < reordered.length ? reordered[movedIndex + 1] : undefined;237+238+ if (previous && next) {apps/mobile/src/features/home/HomeScreen.tsx line 723Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -645,0 +677,21 @@677+ (candidate) =>678+ candidate.environmentId === thread.environmentId && candidate.id === thread.id,679+ );680+ const target = orderedPinnedThreads[index + (direction === "up" ? -1 : 1)];681+ if (index < 0 || !target) return;682+ const updates = pinnedThreadOrderUpdatesForMove(683+ orderedPinnedThreads,684+ scopedThreadKey(thread.environmentId, thread.id),685+ scopedThreadKey(target.environmentId, target.id),686+ );687+ if (updates === null) return;688+ const requests = updates.flatMap((update) => {689+ const updateThread = orderedPinnedThreads.find(690+ (candidate) => scopedThreadKey(candidate.environmentId, candidate.id) === update.threadId,691+ );692+ return updateThread ? [{ update, thread: updateThread }] : [];693+ });694+ if (requests.length !== updates.length) return;695+ pinnedReorderInFlightRef.current = true;696+ setPinnedReorderInFlight(true);697+ void (async () => {
- apps/mobile/src/features/home/HomeScreen.tsx line 669Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -643,3 +661,9 @@643661 threadListV2Enabled,644662 v2ScopedProjectGroup,645663 ]);664+ const orderedPinnedThreads = useMemo(665+ () =>666+ sortPinnedThreads(667+ props.threads.filter((thread) => thread.archivedAt === null && thread.pinnedAt !== null),668+ ),669+ [props.threads],apps/web/src/components/SidebarV2.tsx line 1851OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1681,0 +1831,21 @@1831+ ({ result }) => result._tag === "Failure" && !isAtomCommandInterrupted(result),1832+ );1833+ if (failed?.result._tag === "Failure") {1834+ const error = squashAtomCommandFailure(failed.result);1835+ toastManager.add(1836+ stackedThreadToast({1837+ type: "error",1838+ title: "Failed to reorder pinned thread",1839+ description: error instanceof Error ? error.message : "An error occurred.",1840+ }),1841+ );1842+ }1843+ setPendingPinnedOrderByKey((current) => {1844+ const next = new Map(current);1845+ for (const { request } of results) {1846+ if (next.get(request.threadId) === request.pinnedOrder) {1847+ next.delete(request.threadId);1848+ }1849+ }1850+ return next;1851+ });
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/mobile/src/features/home/HomeScreen.tsx line 704Resolvedby f-trycua(GitHub profile, opens in a new tab)@@ -645,0 +668,21 @@668+ ),669+ [props.threads],670+ );671+ const pinnedReorderInFlightRef = useRef(false);672+ const [pinnedReorderInFlight, setPinnedReorderInFlight] = useState(false);673+ const handleMovePinnedThread = useCallback(674+ (thread: EnvironmentThreadShell, direction: "up" | "down") => {675+ if (pinnedReorderInFlightRef.current) return;676+ const index = orderedPinnedThreads.findIndex(677+ (candidate) =>678+ candidate.environmentId === thread.environmentId && candidate.id === thread.id,679+ );680+ const target = orderedPinnedThreads[index + (direction === "up" ? -1 : 1)];681+ if (index < 0 || !target) return;682+ const updates = pinnedThreadOrderUpdatesForMove(683+ orderedPinnedThreads,684+ scopedThreadKey(thread.environmentId, thread.id),685+ scopedThreadKey(target.environmentId, target.id),686+ );687+ if (updates === null) return;688+ const requests = updates.flatMap((update) => {
- apps/mobile/src/features/home/HomeScreen.tsx line 706OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -645,0 +686,21 @@686+ );687+ if (updates === null) return;688+ const requests = updates.flatMap((update) => {689+ const updateThread = orderedPinnedThreads.find(690+ (candidate) => scopedThreadKey(candidate.environmentId, candidate.id) === update.threadId,691+ );692+ return updateThread ? [{ update, thread: updateThread }] : [];693+ });694+ if (requests.length !== updates.length) return;695+ pinnedReorderInFlightRef.current = true;696+ setPinnedReorderInFlight(true);697+ void (async () => {698+ try {699+ const completed: typeof requests = [];700+ for (const request of requests) {701+ const succeeded = await props.onReorderPinnedThread(702+ request.thread,703+ request.update.pinnedOrder,704+ );705+ if (!succeeded) {706+ for (const applied of completed.toReversed()) {
- packages/client-runtime/src/state/threadSort.ts lines 179 to 179OutdatedResolvedby f-trycua(GitHub profile, opens in a new tab)@@ -104,0 +159,21 @@159+ : defaultPinnedThreadOrder(thread);160+}161+162+function compareRationalOrder(left: RationalOrder, right: RationalOrder): number {163+ const leftProduct = left.numerator * right.denominator;164+ const rightProduct = right.numerator * left.denominator;165+ return leftProduct < rightProduct ? -1 : leftProduct > rightProduct ? 1 : 0;166+}167+168+/** Sorts pinned threads by their synced position, falling back to the v2169+ * creation order for threads pinned by a pre-reordering client. */170+export function sortPinnedThreads<T extends PinnedThreadSortInput>(threads: readonly T[]): T[] {171+ return threads172+ .map((thread) => ({173+ thread,174+ identity: pinnedThreadIdentity(thread),175+ order: effectivePinnedThreadOrder(thread),176+ }))177+ .sort((left, right) => {178+ const order = compareRationalOrder(left.order, right.order);179+ return order !== 0 ? order : left.identity.localeCompare(right.identity);
- apps/web/src/components/SidebarV2.tsx line 2019Resolvedby f-trycua(GitHub profile, opens in a new tab)@@ -1755,0 +1967,21 @@1967+ })();1968+ },1969+ [pinnedThreads, reorderPinnedThread, serverConfigs],1970+ );1971+ const handlePinnedThreadDragEnd = useCallback(1972+ (event: DragEndEvent) => {1973+ const overId = event.over?.id;1974+ if (overId == null || event.active.id === overId) return;1975+ const activeId = String(event.active.id);1976+ const thread = pinnedThreads.find(1977+ (candidate) =>1978+ scopedThreadKey(scopeThreadRef(candidate.environmentId, candidate.id)) === activeId,1979+ );1980+ const overThread = pinnedThreads.find(1981+ (candidate) =>1982+ scopedThreadKey(scopeThreadRef(candidate.environmentId, candidate.id)) === String(overId),1983+ );1984+ if (thread && overThread) movePinnedThread(thread, overThread);1985+ },1986+ [movePinnedThread, pinnedThreads],1987+ );packages/client-runtime/src/state/threadReducer.ts line 176OutdatedResolvedby f-trycua(GitHub profile, opens in a new tab)@@ -173,3 +173,4 @@173173 thread: {174174 ...thread,175175 pinnedAt: event.payload.pinnedAt,176+ pinnedOrder: null,
- @@ -1755,0 +1982,21 @@1982+ : error instanceof Error1983+ ? error.message1984+ : "An error occurred.",1985+ }),1986+ );1987+ }1988+ if (anyFailure) {1989+ setPendingPinnedOrderByKey((current) => {1990+ const next = new Map(current);1991+ for (const { request } of results) {1992+ if (next.get(request.threadId) === request.pinnedOrder) {1993+ next.delete(request.threadId);1994+ }1995+ }1996+ return next;1997+ });1998+ }1999+ })();2000+ },2001+ [orderedPinnedThreads, pinnedOrderingFullyVisible, reorderPinnedThread, serverConfigs],2002+ );
maria-rcks closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
56403cff-effa-4cdc-a733-301b3a024692You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
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