1+ import type { ThreadMoveDestination } from "../threads/threadOrder" ;
12import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell" ;
2- import { canSnooze } from "@t3tools/client-runtime/state/thread-settled" ;
3+ import { canSnooze , effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled" ;
34import * as Cause from "effect/Cause" ;
45import * as Haptics from "expo-haptics" ;
56import { useCallback , useRef } from "react" ;
@@ -15,8 +16,16 @@ import { environmentServerConfigsAtom } from "../../state/server";
1516import { environmentThreadShells , threadEnvironment } from "../../state/threads" ;
1617import { queuedThreadKeysAtom } from "../../state/use-thread-outbox" ;
1718import { useAtomCommand } from "../../state/use-atom-command" ;
18- import { beginPendingThreadOrder , getPendingThreadOrder } from "../../state/thread-order" ;
19- import { createPendingThreadOrder , createThreadMovePlanner } from "../threads/threadOrder" ;
19+ import {
20+ beginPendingThreadOrder ,
21+ getPendingThreadOrder ,
22+ threadDropBusyAtom ,
23+ } from "../../state/thread-order" ;
24+ import {
25+ createPendingThreadOrder ,
26+ createThreadMovePlanner ,
27+ threadDropLifecycle ,
28+ } from "../threads/threadOrder" ;
2029import { getThreadListV2OrderedSection } from "../threads/threadListV2" ;
2130
2231/** Version skew: never send settle/unsettle to a server that predates them
@@ -229,7 +238,7 @@ export function useThreadListActions(): {
229238 readonly unpinThread : ( thread : EnvironmentThreadShell ) => Promise < boolean > ;
230239 readonly moveThread : (
231240 thread : EnvironmentThreadShell ,
232- direction : "up" | "down" ,
241+ direction : ThreadMoveDestination ,
233242 ) => Promise < boolean > ;
234243 readonly regenerateThreadTitle : ( thread : EnvironmentThreadShell ) => Promise < boolean > ;
235244} {
@@ -474,9 +483,29 @@ export function useThreadListActions(): {
474483 reportFailure : false ,
475484 } ) ;
476485 const moveThread = useCallback (
477- async ( thread : EnvironmentThreadShell , direction : "up" | "down" ) => {
478- if ( getPendingThreadOrder ( ) !== null ) return false ;
479- const section = thread . pinnedAt != null ? "pinned" : "active" ;
486+ async ( thread : EnvironmentThreadShell , direction : ThreadMoveDestination ) => {
487+ if ( getPendingThreadOrder ( ) !== null || appAtomRegistry . get ( threadDropBusyAtom ) ) return false ;
488+ const shells = appAtomRegistry . get ( environmentThreadShells . threadShellsAtom ) ;
489+ const current = shells . find (
490+ ( row ) => row . id === thread . id && row . environmentId === thread . environmentId ,
491+ ) ;
492+ if ( ! current || current . archivedAt !== null ) return false ;
493+ thread = current ;
494+ const section =
495+ typeof direction === "object" && direction . section !== undefined
496+ ? direction . section
497+ : thread . pinnedAt != null
498+ ? "pinned"
499+ : "active" ;
500+ if ( section === "settled" ) {
501+ if ( ! environmentSupportsSettlement ( thread . environmentId ) ) return false ;
502+ appAtomRegistry . set ( threadDropBusyAtom , true ) ;
503+ try {
504+ return await settleThread ( thread ) ;
505+ } finally {
506+ appAtomRegistry . set ( threadDropBusyAtom , false ) ;
507+ }
508+ }
480509 const configs = appAtomRegistry . get ( environmentServerConfigsAtom ) ;
481510 const supportsReorder = ( environmentId : EnvironmentThreadShell [ "environmentId" ] ) => {
482511 const capabilities = configs . get ( environmentId ) ?. environment . capabilities ;
@@ -491,7 +520,6 @@ export function useThreadListActions(): {
491520 ) ;
492521 return false ;
493522 }
494- const shells = appAtomRegistry . get ( environmentThreadShells . threadShellsAtom ) ;
495523 const ordered = getThreadListV2OrderedSection ( {
496524 threads : shells ,
497525 section,
@@ -515,24 +543,67 @@ export function useThreadListActions(): {
515543 reorderableEnvironmentIds : new Set ( [ ...configs . keys ( ) ] . filter ( supportsReorder ) ) ,
516544 } ) ( scopedThreadKey ( thread . environmentId , thread . id ) , direction ) ;
517545 if ( assignments === null ) return false ;
546+ const lifecycle = threadDropLifecycle ( thread , section , new Date ( ) . toISOString ( ) ) ;
547+ const crossSection = ! ordered . some (
548+ ( row ) => row . id === thread . id && row . environmentId === thread . environmentId ,
549+ ) ;
550+ if (
551+ crossSection &&
552+ ( ( ( section === "pinned" || thread . pinnedAt != null ) &&
553+ ! environmentSupportsPinning ( thread . environmentId ) ) ||
554+ ( thread . settledOverride === "settled" &&
555+ ! environmentSupportsSettlement ( thread . environmentId ) ) ||
556+ ( effectiveSnoozed ( thread , { now : new Date ( ) . toISOString ( ) } ) &&
557+ ! environmentSupportsSnooze ( thread . environmentId ) ) )
558+ )
559+ return false ;
518560 const shellByKey = new Map (
519- ordered . map ( ( shell ) => [ scopedThreadKey ( shell . environmentId , shell . id ) , shell ] ) ,
561+ shells . map ( ( shell ) => [ scopedThreadKey ( shell . environmentId , shell . id ) , shell ] ) ,
520562 ) ;
521563 selectionHaptic ( ) ;
522- const pending = beginPendingThreadOrder (
523- createPendingThreadOrder ( {
524- section,
525- ordered,
526- movedId : scopedThreadKey ( thread . environmentId , thread . id ) ,
527- direction,
528- assignments,
529- } ) ,
530- ) ;
564+ appAtomRegistry . set ( threadDropBusyAtom , true ) ;
565+ const pending = crossSection
566+ ? null
567+ : beginPendingThreadOrder (
568+ createPendingThreadOrder ( {
569+ section,
570+ ordered,
571+ movedId : scopedThreadKey ( thread . environmentId , thread . id ) ,
572+ direction,
573+ assignments,
574+ } ) ,
575+ ) ;
531576 let succeeded = false ;
532577 const reorder = section === "pinned" ? reorderPinnedMutation : reorderActiveMutation ;
533578 try {
579+ if ( crossSection ) {
580+ if ( section === "pinned" ) {
581+ const orderKey = assignments . find (
582+ ( { id } ) => id === scopedThreadKey ( thread . environmentId , thread . id ) ,
583+ ) ?. orderKey ;
584+ const result = await pinMutation ( {
585+ environmentId : thread . environmentId ,
586+ input : { threadId : thread . id , ...( orderKey === undefined ? { } : { orderKey } ) } ,
587+ } ) ;
588+ if ( result . _tag === "Failure" ) {
589+ Alert . alert ( "Could not pin thread" , String ( Cause . squash ( result . cause ) ) ) ;
590+ return false ;
591+ }
592+ } else {
593+ if ( lifecycle . unpin && ! ( await unpinThread ( thread ) ) ) return false ;
594+ if ( lifecycle . unsettle && ! ( await unsettleThread ( thread ) ) ) return false ;
595+ if ( lifecycle . unsnooze && ! ( await unsnoozeThread ( thread ) ) ) return false ;
596+ }
597+ }
534598 for ( const assignment of assignments ) {
535- if ( ! pending . isPending ( ) ) return false ;
599+ if (
600+ crossSection &&
601+ section === "pinned" &&
602+ thread . pinnedAt == null &&
603+ assignment . id === scopedThreadKey ( thread . environmentId , thread . id )
604+ )
605+ continue ;
606+ if ( pending !== null && ! pending . isPending ( ) ) return false ;
536607 const target = shellByKey . get ( assignment . id ) ;
537608 if ( target === undefined ) continue ;
538609 const result = await reorder ( {
@@ -552,13 +623,22 @@ export function useThreadListActions(): {
552623 }
553624 }
554625 succeeded = true ;
555- pending . complete ( ) ;
626+ pending ? .complete ( ) ;
556627 return true ;
557628 } finally {
558- if ( ! succeeded ) pending . cancel ( ) ;
629+ if ( ! succeeded ) pending ?. cancel ( ) ;
630+ appAtomRegistry . set ( threadDropBusyAtom , false ) ;
559631 }
560632 } ,
561- [ reorderActiveMutation , reorderPinnedMutation ] ,
633+ [
634+ settleThread ,
635+ reorderActiveMutation ,
636+ reorderPinnedMutation ,
637+ pinMutation ,
638+ unpinThread ,
639+ unsettleThread ,
640+ unsnoozeThread ,
641+ ] ,
562642 ) ;
563643
564644 const confirmDeleteThread = useConfirmDeleteThread ( executeAction ) ;
0 commit comments