1+ import { LegendList , type LegendListRef } from "@legendapp/list/react-native" ;
12import { settlePromise } from "@t3tools/client-runtime/state/runtime" ;
23import { useAtomValue } from "@effect/atom-react" ;
34import { AsyncResult } from "effect/reactivity" ;
45import type { ComponentType } from "react" ;
56import { useLayoutEffect , useRef , useState } from "react" ;
67import {
7- FlatList ,
88 Platform ,
99 Pressable ,
1010 RefreshControl ,
@@ -13,6 +13,7 @@ import {
1313 useWindowDimensions ,
1414 View ,
1515} from "react-native" ;
16+ import { NativeScrollView } from "../../native/NativeScrollViewMarker" ;
1617import { WebView } from "react-native-webview" ;
1718import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1819import { AppText as Text } from "../../components/ui/AppText" ;
@@ -43,7 +44,6 @@ import {
4344 selectableSourceHtml ,
4445} from "./source-file-document" ;
4546import { sourceHighlightAtom } from "./sourceHighlightingState" ;
46- import { createSourceLineScroll } from "./source-file-scroll" ;
4747
4848interface SourceFileSurfaceProps {
4949 readonly contents : string ;
@@ -270,8 +270,7 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
270270 const { codeSurface, codeWordBreak } = useAppearanceCodeSurface ( ) ;
271271 const { normalizedContents, lines, status, targetIndex, tokens } = useSourceFileModel ( props ) ;
272272 const selectableTokens = props . selectable ? boundedSelectableSourceTokens ( tokens ) : null ;
273- const listRef = useRef < FlatList < string > > ( null ) ;
274- const [ lineScroll ] = useState ( createSourceLineScroll ) ;
273+ const listRef = useRef < LegendListRef > ( null ) ;
275274 const { isPullRefreshing, handlePullToRefresh } = useSourceFileRefresh ( props . onRefresh ) ;
276275 const refreshControl = props . onRefresh ? (
277276 < RefreshControl refreshing = { isPullRefreshing } onRefresh = { ( ) => void handlePullToRefresh ( ) } />
@@ -283,15 +282,21 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
283282 const usesTextSelection = props . selectable === true && ! usesWebSelection ;
284283
285284 // Layout effect so a pending jump is cancelled in the commit that swaps the file.
286- // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- scrolls the FlatList to the target line and cancels a pending jump when the file changes
285+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- scrolls the list to the target line and cancels a pending jump when the file changes
287286 useLayoutEffect ( ( ) => {
288287 if ( targetIndex === null || ! usesLineList ) {
289288 return ;
290289 }
291- lineScroll . scrollToLine ( listRef . current , targetIndex ) ;
292- return lineScroll . cancel ;
290+ const frame = requestAnimationFrame ( ( ) => {
291+ void listRef . current ?. scrollToIndex ( {
292+ index : targetIndex ,
293+ animated : false ,
294+ viewPosition : 0.3 ,
295+ } ) ;
296+ } ) ;
297+ return ( ) => cancelAnimationFrame ( frame ) ;
293298 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Opening another file at the same line index, or leaving selection mode, scrolls the remounted list to it.
294- } , [ lineScroll , props . path , targetIndex , usesLineList ] ) ;
299+ } , [ props . path , targetIndex , usesLineList ] ) ;
295300
296301 const { onLongPressLine } = props ;
297302 const renderLine = ( { item, index } : { item : string ; index : number } ) => (
@@ -385,33 +390,26 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
385390 ) : null ;
386391
387392 const list = (
388- < FlatList
393+ < LegendList
389394 ref = { listRef }
395+ // An embedded viewer sits inside another screen's scroll surface.
396+ renderScrollComponent = { props . embedded ? undefined : NativeScrollView }
390397 contentInsetAdjustmentBehavior = { usesAutomaticInsets ? "automatic" : "never" }
391398 automaticallyAdjustsScrollIndicatorInsets = { usesAutomaticInsets }
392399 refreshControl = { refreshControl }
393400 data = { lines }
394401 keyExtractor = { ( _line , index ) => String ( index ) }
395- initialNumToRender = { 80 }
396- maxToRenderPerBatch = { 80 }
397- windowSize = { 12 }
398- { ...( codeWordBreak
399- ? { }
400- : {
401- getItemLayout : ( _data , index ) => ( {
402- length : codeSurface . rowHeight ,
403- offset : codeSurface . rowHeight * index ,
404- index,
405- } ) ,
406- } ) }
402+ extraData = { { tokens, targetIndex, codeSurface, codeWordBreak, onLongPressLine } }
403+ estimatedItemSize = { codeSurface . rowHeight }
404+ getFixedItemSize = { codeWordBreak ? undefined : ( ) => codeSurface . rowHeight }
405+ drawDistance = { codeSurface . rowHeight * 40 }
406+ recycleItems
407407 contentContainerStyle = { {
408408 minWidth : codeWordBreak ? undefined : "100%" ,
409409 paddingBottom : codeSurface . rowHeight ,
410410 paddingTop : 8 ,
411411 } }
412412 renderItem = { renderLine }
413- onScrollToIndexFailed = { lineScroll . onScrollToIndexFailed }
414- onScrollBeginDrag = { lineScroll . cancel }
415413 />
416414 ) ;
417415
0 commit comments