@@ -61,6 +61,7 @@ import {
6161} from "@t3tools/client-runtime/state/subagent-display" ;
6262import type { WorkflowOpening } from "@t3tools/client-runtime/state/subagentRuntime" ;
6363import { observeResize } from "~/lib/observeResize" ;
64+ import { useStableCallback } from "~/hooks/useStableCallback" ;
6465
6566const NOOP_USE_ARTIFACT_TEMPLATE = ( ) => { } ;
6667const NOOP_OPEN_ATTACHMENT = ( _attachment : ChatFileAttachment ) => { } ;
@@ -208,6 +209,8 @@ import {
208209 resolveTimelineIsAtEnd ,
209210 resolveTimelineMinimapHasPersistentGutter ,
210211 resolveTimelineMinimapCurrentIndex ,
212+ isTimelineMinimapRowVisible ,
213+ resolveTimelineMinimapVisibleRange ,
211214 resolveTimelineMinimapHeightStyle ,
212215 resolveTimelineMinimapHitStripWidth ,
213216 resolveTimelineMinimapIndexFromPointer ,
@@ -870,6 +873,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
870873 // must not change with them or every timeline row re-renders per event.
871874 const runs = useStableHandoffRuns ( runsProp ) ;
872875 const minimapItems = deriveTimelineMinimapItems ( rows ) ;
876+ const minimapRowIndices = minimapItems . map ( ( item ) => item . rowIndex ) ;
873877 // Preserve initialScrollAtEnd's measured target until the list is reused
874878 // for another thread; scrollToEnd would replace it with an estimated offset.
875879 const listOpenedOnRef = useRef < {
@@ -1126,20 +1130,67 @@ const ConversationTimeline = memo(function ConversationTimeline({
11261130 onContentOverflowChange ( measureContentOverflow ( ) ) ;
11271131 } ) ;
11281132 } ;
1129- const handleItemSizeChanged = ( ) => {
1130- if ( disclosurePinsEndRef . current ) {
1131- queueMicrotask ( pinDisclosureToEnd ) ;
1132- scheduleDisclosureSettle ( ) ;
1133- }
1134- reportContentOverflow ( ) ;
1135- } ;
11361133 useEffect ( ( ) => cancelContentOverflowFrame , [ cancelContentOverflowFrame ] ) ;
11371134 useLayoutEffect ( ( ) => {
11381135 cancelContentOverflowFrame ( ) ;
11391136 onContentOverflowChange ?.( measureContentOverflow ( ) ) ;
11401137 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Added or removed rows change the content height.
11411138 } , [ cancelContentOverflowFrame , measureContentOverflow , onContentOverflowChange , rows . length ] ) ;
11421139
1140+ const refreshMinimap = useStableCallback ( ( ) => {
1141+ const state = listRef . current ?. getState ?.( ) ;
1142+ if ( restoringThreadPosition || state ?. data !== rows ) return ;
1143+ if ( ! state || minimapItems . length === 0 ) {
1144+ return ;
1145+ }
1146+
1147+ const firstBufferedIndex = state . startBuffered ;
1148+ const firstBufferedRow : HTMLElement | undefined =
1149+ firstBufferedIndex == null ? undefined : state . elementAtIndex ( firstBufferedIndex ) ;
1150+ const firstBufferedTop =
1151+ firstBufferedIndex == null ? undefined : state . positionAtIndex ( firstBufferedIndex ) ;
1152+ const viewport : HTMLElement | undefined = listRef . current ?. getScrollableNode ( ) ;
1153+ // Cached row positions exclude the header and pending scroll adjustments.
1154+ const scroll =
1155+ firstBufferedRow && viewport && firstBufferedTop !== undefined
1156+ ? firstBufferedTop +
1157+ viewport . getBoundingClientRect ( ) . top -
1158+ firstBufferedRow . getBoundingClientRect ( ) . top
1159+ : state . scroll ;
1160+ const visibleRange = resolveTimelineMinimapVisibleRange ( { ...state , scroll } ) ;
1161+ for ( const item of minimapItems ) {
1162+ const strip = minimapStripMap . get ( item . id ) ;
1163+ const inView = isTimelineMinimapRowVisible (
1164+ item . rowIndex ,
1165+ visibleRange . start ,
1166+ visibleRange . end ,
1167+ ) ;
1168+ const next = inView ? "true" : "false" ;
1169+ // Avoid dirtying style state on every scroll tick for unchanged strips.
1170+ if ( strip && strip . dataset . inView !== next ) {
1171+ strip . dataset . inView = next ;
1172+ }
1173+ }
1174+ const nextCurrentIndex = resolveTimelineMinimapCurrentIndex ( {
1175+ visibleStart : visibleRange . start ,
1176+ visibleEnd : visibleRange . end ,
1177+ rowIndices : minimapRowIndices ,
1178+ } ) ;
1179+ setMinimapCurrentIndex ( ( current ) =>
1180+ current === nextCurrentIndex ? current : nextCurrentIndex ,
1181+ ) ;
1182+ } ) ;
1183+
1184+ const minimapRefreshFrameRef = useRef < number | null > ( null ) ;
1185+ const scheduleMinimapRefresh = ( ) => {
1186+ if ( minimapRefreshFrameRef . current !== null ) return ;
1187+ // LegendList recycles row containers before React commits their positions.
1188+ minimapRefreshFrameRef . current = requestAnimationFrame ( ( ) => {
1189+ minimapRefreshFrameRef . current = null ;
1190+ refreshMinimap ( ) ;
1191+ } ) ;
1192+ } ;
1193+
11431194 const handleScroll = ( ) => {
11441195 const state = listRef . current ?. getState ?.( ) ;
11451196 if ( restoringThreadPosition || state ?. data !== rows ) return ;
@@ -1172,45 +1223,28 @@ const ConversationTimeline = memo(function ConversationTimeline({
11721223 onIsAtEndChange ( isAtEnd ) ;
11731224 }
11741225 reportContentOverflow ( ) ;
1175- if ( ! state || minimapItems . length === 0 ) {
1176- return ;
1177- }
1178-
1179- const scrollTop = state . scroll ?? 0 ;
1180- const scrollBottom = scrollTop + ( state . scrollLength ?? 0 ) ;
1181-
1182- const itemBounds = minimapItems . map ( ( item ) => ( {
1183- top : resolveTimelineRowTop ( state , item . rowIndex ) ,
1184- height : resolveTimelineRowHeight ( state , item . rowIndex ) ,
1185- } ) ) ;
1226+ scheduleMinimapRefresh ( ) ;
1227+ } ;
11861228
1187- for ( const [ index , item ] of minimapItems . entries ( ) ) {
1188- const strip = minimapStripMap . get ( item . id ) ;
1189- const bounds = itemBounds [ index ] ;
1190- const rowTop = bounds ?. top ?? null ;
1191- const rowHeight = bounds ?. height ?? null ;
1192- const inView =
1193- rowTop !== null &&
1194- rowTop < scrollBottom &&
1195- rowTop + Math . max ( 1 , rowHeight ?? 1 ) > scrollTop ;
1196-
1197- // Skip no-op attribute writes: this runs for every strip on every scroll
1198- // tick, and rewriting an unchanged attribute still dirties style state.
1199- const next = inView ? "true" : "false" ;
1200- if ( strip && strip . dataset . inView !== next ) {
1201- strip . dataset . inView = next ;
1202- }
1229+ const handleItemSizeChanged = ( ) => {
1230+ if ( disclosurePinsEndRef . current ) {
1231+ queueMicrotask ( pinDisclosureToEnd ) ;
1232+ scheduleDisclosureSettle ( ) ;
12031233 }
1204- const nextCurrentIndex = resolveTimelineMinimapCurrentIndex ( {
1205- scrollTop,
1206- scrollBottom,
1207- itemBounds,
1208- } ) ;
1209- setMinimapCurrentIndex ( ( current ) =>
1210- current === nextCurrentIndex ? current : nextCurrentIndex ,
1211- ) ;
1234+ reportContentOverflow ( ) ;
1235+ scheduleMinimapRefresh ( ) ;
12121236 } ;
12131237
1238+ useEffect (
1239+ ( ) => ( ) => {
1240+ if ( minimapRefreshFrameRef . current !== null ) {
1241+ cancelAnimationFrame ( minimapRefreshFrameRef . current ) ;
1242+ minimapRefreshFrameRef . current = null ;
1243+ }
1244+ } ,
1245+ [ ] ,
1246+ ) ;
1247+
12141248 useEffect ( ( ) => {
12151249 const frame = requestAnimationFrame ( handleScroll ) ;
12161250 return ( ) => cancelAnimationFrame ( frame ) ;
@@ -1613,24 +1647,6 @@ function getItemType(item: MessagesTimelineRow) {
16131647 return `message:${ item . message . role } :${ lengthBucket } ` ;
16141648}
16151649
1616- interface TimelinePositionState {
1617- readonly contentLength ?: number ;
1618- readonly scroll ?: number ;
1619- readonly scrollLength ?: number ;
1620- readonly positionAtIndex ?: ( index : number ) => number | undefined ;
1621- readonly sizeAtIndex ?: ( index : number ) => number | undefined ;
1622- }
1623-
1624- function resolveTimelineRowTop ( state : TimelinePositionState , rowIndex : number ) {
1625- const top = state . positionAtIndex ?.( rowIndex ) ;
1626- return typeof top === "number" && Number . isFinite ( top ) ? top : null ;
1627- }
1628-
1629- function resolveTimelineRowHeight ( state : TimelinePositionState , rowIndex : number ) {
1630- const height = state . sizeAtIndex ?.( rowIndex ) ;
1631- return typeof height === "number" && Number . isFinite ( height ) ? height : null ;
1632- }
1633-
16341650function timelineMinimapEventTargetsPreview ( target : EventTarget ) : boolean {
16351651 return target instanceof Element && target . closest ( "[data-minimap-preview]" ) !== null ;
16361652}
0 commit comments