Skip to content

Commit 5661c61

Browse files
t3dotggclaude
andauthored
feat(web): drag pinned threads into your own order (pingdotgg#5581)
Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 6d70e6d commit 5661c61

34 files changed

Lines changed: 1407 additions & 92 deletions

‎apps/mobile/src/features/home/HomeRouteScreen.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export function HomeRouteScreen() {
4444
unsnoozeThread,
4545
pinThread,
4646
unpinThread,
47+
movePinnedThread,
4748
unsettleThread,
4849
} = useThreadListActions();
4950
const pendingTasks = usePendingNewTasks();
@@ -170,6 +171,7 @@ export function HomeRouteScreen() {
170171
onUnsettleThread={unsettleThread}
171172
onPinThread={pinThread}
172173
onUnpinThread={unpinThread}
174+
onMovePinnedThread={movePinnedThread}
173175
onEnvironmentChange={setSelectedEnvironmentId}
174176
onProjectChange={setSelectedProjectKey}
175177
onOpenSettings={() => navigation.navigate("SettingsSheet", { screen: "Settings" })}

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
threadSearchMatchKey,
1212
type EnvironmentThreadSearchMatch,
1313
} from "@t3tools/client-runtime/state/thread-search";
14+
import { sortPinnedThreadsByOrderKey } from "@t3tools/client-runtime/state/thread-sort";
1415
import type {
1516
EnvironmentId,
1617
SidebarProjectGroupingMode,
@@ -110,6 +111,10 @@ interface HomeScreenProps {
110111
readonly onUnsettleThread: (thread: EnvironmentThreadShell) => void;
111112
readonly onPinThread: (thread: EnvironmentThreadShell) => Promise<boolean>;
112113
readonly onUnpinThread: (thread: EnvironmentThreadShell) => Promise<boolean>;
114+
readonly onMovePinnedThread: (
115+
thread: EnvironmentThreadShell,
116+
direction: "up" | "down",
117+
) => Promise<boolean>;
113118
readonly onSelectPendingTask: (pendingTask: PendingNewTask) => void;
114119
readonly onDeletePendingTask: (pendingTask: PendingNewTask) => void;
115120
readonly onNewThreadInProject: (project: EnvironmentProject) => void;
@@ -521,6 +526,12 @@ export function HomeScreen(props: HomeScreenProps) {
521526
},
522527
[props.onPinThread],
523528
);
529+
const handleMovePinnedThread = useCallback(
530+
(thread: EnvironmentThreadShell, direction: "up" | "down") => {
531+
void props.onMovePinnedThread(thread, direction);
532+
},
533+
[props.onMovePinnedThread],
534+
);
524535
const handleUnpinThread = useCallback(
525536
(thread: EnvironmentThreadShell) => {
526537
void props.onUnpinThread(thread);
@@ -595,6 +606,29 @@ export function HomeScreen(props: HomeScreenProps) {
595606
}
596607
return supported;
597608
}, [serverConfigs]);
609+
const pinReorderEnvironmentIds = useMemo(() => {
610+
const supported = new Set<EnvironmentId>();
611+
for (const [environmentId, config] of serverConfigs) {
612+
if (config.environment.capabilities.threadPinReorder === true) {
613+
supported.add(environmentId);
614+
}
615+
}
616+
return supported;
617+
}, [serverConfigs]);
618+
// Canonical arranged pinned order (reorder-capable threads only) for the
619+
// Move up/down position flags. Computed from all shells, not the rendered
620+
// list, so search/scope filtering never disables or misdirects a move.
621+
const arrangedPinnedKeys = useMemo(() => {
622+
const pinned = sortPinnedThreadsByOrderKey(
623+
props.threads.filter(
624+
(thread) =>
625+
thread.pinnedAt != null &&
626+
thread.archivedAt === null &&
627+
pinReorderEnvironmentIds.has(thread.environmentId),
628+
),
629+
);
630+
return pinned.map((thread) => `${thread.environmentId}:${thread.id}`);
631+
}, [pinReorderEnvironmentIds, props.threads]);
598632
const threadListV2Layout = useMemo(() => {
599633
if (!threadListV2Enabled)
600634
return {
@@ -781,11 +815,18 @@ export function HomeScreen(props: HomeScreenProps) {
781815
onSettleThread={handleSettleThread}
782816
snoozeSupported={snoozeEnvironmentIds.has(thread.environmentId)}
783817
pinningSupported={pinningEnvironmentIds.has(thread.environmentId)}
818+
pinReorderSupported={pinReorderEnvironmentIds.has(thread.environmentId)}
819+
canMovePinnedUp={arrangedPinnedKeys.indexOf(`${thread.environmentId}:${thread.id}`) > 0}
820+
canMovePinnedDown={(() => {
821+
const index = arrangedPinnedKeys.indexOf(`${thread.environmentId}:${thread.id}`);
822+
return index !== -1 && index < arrangedPinnedKeys.length - 1;
823+
})()}
784824
onSnoozeThread={handleSnoozeThread}
785825
onUnsnoozeThread={handleUnsnoozeThread}
786826
onUnsettleThread={handleUnsettleThread}
787827
onPinThread={handlePinThread}
788828
onUnpinThread={handleUnpinThread}
829+
onMovePinnedThread={handleMovePinnedThread}
789830
onChangeRequestState={handleChangeRequestState}
790831
projectCwd={
791832
projectCwdByKey.get(scopedProjectKey(thread.environmentId, thread.projectId)) ?? null
@@ -798,6 +839,8 @@ export function HomeScreen(props: HomeScreenProps) {
798839
[
799840
handleChangeRequestState,
800841
handleDeleteThread,
842+
arrangedPinnedKeys,
843+
handleMovePinnedThread,
801844
handlePinThread,
802845
handleSettleThread,
803846
handleSnoozeThread,
@@ -807,6 +850,7 @@ export function HomeScreen(props: HomeScreenProps) {
807850
handleSwipeableWillOpen,
808851
handleUnsettleThread,
809852
pinningEnvironmentIds,
853+
pinReorderEnvironmentIds,
810854
projectByKey,
811855
projectCwdByKey,
812856
props.onArchiveThread,

‎apps/mobile/src/features/home/useThreadListActions.ts‎

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ import { Alert } from "react-native";
88
import { showConfirmDialog } from "../../components/ConfirmDialogHost";
99
import { scopedThreadKey } from "../../lib/scopedEntities";
1010
import { refreshArchivedThreadsForEnvironment } from "../archive/useArchivedThreadSnapshots";
11+
import {
12+
pinOrderKeyBetween,
13+
planPinnedMove,
14+
sortPinnedThreadsByOrderKey,
15+
} from "@t3tools/client-runtime/state/thread-sort";
1116
import { appAtomRegistry } from "../../state/atom-registry";
1217
import { environmentServerConfigsAtom } from "../../state/server";
13-
import { threadEnvironment } from "../../state/threads";
18+
import { environmentThreadShells, threadEnvironment } from "../../state/threads";
1419
import { useAtomCommand } from "../../state/use-atom-command";
1520

1621
/** Version skew: never send settle/unsettle to a server that predates them
@@ -36,6 +41,13 @@ function environmentSupportsPinning(environmentId: EnvironmentThreadShell["envir
3641
);
3742
}
3843

44+
function environmentSupportsPinReorder(environmentId: EnvironmentThreadShell["environmentId"]) {
45+
return (
46+
appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
47+
.threadPinReorder === true
48+
);
49+
}
50+
3951
type ThreadListAction = "archive" | "unarchive" | "delete" | "settle" | "unsettle";
4052

4153
const ACTION_VERBS: Record<ThreadListAction, string> = {
@@ -211,6 +223,10 @@ export function useThreadListActions(): {
211223
readonly unsettleThread: (thread: EnvironmentThreadShell) => Promise<boolean>;
212224
readonly pinThread: (thread: EnvironmentThreadShell) => Promise<boolean>;
213225
readonly unpinThread: (thread: EnvironmentThreadShell) => Promise<boolean>;
226+
readonly movePinnedThread: (
227+
thread: EnvironmentThreadShell,
228+
direction: "up" | "down",
229+
) => Promise<boolean>;
214230
} {
215231
const executeAction = useThreadActionExecutor();
216232
const snoozeMutation = useAtomCommand(threadEnvironment.snooze, { reportFailure: false });
@@ -331,9 +347,21 @@ export function useThreadListActions(): {
331347
return false;
332348
}
333349
selectionHaptic();
350+
// Same placement as web: a fresh pin takes the top of the arranged
351+
// run. Servers that predate reordering get the bare pin (keyless).
352+
let orderKey: string | undefined;
353+
if (environmentSupportsPinReorder(thread.environmentId)) {
354+
const shells = appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
355+
let firstKey: string | null = null;
356+
for (const shell of shells) {
357+
if (shell.pinnedAt == null || shell.pinOrderKey == null) continue;
358+
if (firstKey === null || shell.pinOrderKey < firstKey) firstKey = shell.pinOrderKey;
359+
}
360+
orderKey = pinOrderKeyBetween(null, firstKey) ?? undefined;
361+
}
334362
const result = await pinMutation({
335363
environmentId: thread.environmentId,
336-
input: { threadId: thread.id },
364+
input: { threadId: thread.id, ...(orderKey !== undefined ? { orderKey } : {}) },
337365
});
338366
if (result._tag === "Failure") {
339367
const error = Cause.squash(result.cause);
@@ -378,6 +406,85 @@ export function useThreadListActions(): {
378406
[unpinMutation],
379407
);
380408

409+
// Move up / Move down for the pinned block. Computed against the CANONICAL
410+
// keyed pinned order (not the rendered list), so the move is valid even
411+
// while search or a project scope filters rows: the same fractional-key
412+
// scheme web dragging uses, one write to one thread per move (plus a
413+
// one-time section materialization when legacy keyless pins are involved).
414+
const reorderPinnedMutation = useAtomCommand(threadEnvironment.reorderPin, {
415+
reportFailure: false,
416+
});
417+
// One move at a time: a second tap before the first write's event lands
418+
// would plan from the same stale snapshot and silently collapse two moves
419+
// into one — same double-dispatch guard as snoozeThread.
420+
const movePinnedInFlightRef = useRef(false);
421+
const movePinnedThread = useCallback(
422+
async (thread: EnvironmentThreadShell, direction: "up" | "down") => {
423+
if (movePinnedInFlightRef.current) return false;
424+
if (!environmentSupportsPinReorder(thread.environmentId)) {
425+
Alert.alert(
426+
"Could not move thread",
427+
"This environment's server does not support pinned reordering yet. Update the server to reorder pins.",
428+
);
429+
return false;
430+
}
431+
const shells = appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
432+
const pinned = sortPinnedThreadsByOrderKey(
433+
shells.filter(
434+
(shell) =>
435+
shell.pinnedAt != null &&
436+
shell.archivedAt === null &&
437+
environmentSupportsPinReorder(shell.environmentId),
438+
),
439+
);
440+
const orderedIds = pinned.map((shell) => scopedThreadKey(shell.environmentId, shell.id));
441+
const assignments = planPinnedMove({
442+
orderedIds,
443+
keysById: new Map(
444+
pinned.map((shell) => [
445+
scopedThreadKey(shell.environmentId, shell.id),
446+
shell.pinOrderKey ?? null,
447+
]),
448+
),
449+
movedId: scopedThreadKey(thread.environmentId, thread.id),
450+
direction,
451+
});
452+
if (assignments === null || assignments.length === 0) return false;
453+
const shellByKey = new Map(
454+
pinned.map((shell) => [scopedThreadKey(shell.environmentId, shell.id), shell]),
455+
);
456+
selectionHaptic();
457+
movePinnedInFlightRef.current = true;
458+
try {
459+
for (const assignment of assignments) {
460+
const target = shellByKey.get(assignment.id);
461+
if (target === undefined) continue;
462+
const result = await reorderPinnedMutation({
463+
environmentId: target.environmentId,
464+
input: { threadId: target.id, orderKey: assignment.orderKey },
465+
});
466+
if (result._tag === "Failure") {
467+
const error = Cause.squash(result.cause);
468+
Alert.alert(
469+
"Could not move thread",
470+
error instanceof Error && error.message.trim().length > 0
471+
? error.message
472+
: "The pinned thread could not be moved.",
473+
);
474+
// No rollback: keys already written are valid orderings on their
475+
// own (each write is a complete, consistent placement), so a
476+
// partial materialization leaves the list sensible, not corrupt.
477+
return false;
478+
}
479+
}
480+
return true;
481+
} finally {
482+
movePinnedInFlightRef.current = false;
483+
}
484+
},
485+
[reorderPinnedMutation],
486+
);
487+
381488
const confirmDeleteThread = useConfirmDeleteThread(executeAction);
382489

383490
return {
@@ -389,6 +496,7 @@ export function useThreadListActions(): {
389496
unsettleThread,
390497
pinThread,
391498
unpinThread,
499+
movePinnedThread,
392500
};
393501
}
394502

‎apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { LegendList } from "@legendapp/list/react-native";
1111
import type { MenuAction } from "@react-native-menu/menu";
1212
import { useAtomValue } from "@effect/atom-react";
1313
import type { EnvironmentId } from "@t3tools/contracts";
14+
import { sortPinnedThreadsByOrderKey } from "@t3tools/client-runtime/state/thread-sort";
1415
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
1516
import type { LayoutChangeEvent, NativeScrollEvent, NativeSyntheticEvent } from "react-native";
1617
import { Platform, Pressable, StyleSheet, TextInput, View, useColorScheme } from "react-native";
@@ -209,6 +210,7 @@ function ThreadNavigationSidebarPane(
209210
unsettleThread,
210211
pinThread,
211212
unpinThread,
213+
movePinnedThread,
212214
} = useThreadListActions();
213215
const threadListV2Enabled = useThreadListV2Enabled();
214216
const pendingTasks = usePendingNewTasks();
@@ -494,6 +496,28 @@ function ThreadNavigationSidebarPane(
494496
}
495497
return supported;
496498
}, [serverConfigs]);
499+
const pinReorderEnvironmentIds = useMemo(() => {
500+
const supported = new Set<EnvironmentId>();
501+
for (const [environmentId, config] of serverConfigs) {
502+
if (config.environment.capabilities.threadPinReorder === true) {
503+
supported.add(environmentId);
504+
}
505+
}
506+
return supported;
507+
}, [serverConfigs]);
508+
// Canonical arranged pinned order for Move up/down flags — computed from
509+
// all shells so search/scope filtering never disables a valid move.
510+
const arrangedPinnedKeys = useMemo(() => {
511+
const pinned = sortPinnedThreadsByOrderKey(
512+
threads.filter(
513+
(thread) =>
514+
thread.pinnedAt != null &&
515+
thread.archivedAt === null &&
516+
pinReorderEnvironmentIds.has(thread.environmentId),
517+
),
518+
);
519+
return pinned.map((thread) => `${thread.environmentId}:${thread.id}`);
520+
}, [pinReorderEnvironmentIds, threads]);
497521
const threadListV2Layout = useMemo(() => {
498522
if (!threadListV2Enabled)
499523
return {
@@ -930,11 +954,20 @@ function ThreadNavigationSidebarPane(
930954
onSettleThread={settleThread}
931955
snoozeSupported={snoozeEnvironmentIds.has(thread.environmentId)}
932956
pinningSupported={pinningEnvironmentIds.has(thread.environmentId)}
957+
pinReorderSupported={pinReorderEnvironmentIds.has(thread.environmentId)}
958+
canMovePinnedUp={
959+
arrangedPinnedKeys.indexOf(`${thread.environmentId}:${thread.id}`) > 0
960+
}
961+
canMovePinnedDown={(() => {
962+
const index = arrangedPinnedKeys.indexOf(`${thread.environmentId}:${thread.id}`);
963+
return index !== -1 && index < arrangedPinnedKeys.length - 1;
964+
})()}
933965
onSnoozeThread={snoozeThread}
934966
onUnsnoozeThread={unsnoozeThread}
935967
onUnsettleThread={unsettleThread}
936968
onPinThread={pinThread}
937969
onUnpinThread={unpinThread}
970+
onMovePinnedThread={movePinnedThread}
938971
onChangeRequestState={handleChangeRequestState}
939972
projectCwd={projectCwdByKey.get(scopeKey) ?? null}
940973
onSwipeableClose={handleSwipeableClose}
@@ -1055,13 +1088,16 @@ function ThreadNavigationSidebarPane(
10551088
},
10561089
[
10571090
archiveThread,
1091+
arrangedPinnedKeys,
10581092
confirmDeletePendingTask,
10591093
confirmDeleteThread,
10601094
handleChangeRequestState,
10611095
handleSelectThread,
10621096
handleSwipeableClose,
10631097
handleSwipeableWillOpen,
1098+
movePinnedThread,
10641099
openPendingTask,
1100+
pinReorderEnvironmentIds,
10651101
pinThread,
10661102
pinningEnvironmentIds,
10671103
projectByKey,

0 commit comments

Comments
 (0)