|
| 1 | +import React, { useCallback, useEffect, useState } from 'react'; |
| 2 | +import { SectionList, StyleSheet, View } from 'react-native'; |
| 3 | +import sectionListGetItemLayout from 'react-native-section-list-get-item-layout'; |
| 4 | +import ActivityIndicator from '../ActivityIndicator'; |
| 5 | +import Spinner from '../Spinner'; |
| 6 | +import { ButtonPressAnimation } from '../animations'; |
| 7 | +import { CoinRowHeight } from '../coin-row/CoinRow'; |
| 8 | +import Text from '../text/Text'; |
| 9 | +import ActivityListEmptyState from './ActivityListEmptyState'; |
| 10 | +import ActivityListHeader from './ActivityListHeader'; |
| 11 | +import styled from '@/styled-thing'; |
| 12 | +import { ThemeContextProps, useTheme } from '@/theme'; |
| 13 | +import { useSectionListScrollToTopContext } from '@/navigation/SectionListScrollToTopContext'; |
| 14 | +import { safeAreaInsetValues } from '@/utils'; |
| 15 | +import { useAccountSettings, useAccountTransactions } from '@/hooks'; |
| 16 | +import { usePendingTransactionsStore } from '@/state/pendingTransactions'; |
| 17 | +import { TransactionSections, TransactionItemForSectionList } from '@/helpers/buildTransactionsSectionsSelector'; |
| 18 | + |
| 19 | +const sx = StyleSheet.create({ |
| 20 | + sectionHeader: { |
| 21 | + paddingVertical: 18, |
| 22 | + }, |
| 23 | +}); |
| 24 | + |
| 25 | +const ActivityListHeaderHeight = 42; |
| 26 | +const TRANSACTION_COIN_ROW_VERTICAL_PADDING = 7; |
| 27 | + |
| 28 | +const getItemLayout = sectionListGetItemLayout({ |
| 29 | + getItemHeight: () => CoinRowHeight + TRANSACTION_COIN_ROW_VERTICAL_PADDING * 2, |
| 30 | + getSectionHeaderHeight: () => ActivityListHeaderHeight, |
| 31 | +}); |
| 32 | + |
| 33 | +const keyExtractor = (data: TransactionSections['data'][number]) => { |
| 34 | + if ('hash' in data) { |
| 35 | + return (data.hash || data.timestamp ? data.timestamp?.toString() : performance.now().toString()) ?? performance.now().toString(); |
| 36 | + } |
| 37 | + return ( |
| 38 | + (data.displayDetails?.timestampInMs ? data.displayDetails.timestampInMs.toString() : performance.now().toString()) ?? |
| 39 | + performance.now().toString() |
| 40 | + ); |
| 41 | +}; |
| 42 | + |
| 43 | +const renderSectionHeader = ({ section, colors }: { section: TransactionSections; colors: ThemeContextProps['colors'] }) => { |
| 44 | + return ( |
| 45 | + <View style={[sx.sectionHeader, { backgroundColor: colors.white }]}> |
| 46 | + <ActivityListHeader {...section} /> |
| 47 | + </View> |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +const LoadingSpinner = android ? Spinner : ActivityIndicator; |
| 52 | + |
| 53 | +const FooterWrapper = styled(ButtonPressAnimation)({ |
| 54 | + alignItems: 'center', |
| 55 | + height: 40, |
| 56 | + justifyContent: 'center', |
| 57 | + paddingBottom: 10, |
| 58 | + width: '100%', |
| 59 | +}); |
| 60 | + |
| 61 | +function ListFooterComponent({ label, onPress }: { label: string; onPress: () => void }) { |
| 62 | + const [isLoading, setIsLoading] = useState(false); |
| 63 | + const { colors } = useTheme(); |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + if (isLoading) { |
| 67 | + onPress(); |
| 68 | + setIsLoading(false); |
| 69 | + } |
| 70 | + }, [isLoading, setIsLoading, onPress]); |
| 71 | + const onPressWrapper = () => { |
| 72 | + setIsLoading(true); |
| 73 | + }; |
| 74 | + return ( |
| 75 | + <FooterWrapper onPress={onPressWrapper}> |
| 76 | + {isLoading ? ( |
| 77 | + <LoadingSpinner /> |
| 78 | + ) : ( |
| 79 | + <Text align="center" color={colors.alpha(colors.blueGreyDark, 0.3)} lineHeight="loose" size="smedium" weight="bold"> |
| 80 | + {label} |
| 81 | + </Text> |
| 82 | + )} |
| 83 | + </FooterWrapper> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +const ActivityList = () => { |
| 88 | + const { accountAddress, nativeCurrency } = useAccountSettings(); |
| 89 | + |
| 90 | + const { setScrollToTopRef } = useSectionListScrollToTopContext(); |
| 91 | + const { sections, nextPage, transactionsCount, remainingItemsLabel } = useAccountTransactions(); |
| 92 | + const pendingTransactions = usePendingTransactionsStore(state => state.pendingTransactions[accountAddress] || []); |
| 93 | + |
| 94 | + const { colors } = useTheme(); |
| 95 | + const renderSectionHeaderWithTheme = useCallback( |
| 96 | + ({ section }: { section: TransactionSections }) => renderSectionHeader({ colors, section }), |
| 97 | + [colors] |
| 98 | + ); |
| 99 | + |
| 100 | + const handleScrollToTopRef = (ref: SectionList<TransactionItemForSectionList, TransactionSections> | null) => { |
| 101 | + if (!ref) return; |
| 102 | + // @ts-expect-error - no idea why this is not working |
| 103 | + setScrollToTopRef(ref); |
| 104 | + }; |
| 105 | + |
| 106 | + return ( |
| 107 | + <SectionList<TransactionItemForSectionList, TransactionSections> |
| 108 | + ListFooterComponent={() => remainingItemsLabel && <ListFooterComponent label={remainingItemsLabel} onPress={nextPage} />} |
| 109 | + ref={handleScrollToTopRef} |
| 110 | + alwaysBounceVertical={false} |
| 111 | + contentContainerStyle={{ paddingBottom: !transactionsCount ? 0 : 90 }} |
| 112 | + extraData={{ |
| 113 | + hasPendingTransaction: pendingTransactions.length > 0, |
| 114 | + nativeCurrency, |
| 115 | + pendingTransactionsCount: pendingTransactions.length, |
| 116 | + }} |
| 117 | + testID={'wallet-activity-list'} |
| 118 | + ListEmptyComponent={<ActivityListEmptyState />} |
| 119 | + // @ts-expect-error - mismatch between react-native-section-list-get-item-layout and SectionList |
| 120 | + getItemLayout={getItemLayout} |
| 121 | + initialNumToRender={12} |
| 122 | + keyExtractor={keyExtractor} |
| 123 | + removeClippedSubviews |
| 124 | + renderSectionHeader={renderSectionHeaderWithTheme} |
| 125 | + scrollIndicatorInsets={{ |
| 126 | + bottom: safeAreaInsetValues.bottom + 14, |
| 127 | + }} |
| 128 | + sections={sections} |
| 129 | + /> |
| 130 | + ); |
| 131 | +}; |
| 132 | + |
| 133 | +export default ActivityList; |
0 commit comments