11import type { OrchestrationThreadShell , ProjectId } from "@t3tools/contracts" ;
22import type { SidebarProjectSortOrder , SidebarThreadSortOrder } from "@t3tools/contracts/settings" ;
3- import * as Arr from "effect/Array" ;
4- import * as Order from "effect/Order" ;
53
64export interface ThreadSortInput {
75 readonly createdAt : string ;
@@ -121,19 +119,15 @@ export function sortThreads<T extends { readonly id: string } & ThreadSortInput>
121119 threads : readonly T [ ] ,
122120 sortOrder : SidebarThreadSortOrder ,
123121) : T [ ] {
124- return Arr . sort (
125- threads ,
126- Order . mapInput (
127- Order . Struct ( {
128- timestamp : Order . flip ( Order . Number ) ,
129- id : Order . flip ( Order . String ) ,
130- } ) ,
131- ( thread : T ) => ( {
132- timestamp : getThreadSortTimestamp ( thread , sortOrder ) ,
133- id : thread . id ,
134- } ) ,
135- ) ,
136- ) ;
122+ if ( threads . length < 2 ) return [ ...threads ] ;
123+ return threads
124+ . map ( ( thread ) => ( { thread, timestamp : getThreadSortTimestamp ( thread , sortOrder ) } ) )
125+ . sort (
126+ ( left , right ) =>
127+ right . timestamp - left . timestamp ||
128+ ( left . thread . id < right . thread . id ? 1 : left . thread . id > right . thread . id ? - 1 : 0 ) ,
129+ )
130+ . map ( ( { thread } ) => thread ) ;
137131}
138132
139133export function getLatestThreadForProject <
@@ -143,12 +137,21 @@ export function getLatestThreadForProject<
143137 readonly archivedAt : string | null ;
144138 } & ThreadSortInput ,
145139> ( threads : readonly T [ ] , projectId : ProjectId , sortOrder : SidebarThreadSortOrder ) : T | null {
146- return (
147- sortThreads (
148- threads . filter ( ( thread ) => thread . projectId === projectId && thread . archivedAt === null ) ,
149- sortOrder ,
150- ) [ 0 ] ?? null
151- ) ;
140+ let latest : T | null = null ;
141+ let latestTimestamp = Number . NEGATIVE_INFINITY ;
142+ for ( const thread of threads ) {
143+ if ( thread . projectId !== projectId || thread . archivedAt !== null ) continue ;
144+ const timestamp = getThreadSortTimestamp ( thread , sortOrder ) ;
145+ if (
146+ latest === null ||
147+ timestamp > latestTimestamp ||
148+ ( timestamp === latestTimestamp && thread . id > latest . id )
149+ ) {
150+ latest = thread ;
151+ latestTimestamp = timestamp ;
152+ }
153+ }
154+ return latest ;
152155}
153156
154157// ── Pinned reorder: fractional index keys ──────────────────────────────
@@ -290,6 +293,7 @@ export function sortPinnedThreadsByOrderKey<
290293 readonly environmentId ?: string | undefined ;
291294 } ,
292295> ( threads : readonly T [ ] ) : T [ ] {
296+ if ( threads . length < 2 ) return [ ...threads ] ;
293297 const keyed : T [ ] = [ ] ;
294298 const keyless : T [ ] = [ ] ;
295299 for ( const thread of threads ) {
@@ -303,14 +307,13 @@ export function sortPinnedThreadsByOrderKey<
303307 const rightKey = right . pinOrderKey ! ;
304308 return leftKey < rightKey ? - 1 : leftKey > rightKey ? 1 : identityTiebreak ( left , right ) ;
305309 } ) ;
306- keyless . sort ( ( left , right ) => {
307- const leftMs = Date . parse ( left . createdAt ) ;
308- const rightMs = Date . parse ( right . createdAt ) ;
309- return (
310- ( Number . isNaN ( rightMs ) ? 0 : rightMs ) - ( Number . isNaN ( leftMs ) ? 0 : leftMs ) ||
311- identityTiebreak ( left , right )
312- ) ;
313- } ) ;
310+ const timestamps = new Map (
311+ keyless . map ( ( thread ) => [ thread , toSortableTimestamp ( thread . createdAt ) ?? 0 ] ) ,
312+ ) ;
313+ keyless . sort (
314+ ( left , right ) =>
315+ timestamps . get ( right ) ! - timestamps . get ( left ) ! || identityTiebreak ( left , right ) ,
316+ ) ;
314317 return [ ...keyed , ...keyless ] ;
315318}
316319
@@ -325,6 +328,13 @@ export function sortActiveThreadsByOrderKey<
325328 readonly environmentId ?: string | undefined ;
326329 } ,
327330> ( threads : readonly T [ ] ) : T [ ] {
331+ if ( threads . length < 2 ) return [ ...threads ] ;
332+ const timestamps = new Map < T , number > ( ) ;
333+ for ( const thread of threads ) {
334+ if ( thread . activeOrderKey == null ) {
335+ timestamps . set ( thread , activeThreadAnchorTimestampMs ( thread ) ) ;
336+ }
337+ }
328338 return [ ...threads ] . sort ( ( left , right ) => {
329339 const leftKey = left . activeOrderKey ;
330340 const rightKey = right . activeOrderKey ;
@@ -334,7 +344,7 @@ export function sortActiveThreadsByOrderKey<
334344 if ( leftKey != null && rightKey != null ) {
335345 order = leftKey < rightKey ? - 1 : leftKey > rightKey ? 1 : 0 ;
336346 } else {
337- order = activeThreadAnchorTimestampMs ( right ) - activeThreadAnchorTimestampMs ( left ) ;
347+ order = timestamps . get ( right ) ! - timestamps . get ( left ) ! ;
338348 }
339349 return (
340350 order ||
0 commit comments