Skip to content

Commit

Permalink
feature(wallet-mobile): portfolio token list performance improvement (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
banklesss authored Dec 5, 2024
1 parent 131b732 commit 918273d
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -126,8 +143,8 @@ export const PortfolioWalletTokenList = () => {

return (
<View style={styles.root}>
<FlatList
data={getListTokens}
<FlashList
data={loadedTokens}
ListHeaderComponent={
<HeadingList
isShowBalanceCard={!isSearching}
Expand All @@ -141,6 +158,9 @@ export const PortfolioWalletTokenList = () => {
renderItem={({item}) => <TokenBalanceItem amount={item} />}
contentContainerStyle={styles.container}
ListEmptyComponent={() => <TokenEmptyList />}
onEndReached={handleOnEndReached}
onEndReachedThreshold={0.5}
estimatedItemSize={72}
/>
</View>
)
Expand Down Expand Up @@ -185,6 +205,8 @@ const SkeletonItem = () => {
)
}

const batchSize = 50

const useStyles = () => {
const {atoms, color} = useTheme()
const styles = StyleSheet.create({
Expand Down

0 comments on commit 918273d

Please sign in to comment.