@@ -8,9 +8,14 @@ import { Alert } from "react-native";
88import { showConfirmDialog } from "../../components/ConfirmDialogHost" ;
99import { scopedThreadKey } from "../../lib/scopedEntities" ;
1010import { refreshArchivedThreadsForEnvironment } from "../archive/useArchivedThreadSnapshots" ;
11+ import {
12+ pinOrderKeyBetween ,
13+ planPinnedMove ,
14+ sortPinnedThreadsByOrderKey ,
15+ } from "@t3tools/client-runtime/state/thread-sort" ;
1116import { appAtomRegistry } from "../../state/atom-registry" ;
1217import { environmentServerConfigsAtom } from "../../state/server" ;
13- import { threadEnvironment } from "../../state/threads" ;
18+ import { environmentThreadShells , threadEnvironment } from "../../state/threads" ;
1419import { 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+
3951type ThreadListAction = "archive" | "unarchive" | "delete" | "settle" | "unsettle" ;
4052
4153const 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
0 commit comments