From 80bf614acb6011ca04a524780ca490438774f141 Mon Sep 17 00:00:00 2001 From: Javier Bueno Date: Thu, 5 Dec 2024 13:19:03 +0100 Subject: [PATCH] feature(wallet-mobile): portfolio token list performance improvement --- .../PortfolioWalletTokenList.tsx | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/apps/wallet-mobile/src/features/Portfolio/useCases/PortfolioTokensList/PortfolioWalletTokenList/PortfolioWalletTokenList.tsx b/apps/wallet-mobile/src/features/Portfolio/useCases/PortfolioTokensList/PortfolioWalletTokenList/PortfolioWalletTokenList.tsx index a9e115c066..aeaf17c219 100644 --- a/apps/wallet-mobile/src/features/Portfolio/useCases/PortfolioTokensList/PortfolioWalletTokenList/PortfolioWalletTokenList.tsx +++ b/apps/wallet-mobile/src/features/Portfolio/useCases/PortfolioTokensList/PortfolioWalletTokenList/PortfolioWalletTokenList.tsx @@ -1,9 +1,10 @@ import {useFocusEffect} from '@react-navigation/native' +import {FlashList} from '@shopify/flash-list' import {infoExtractName, isPrimaryToken} from '@yoroi/portfolio' import {useTheme} from '@yoroi/theme' import {Chain, Portfolio} from '@yoroi/types' import * as React from 'react' -import {FlatList, StyleSheet, Text, View} from 'react-native' +import {StyleSheet, Text, View} from 'react-native' import {Spacer} from '../../../../../components/Spacer/Spacer' import {useMetrics} from '../../../../../kernel/metrics/metricsManager' @@ -90,6 +91,22 @@ export const PortfolioWalletTokenList = () => { const isPreprod = network === Chain.Network.Preprod + const [loadedTokens, setLoadedTokens] = React.useState(getListTokens.slice(0, batchSize)) + const [currentIndex, setCurrentIndex] = React.useState(batchSize) + + React.useEffect(() => { + setLoadedTokens(getListTokens.slice(0, currentIndex + batchSize)) + setCurrentIndex(currentIndex + batchSize) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [tokensList]) // must be tokensList + + const handleOnEndReached = React.useCallback(() => { + if (currentIndex >= getListTokens.length) return + const nextBatch = getListTokens.slice(currentIndex, currentIndex + batchSize) + setLoadedTokens([...loadedTokens, ...nextBatch]) + setCurrentIndex(currentIndex + batchSize) + }, [currentIndex, getListTokens, loadedTokens]) + const renderFooterList = () => { if (isSearching) return null if (isLoading) { @@ -126,8 +143,8 @@ export const PortfolioWalletTokenList = () => { return ( - { renderItem={({item}) => } contentContainerStyle={styles.container} ListEmptyComponent={() => } + onEndReached={handleOnEndReached} + onEndReachedThreshold={0.5} + estimatedItemSize={72} /> ) @@ -185,6 +205,8 @@ const SkeletonItem = () => { ) } +const batchSize = 50 + const useStyles = () => { const {atoms, color} = useTheme() const styles = StyleSheet.create({