import React, {memo, startTransition} from 'react' import {FlatListProps} from 'react-native' import {FlatList_INTERNAL} from './Views' import {useScrollHandlers} from '#/lib/ScrollContext' import {runOnJS, useSharedValue} from 'react-native-reanimated' import {useAnimatedScrollHandler} from '#/lib/hooks/useAnimatedScrollHandler_FIXED' export type ListMethods = FlatList_INTERNAL export type ListProps = Omit< FlatListProps, 'onScroll' // Use ScrollContext instead. > & { onScrolledDownChange?: (isScrolledDown: boolean) => void } export type ListRef = React.MutableRefObject const SCROLLED_DOWN_LIMIT = 200 function ListImpl( {onScrolledDownChange, ...props}: ListProps, ref: React.Ref, ) { const isScrolledDown = useSharedValue(false) const contextScrollHandlers = useScrollHandlers() function handleScrolledDownChange(didScrollDown: boolean) { startTransition(() => { onScrolledDownChange?.(didScrollDown) }) } const scrollHandler = useAnimatedScrollHandler({ onBeginDrag(e, ctx) { contextScrollHandlers.onBeginDrag?.(e, ctx) }, onEndDrag(e, ctx) { contextScrollHandlers.onEndDrag?.(e, ctx) }, onScroll(e, ctx) { contextScrollHandlers.onScroll?.(e, ctx) const didScrollDown = e.contentOffset.y > SCROLLED_DOWN_LIMIT if (isScrolledDown.value !== didScrollDown) { isScrolledDown.value = didScrollDown if (onScrolledDownChange != null) { runOnJS(handleScrolledDownChange)(didScrollDown) } } }, }) return ( ) } export const List = memo(React.forwardRef(ListImpl)) as ( props: ListProps & {ref?: React.Ref}, ) => React.ReactElement