1- import { For , Show , createMemo , createSignal , onCleanup } from "solid-js" ;
1+ import { For , Show , createEffect , createMemo , createSignal , onCleanup } from "solid-js" ;
22import type { JSX } from "solid-js" ;
33import type { Part } from "@opencode-ai/sdk/v2/client" ;
44import { Check , ChevronDown , ChevronRight , Copy , Eye , File , FileEdit , FolderSearch , Pencil , Search , Sparkles , Terminal } from "lucide-solid" ;
5+ import { createVirtualizer } from "@tanstack/solid-virtual" ;
56
67import type { MessageGroup , MessageWithParts , StepGroupMode } from "../../types" ;
78import { groupMessageParts , isUserVisiblePart , summarizeStep } from "../../utils" ;
@@ -20,6 +21,8 @@ export type MessageListProps = {
2021 activeSearchMessageId ?: string | null ;
2122 searchHighlightQuery ?: string ;
2223 workspaceRoot ?: string ;
24+ scrollElement ?: ( ) => HTMLElement | null | undefined ;
25+ virtualizationThreshold ?: number ;
2326 footer ?: JSX . Element ;
2427} ;
2528
@@ -427,6 +430,43 @@ export default function MessageList(props: MessageListProps) {
427430 } ;
428431 } ;
429432
433+ const blockContainsMessageId = ( block : MessageBlockItem , messageId : string ) => {
434+ if ( ! messageId ) return false ;
435+ if ( block . kind === "steps-cluster" ) {
436+ return block . messageIds . includes ( messageId ) ;
437+ }
438+ return block . messageId === messageId ;
439+ } ;
440+
441+ const virtualizationThreshold = ( ) => props . virtualizationThreshold ?? 30 ;
442+ const shouldVirtualize = createMemo ( ( ) => messageBlocks ( ) . length >= virtualizationThreshold ( ) ) ;
443+
444+ const virtualizer = createVirtualizer < HTMLElement , HTMLDivElement > ( {
445+ get count ( ) {
446+ return shouldVirtualize ( ) ? messageBlocks ( ) . length : 0 ;
447+ } ,
448+ getScrollElement : ( ) => props . scrollElement ?.( ) ?? null ,
449+ estimateSize : ( ) => 220 ,
450+ overscan : 6 ,
451+ getItemKey : ( index ) => {
452+ const block = messageBlocks ( ) [ index ] ;
453+ if ( ! block ) return `block-${ index } ` ;
454+ if ( block . kind === "steps-cluster" ) {
455+ return `steps-${ block . messageIds . join ( "," ) } ` ;
456+ }
457+ return `message-${ block . messageId } ` ;
458+ } ,
459+ } ) ;
460+
461+ createEffect ( ( ) => {
462+ if ( ! shouldVirtualize ( ) ) return ;
463+ const activeMessageId = props . activeSearchMessageId ?. trim ( ) ;
464+ if ( ! activeMessageId ) return ;
465+ const index = messageBlocks ( ) . findIndex ( ( block ) => blockContainsMessageId ( block , activeMessageId ) ) ;
466+ if ( index < 0 ) return ;
467+ virtualizer . scrollToIndex ( index , { align : "center" } ) ;
468+ } ) ;
469+
430470 /** Compact single-line step row */
431471 const StepRow = ( rowProps : { part : Part ; isUser : boolean ; groupMode ?: StepGroupMode } ) => {
432472 const summary = createMemo ( ( ) => summarizeStep ( rowProps . part ) ) ;
@@ -809,10 +849,7 @@ export default function MessageList(props: MessageListProps) {
809849 ) ;
810850 } ;
811851
812- return (
813- < div class = "space-y-4 pb-24" style = { { contain : "layout paint style" } } >
814- < For each = { messageBlocks ( ) } >
815- { ( block , blockIndex ) => {
852+ const renderBlock = ( block : MessageBlockItem , blockIndex : number ) => {
816853 const blockMessageIds = block . kind === "steps-cluster" ? block . messageIds : [ block . messageId ] ;
817854 const hasSearchMatch = blockMessageIds . some ( ( id ) => props . searchMatchMessageIds ?. has ( id ) ) ;
818855 const hasActiveSearchMatch = blockMessageIds . some ( ( id ) => id === props . activeSearchMessageId ) ;
@@ -828,7 +865,7 @@ export default function MessageList(props: MessageListProps) {
828865 class = { `flex group ${ block . isUser ? "justify-end" : "justify-start" } ` . trim ( ) }
829866 data-message-role = { block . isUser ? "user" : "assistant" }
830867 data-message-id = { block . messageIds [ 0 ] ?? "" }
831- style = { blockPerfStyle ( blockIndex ( ) ) }
868+ style = { blockPerfStyle ( blockIndex ) }
832869 >
833870 < div
834871 class = { `w-full relative ${
@@ -854,7 +891,7 @@ export default function MessageList(props: MessageListProps) {
854891 class = { `flex group ${ block . isUser ? "justify-end" : "justify-start" } ` . trim ( ) }
855892 data-message-role = { block . isUser ? "user" : "assistant" }
856893 data-message-id = { block . messageId }
857- style = { blockPerfStyle ( blockIndex ( ) ) }
894+ style = { blockPerfStyle ( blockIndex ) }
858895 >
859896 < div
860897 class = { `w-full relative ${
@@ -951,8 +988,40 @@ export default function MessageList(props: MessageListProps) {
951988 </ div >
952989 </ div >
953990 ) ;
954- } }
955- </ For >
991+ } ;
992+
993+ return (
994+ < div class = "space-y-4 pb-24" style = { { contain : "layout paint style" } } >
995+ < Show
996+ when = { shouldVirtualize ( ) }
997+ fallback = {
998+ < For each = { messageBlocks ( ) } >
999+ { ( block , index ) => renderBlock ( block , index ( ) ) }
1000+ </ For >
1001+ }
1002+ >
1003+ < div style = { { height : `${ virtualizer . getTotalSize ( ) } px` , position : "relative" } } >
1004+ < For each = { virtualizer . getVirtualItems ( ) } >
1005+ { ( item ) => {
1006+ const block = ( ) => messageBlocks ( ) [ item . index ] ;
1007+ return (
1008+ < div
1009+ ref = { ( el ) => virtualizer . measureElement ( el ) }
1010+ style = { {
1011+ position : "absolute" ,
1012+ top : "0" ,
1013+ left : "0" ,
1014+ width : "100%" ,
1015+ transform : `translateY(${ item . start } px)` ,
1016+ } }
1017+ >
1018+ < Show when = { block ( ) } > { ( value ) => renderBlock ( value ( ) , item . index ) } </ Show >
1019+ </ div >
1020+ ) ;
1021+ } }
1022+ </ For >
1023+ </ div >
1024+ </ Show >
9561025 < Show when = { props . footer } > { props . footer } </ Show >
9571026 </ div >
9581027 ) ;
0 commit comments