1+ import { createThreadMovePlanner } from "../threads/threadOrder" ;
12import {
23 LegendList ,
34 type LegendListRef ,
@@ -11,7 +12,6 @@ import {
1112 threadSearchMatchKey ,
1213 type EnvironmentThreadSearchMatch ,
1314} from "@t3tools/client-runtime/state/thread-search" ;
14- import { sortPinnedThreadsByOrderKey } from "@t3tools/client-runtime/state/thread-sort" ;
1515import {
1616 type EnvironmentId ,
1717 resolveEnvironmentMachineKind ,
@@ -35,6 +35,7 @@ import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
3535import { mobilePreferencesAtom , updateMobilePreferencesAtom } from "../../state/preferences" ;
3636import { useThreadSearch } from "../../state/queries" ;
3737import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled" ;
38+ import { usePendingThreadOrder } from "../../state/thread-order" ;
3839import { environmentServerConfigsAtom } from "../../state/server" ;
3940import type { PendingNewTask } from "../../state/use-pending-new-tasks" ;
4041import {
@@ -51,6 +52,7 @@ import {
5152} from "../threads/thread-list-v2-items" ;
5253import {
5354 buildThreadListV2Items ,
55+ getThreadListV2OrderedSection ,
5456 buildThreadListV2ListItems ,
5557 THREAD_LIST_V2_SETTLED_INITIAL_COUNT ,
5658 THREAD_LIST_V2_SETTLED_PAGE_COUNT ,
@@ -114,7 +116,7 @@ interface HomeScreenProps {
114116 readonly onUnsettleThread : ( thread : EnvironmentThreadShell ) => void ;
115117 readonly onPinThread : ( thread : EnvironmentThreadShell ) => Promise < boolean > ;
116118 readonly onUnpinThread : ( thread : EnvironmentThreadShell ) => Promise < boolean > ;
117- readonly onMovePinnedThread : (
119+ readonly onMoveThread : (
118120 thread : EnvironmentThreadShell ,
119121 direction : "up" | "down" ,
120122 ) => Promise < boolean > ;
@@ -512,11 +514,11 @@ export function HomeScreen(props: HomeScreenProps) {
512514 } ,
513515 [ props . onPinThread ] ,
514516 ) ;
515- const handleMovePinnedThread = useCallback (
517+ const handleMoveThread = useCallback (
516518 ( thread : EnvironmentThreadShell , direction : "up" | "down" ) => {
517- void props . onMovePinnedThread ( thread , direction ) ;
519+ void props . onMoveThread ( thread , direction ) ;
518520 } ,
519- [ props . onMovePinnedThread ] ,
521+ [ props . onMoveThread ] ,
520522 ) ;
521523 const handleUnpinThread = useCallback (
522524 ( thread : EnvironmentThreadShell ) => {
@@ -608,6 +610,15 @@ export function HomeScreen(props: HomeScreenProps) {
608610 }
609611 return supported ;
610612 } , [ serverConfigs ] ) ;
613+ const activeReorderEnvironmentIds = useMemo ( ( ) => {
614+ const supported = new Set < EnvironmentId > ( ) ;
615+ for ( const [ environmentId , config ] of serverConfigs ) {
616+ if ( config . environment . capabilities . threadActiveReorder === true ) {
617+ supported . add ( environmentId ) ;
618+ }
619+ }
620+ return supported ;
621+ } , [ serverConfigs ] ) ;
611622 const titleRegenerationEnvironmentIds = useMemo ( ( ) => {
612623 const supported = new Set < EnvironmentId > ( ) ;
613624 for ( const [ environmentId , config ] of serverConfigs ) {
@@ -627,20 +638,40 @@ export function HomeScreen(props: HomeScreenProps) {
627638 ) ,
628639 [ serverConfigs ] ,
629640 ) ;
630- // Canonical arranged pinned order (reorder-capable threads only) for the
631- // Move up/down position flags. Computed from all shells, not the rendered
632- // list, so search/scope filtering never disables or misdirects a move.
633- const arrangedPinnedKeys = useMemo ( ( ) => {
634- const pinned = sortPinnedThreadsByOrderKey (
635- props . threads . filter (
636- ( thread ) =>
637- thread . pinnedAt != null &&
638- thread . archivedAt === null &&
639- pinReorderEnvironmentIds . has ( thread . environmentId ) ,
640- ) ,
641- ) ;
642- return pinned . map ( ( thread ) => `${ thread . environmentId } :${ thread . id } ` ) ;
643- } , [ pinReorderEnvironmentIds , props . threads ] ) ;
641+ const pendingOrder = usePendingThreadOrder ( nowMinute , snoozeWakeTick ) ;
642+ const threadMovePlanners = useMemo ( ( ) => {
643+ const sectionPlanner = ( section : "pinned" | "active" ) =>
644+ createThreadMovePlanner ( {
645+ allThreads : props . threads ,
646+ section,
647+ reorderableEnvironmentIds : new Set (
648+ [ ...serverConfigs ] . flatMap ( ( [ id , config ] ) =>
649+ ( section === "pinned"
650+ ? config . environment . capabilities . threadPinReorder
651+ : config . environment . capabilities . threadActiveReorder ) === true
652+ ? [ id ]
653+ : [ ] ,
654+ ) ,
655+ ) ,
656+ ordered : getThreadListV2OrderedSection ( {
657+ threads : props . threads ,
658+ section,
659+ pendingOrder,
660+ now : new Date ( ) . toISOString ( ) ,
661+ settlementEnvironmentIds,
662+ snoozeEnvironmentIds,
663+ } ) ,
664+ } ) ;
665+ return { pinned : sectionPlanner ( "pinned" ) , active : sectionPlanner ( "active" ) } ;
666+ } , [
667+ serverConfigs ,
668+ props . threads ,
669+ pendingOrder ,
670+ settlementEnvironmentIds ,
671+ snoozeEnvironmentIds ,
672+ nowMinute ,
673+ snoozeWakeTick ,
674+ ] ) ;
644675 const threadListV2Layout = useMemo ( ( ) => {
645676 if ( ! threadListV2Enabled )
646677 return {
@@ -655,6 +686,7 @@ export function HomeScreen(props: HomeScreenProps) {
655686 // Settled threads are live shells; archived threads keep their original
656687 // "hidden from lists" meaning.
657688 return buildThreadListV2Items ( {
689+ pendingOrder,
658690 threads : props . threads . filter ( ( thread ) => thread . archivedAt === null ) ,
659691 environmentId : props . selectedEnvironmentId ,
660692 projectRefs : v2ScopedProjectGroup === null ? null : v2ScopedProjectGroup . projectRefs ,
@@ -669,6 +701,7 @@ export function HomeScreen(props: HomeScreenProps) {
669701 selectedThreadKey : null ,
670702 } ) ;
671703 } , [
704+ pendingOrder ,
672705 nowMinute ,
673706 snoozeWakeTick ,
674707 snoozedShelfExpanded ,
@@ -784,6 +817,8 @@ export function HomeScreen(props: HomeScreenProps) {
784817 ) ;
785818 }
786819 const thread = item . item . thread ;
820+ const movePlanner = item . item . pinned ? threadMovePlanners . pinned : threadMovePlanners . active ;
821+ const movedId = `${ thread . environmentId } :${ thread . id } ` ;
787822 return (
788823 < ThreadListV2Row
789824 thread = { thread }
@@ -830,27 +865,30 @@ export function HomeScreen(props: HomeScreenProps) {
830865 onSettleThread = { handleSettleThread }
831866 snoozeSupported = { snoozeEnvironmentIds . has ( thread . environmentId ) }
832867 pinningSupported = { pinningEnvironmentIds . has ( thread . environmentId ) }
833- pinReorderSupported = { pinReorderEnvironmentIds . has ( thread . environmentId ) }
834- canMovePinnedUp = { arrangedPinnedKeys . indexOf ( `${ thread . environmentId } :${ thread . id } ` ) > 0 }
835- canMovePinnedDown = { ( ( ) => {
836- const index = arrangedPinnedKeys . indexOf ( `${ thread . environmentId } :${ thread . id } ` ) ;
837- return index !== - 1 && index < arrangedPinnedKeys . length - 1 ;
838- } ) ( ) }
868+ reorderSupported = {
869+ item . item . pinned
870+ ? pinReorderEnvironmentIds . has ( thread . environmentId )
871+ : activeReorderEnvironmentIds . has ( thread . environmentId )
872+ }
873+ canMoveUp = { pendingOrder === null && movePlanner ( movedId , "up" ) !== null }
874+ canMoveDown = { pendingOrder === null && movePlanner ( movedId , "down" ) !== null }
839875 onSnoozeThread = { handleSnoozeThread }
840876 onUnsnoozeThread = { handleUnsnoozeThread }
841877 onUnsettleThread = { handleUnsettleThread }
842878 onPinThread = { handlePinThread }
843879 onUnpinThread = { handleUnpinThread }
844- onMovePinnedThread = { handleMovePinnedThread }
880+ onMoveThread = { handleMoveThread }
845881 onSwipeableClose = { handleSwipeableClose }
846882 onSwipeableWillOpen = { handleSwipeableWillOpen }
847883 />
848884 ) ;
849885 } ,
850886 [
851887 handleDeleteThread ,
852- arrangedPinnedKeys ,
853- handleMovePinnedThread ,
888+ activeReorderEnvironmentIds ,
889+ threadMovePlanners ,
890+ pendingOrder ,
891+ handleMoveThread ,
854892 handlePinThread ,
855893 handleRegenerateThreadTitle ,
856894 handleSettleThread ,
0 commit comments