refactor: remove performance hack for pager list and update imports to use FlashList directly

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-07-24 20:20:31 +08:00
parent 80d5aad909
commit e32430f4d9
7 changed files with 35 additions and 78 deletions
@@ -1,8 +1,8 @@
import type { FeedViewType } from "@follow/constants"
import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks"
import type { ListRenderItemInfo } from "@shopify/flash-list"
import type { FlashList, ListRenderItemInfo } from "@shopify/flash-list"
import type { ElementRef } from "react"
import { useCallback, useImperativeHandle, useMemo } from "react"
import { useCallback, useImperativeHandle, useMemo, useRef } from "react"
import { View } from "react-native"
import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general"
@@ -13,7 +13,7 @@ import { useHeaderHeight } from "@/src/modules/screen/hooks/useHeaderHeight"
import { useEntries } from "../screen/atoms"
import { TimelineSelectorList } from "../screen/TimelineSelectorList"
import { EntryListFooter } from "./EntryListFooter"
import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks"
import { useOnViewableItemsChanged } from "./hooks"
import { ItemSeparator } from "./ItemSeparator"
import { EntryNormalItem } from "./templates/EntryNormalItem"
import type { EntryExtraData } from "./types"
@@ -43,11 +43,10 @@ export const EntryListContentArticle = ({
[hasNextPage, fetchedTime],
)
const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack()
const ref = useRef<FlashList<any>>(null)
const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({
disabled: active === false || isFetching,
onScroll: hackOnScroll,
})
useImperativeHandle(forwardRef, () => ref.current!)
@@ -90,7 +89,6 @@ export const EntryListContentArticle = ({
onViewableItemsChanged={onViewableItemsChanged}
ItemSeparatorComponent={ItemSeparator}
ListFooterComponent={ListFooterComponent}
style={hackStyle}
/>
)
}
@@ -1,8 +1,8 @@
import { useTypeScriptHappyCallback } from "@follow/hooks"
import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks"
import type { MasonryFlashListProps } from "@shopify/flash-list"
import type { FlashList, MasonryFlashListProps } from "@shopify/flash-list"
import type { ElementRef } from "react"
import { useImperativeHandle } from "react"
import { useImperativeHandle, useRef } from "react"
import { StyleSheet, View } from "react-native"
import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general"
@@ -14,7 +14,7 @@ import { useHeaderHeight } from "@/src/modules/screen/hooks/useHeaderHeight"
import { TimelineSelectorMasonryList } from "../screen/TimelineSelectorList"
import { GridEntryListFooter } from "./EntryListFooter"
import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks"
import { useOnViewableItemsChanged } from "./hooks"
// import type { MasonryItem } from "./templates/EntryGridItem"
import { EntryPictureItem } from "./templates/EntryPictureItem"
@@ -27,12 +27,12 @@ export const EntryListContentPicture = ({
MasonryFlashListProps<string>,
"data" | "renderItem"
> & { ref?: React.Ref<ElementRef<typeof TimelineSelectorMasonryList> | null> }) => {
const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack()
const ref = useRef<FlashList<any>>(null)
useImperativeHandle(forwardRef, () => ref.current!)
const { fetchNextPage, refetch, isRefetching, hasNextPage, isFetching, isReady } = useEntries()
const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({
disabled: active === false || isFetching,
onScroll: hackOnScroll,
})
const translation = useGeneralSettingKey("translation")
const actionLanguage = useActionLanguage()
@@ -84,7 +84,6 @@ export const EntryListContentPicture = ({
onScroll={onScroll}
onEndReached={fetchNextPage}
numColumns={2}
style={hackStyle}
estimatedItemSize={100}
contentContainerStyle={styles.contentContainer}
ListFooterComponent={
@@ -1,7 +1,7 @@
import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks"
import type { ListRenderItemInfo } from "@shopify/flash-list"
import type { FlashList, ListRenderItemInfo } from "@shopify/flash-list"
import type { ElementRef } from "react"
import { useCallback, useImperativeHandle, useMemo } from "react"
import { useCallback, useImperativeHandle, useMemo, useRef } from "react"
import { View } from "react-native"
import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general"
@@ -12,7 +12,7 @@ import { useHeaderHeight } from "@/src/modules/screen/hooks/useHeaderHeight"
import { useEntries } from "../screen/atoms"
import { TimelineSelectorList } from "../screen/TimelineSelectorList"
import { EntryListFooter } from "./EntryListFooter"
import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks"
import { useOnViewableItemsChanged } from "./hooks"
import { ItemSeparatorFullWidth } from "./ItemSeparator"
import { EntrySocialItem } from "./templates/EntrySocialItem"
import type { EntryExtraData } from "./types"
@@ -27,7 +27,7 @@ export const EntryListContentSocial = ({
const { fetchNextPage, isFetching, refetch, isRefetching, hasNextPage, isReady } = useEntries()
const extraData: EntryExtraData = useMemo(() => ({ entryIds }), [entryIds])
const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack()
const ref = useRef<FlashList<any>>(null)
useImperativeHandle(forwardRef, () => ref.current!)
// eslint-disable-next-line @eslint-react/hooks-extra/no-unnecessary-use-callback
const renderItem = useCallback(
@@ -44,7 +44,6 @@ export const EntryListContentSocial = ({
const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({
disabled: active === false || isFetching,
onScroll: hackOnScroll,
})
const translation = useGeneralSettingKey("translation")
@@ -87,7 +86,6 @@ export const EntryListContentSocial = ({
onScroll={onScroll}
ItemSeparatorComponent={ItemSeparatorFullWidth}
ListFooterComponent={ListFooterComponent}
style={hackStyle}
/>
)
}
@@ -1,8 +1,8 @@
import { useTypeScriptHappyCallback } from "@follow/hooks"
import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks"
import type { MasonryFlashListProps } from "@shopify/flash-list"
import type { FlashList, MasonryFlashListProps } from "@shopify/flash-list"
import type { ElementRef } from "react"
import { useImperativeHandle, useMemo } from "react"
import { useImperativeHandle, useMemo, useRef } from "react"
import { StyleSheet, View } from "react-native"
import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general"
@@ -13,7 +13,7 @@ import { useHeaderHeight } from "@/src/modules/screen/hooks/useHeaderHeight"
import { TimelineSelectorMasonryList } from "../screen/TimelineSelectorList"
import { GridEntryListFooter } from "./EntryListFooter"
import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks"
import { useOnViewableItemsChanged } from "./hooks"
import { EntryVideoItem } from "./templates/EntryVideoItem"
export const EntryListContentVideo = ({
@@ -25,12 +25,11 @@ export const EntryListContentVideo = ({
MasonryFlashListProps<string>,
"data" | "renderItem"
> & { ref?: React.Ref<ElementRef<typeof TimelineSelectorMasonryList> | null> }) => {
const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack()
const ref = useRef<FlashList<any>>(null)
useImperativeHandle(forwardRef, () => ref.current!)
const { fetchNextPage, refetch, isRefetching, isFetching, hasNextPage, isReady } = useEntries()
const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({
disabled: active === false || isFetching,
onScroll: hackOnScroll,
})
const translation = useGeneralSettingKey("translation")
@@ -100,7 +99,6 @@ export const EntryListContentVideo = ({
ListFooterComponent={ListFooterComponent}
{...rest}
onRefresh={refetch}
style={hackStyle}
contentContainerStyle={styles.contentContainer}
/>
)
+2 -41
View File
@@ -1,18 +1,12 @@
import { debouncedFetchEntryContentByStream } from "@follow/store/entry/store"
import { unreadSyncService } from "@follow/store/unread/store"
import type { FlashList } from "@shopify/flash-list"
import type ViewToken from "@shopify/flash-list/dist/viewability/ViewToken"
import { fetch as expoFetch } from "expo/fetch"
import type { RefObject } from "react"
import { use, useCallback, useEffect, useInsertionEffect, useMemo, useRef, useState } from "react"
import type { NativeScrollEvent, NativeSyntheticEvent, ViewStyle } from "react-native"
import { useEventCallback } from "usehooks-ts"
import { useCallback, useEffect, useInsertionEffect, useMemo, useRef, useState } from "react"
import type { NativeScrollEvent, NativeSyntheticEvent } from "react-native"
import { useGeneralSettingKey } from "@/src/atoms/settings/general"
import { getCookie } from "@/src/lib/auth"
import { isAndroid } from "@/src/lib/platform"
import { PagerListVisibleContext, PagerListWillVisibleContext } from "../screen/PagerListContext"
const defaultIdExtractor = (item: ViewToken) => item.key
export function useOnViewableItemsChanged({
@@ -127,36 +121,3 @@ function useNonReactiveCallback<T extends (...args: any[]) => any>(fn: T): T {
[ref],
) as unknown as T
}
export const usePagerListPerformanceHack = (provideRef?: RefObject<FlashList<any> | null>) => {
const lastY = useRef(0)
const onScroll = useEventCallback((e: NativeSyntheticEvent<NativeScrollEvent>) => {
if (!visible) return
lastY.current = e.nativeEvent.contentOffset.y
})
const visible = use(PagerListVisibleContext)
const willVisible = use(PagerListWillVisibleContext)
const nextVisible = visible || willVisible
const ref = useRef<FlashList<any>>(null)
const usingRef = provideRef ?? ref
const [style, setStyle] = useState<ViewStyle>({})
useEffect(() => {
if (!isAndroid) {
// Always show the pager list on Android to avoid blank screen when switching tabs
setStyle({ display: nextVisible ? "flex" : "none" })
}
if (nextVisible && lastY.current > 0) {
requestAnimationFrame(() => {
usingRef.current?.scrollToOffset({ offset: lastY.current, animated: false })
})
}
}, [nextVisible, usingRef])
return { onScroll, ref, style }
}
@@ -4,6 +4,7 @@ import { useViewWithSubscription } from "@follow/store/subscription/hooks"
import { EventBus } from "@follow/utils/event-bus"
import * as Haptics from "expo-haptics"
import { useCallback, useId, useLayoutEffect, useMemo, useRef, useState } from "react"
import { Freeze } from "react-freeze"
import type { StyleProp, ViewStyle } from "react-native"
import { clamp, withTiming } from "react-native-reanimated"
@@ -76,15 +77,20 @@ export function PagerList({
[activeViews, rid],
)}
renderPage={useTypeScriptHappyCallback(
(index) => (
<PagerListVisibleContext value={index === activeViewIndex} key={activeViews[index]!}>
<PagerListWillVisibleContext
value={(index === activeViewIndex + 1 || index === activeViewIndex - 1) && dragging}
>
{renderItem(activeViews[index]!, index === activeViewIndex)}
</PagerListWillVisibleContext>
</PagerListVisibleContext>
),
(index) => {
const isActive = index === activeViewIndex
const willVisible =
(index === activeViewIndex + 1 || index === activeViewIndex - 1) && dragging
const freeze = !(isActive || willVisible)
return (
<PagerListVisibleContext value={isActive} key={activeViews[index]!}>
<PagerListWillVisibleContext value={willVisible}>
<Freeze freeze={freeze}>{renderItem(activeViews[index]!, isActive)}</Freeze>
</PagerListWillVisibleContext>
</PagerListVisibleContext>
)
},
[activeViews, activeViewIndex, dragging, renderItem],
)}
pageTotal={activeViews.length}
@@ -35,7 +35,6 @@ import { selectFeed } from "@/src/modules/screen/atoms"
import { TimelineSelectorList } from "@/src/modules/screen/TimelineSelectorList"
import { FeedScreen } from "@/src/screens/(stack)/feeds/[feedId]/FeedScreen"
import { usePagerListPerformanceHack } from "../entry-list/hooks"
import { useFeedListSortMethod, useFeedListSortOrder } from "./atoms"
import { CategoryGrouped } from "./CategoryGrouped"
import { InboxItem } from "./items/InboxItem"
@@ -129,7 +128,7 @@ const SubscriptionListImpl = ({
return subscriptionSyncService.fetch(view)
})
const scrollViewRef = useRegisterNavigationScrollView<FlashList<any> | null>(active)
const { onScroll, style } = usePagerListPerformanceHack(scrollViewRef)
return (
<TimelineSelectorList
contentContainerClassName="pb-6"
@@ -146,8 +145,6 @@ const SubscriptionListImpl = ({
renderItem={ItemRender}
keyExtractor={keyExtractor}
extraData={extraData}
onScroll={onScroll}
style={style}
/>
)
}