-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CHORE]: Remove transaction inconsistencies and improve type safety #6137
Changes from all commits
2a4c08f
e574971
8f93649
e633236
fac310e
f6f85cb
1b99983
ede6f06
01ae202
0eb75ec
63a15d1
c0dc1e0
f10e005
09fe478
564b0a5
211015c
9154327
be5256e
2975158
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import React, { useCallback, useEffect, useState } from 'react'; | ||
import { SectionList, StyleSheet, View } from 'react-native'; | ||
import sectionListGetItemLayout from 'react-native-section-list-get-item-layout'; | ||
import ActivityIndicator from '../ActivityIndicator'; | ||
import Spinner from '../Spinner'; | ||
import { ButtonPressAnimation } from '../animations'; | ||
import { CoinRowHeight } from '../coin-row/CoinRow'; | ||
import Text from '../text/Text'; | ||
import ActivityListEmptyState from './ActivityListEmptyState'; | ||
import ActivityListHeader from './ActivityListHeader'; | ||
import styled from '@/styled-thing'; | ||
import { ThemeContextProps, useTheme } from '@/theme'; | ||
import { useSectionListScrollToTopContext } from '@/navigation/SectionListScrollToTopContext'; | ||
import { safeAreaInsetValues } from '@/utils'; | ||
import { useAccountSettings, useAccountTransactions } from '@/hooks'; | ||
import { usePendingTransactionsStore } from '@/state/pendingTransactions'; | ||
import { TransactionSections, TransactionItemForSectionList } from '@/helpers/buildTransactionsSectionsSelector'; | ||
|
||
const sx = StyleSheet.create({ | ||
sectionHeader: { | ||
paddingVertical: 18, | ||
}, | ||
}); | ||
|
||
const ActivityListHeaderHeight = 42; | ||
const TRANSACTION_COIN_ROW_VERTICAL_PADDING = 7; | ||
|
||
const getItemLayout = sectionListGetItemLayout({ | ||
getItemHeight: () => CoinRowHeight + TRANSACTION_COIN_ROW_VERTICAL_PADDING * 2, | ||
getSectionHeaderHeight: () => ActivityListHeaderHeight, | ||
}); | ||
|
||
const keyExtractor = (data: TransactionSections['data'][number]) => { | ||
if ('hash' in data) { | ||
return (data.hash || data.timestamp ? data.timestamp?.toString() : performance.now().toString()) ?? performance.now().toString(); | ||
} | ||
return ( | ||
(data.displayDetails?.timestampInMs ? data.displayDetails.timestampInMs.toString() : performance.now().toString()) ?? | ||
performance.now().toString() | ||
); | ||
}; | ||
Comment on lines
+33
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need the key extractor to always fallback to a string, so theoretically we should never hit |
||
|
||
const renderSectionHeader = ({ section, colors }: { section: TransactionSections; colors: ThemeContextProps['colors'] }) => { | ||
return ( | ||
<View style={[sx.sectionHeader, { backgroundColor: colors.white }]}> | ||
<ActivityListHeader {...section} /> | ||
</View> | ||
); | ||
}; | ||
|
||
const LoadingSpinner = android ? Spinner : ActivityIndicator; | ||
|
||
const FooterWrapper = styled(ButtonPressAnimation)({ | ||
alignItems: 'center', | ||
height: 40, | ||
justifyContent: 'center', | ||
paddingBottom: 10, | ||
width: '100%', | ||
}); | ||
|
||
function ListFooterComponent({ label, onPress }: { label: string; onPress: () => void }) { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const { colors } = useTheme(); | ||
|
||
useEffect(() => { | ||
if (isLoading) { | ||
onPress(); | ||
setIsLoading(false); | ||
} | ||
}, [isLoading, setIsLoading, onPress]); | ||
const onPressWrapper = () => { | ||
setIsLoading(true); | ||
}; | ||
Comment on lines
+65
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how does this work? is it not the same as const onPressWrapper = () => {
setIsLoading(true);
onPress();
setIsLoading(false);
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I didn't originally write this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. afaik this is just for fetching the next batch of transactions |
||
return ( | ||
<FooterWrapper onPress={onPressWrapper}> | ||
{isLoading ? ( | ||
<LoadingSpinner /> | ||
) : ( | ||
<Text align="center" color={colors.alpha(colors.blueGreyDark, 0.3)} lineHeight="loose" size="smedium" weight="bold"> | ||
{label} | ||
</Text> | ||
)} | ||
</FooterWrapper> | ||
); | ||
} | ||
|
||
const ActivityList = () => { | ||
const { accountAddress, nativeCurrency } = useAccountSettings(); | ||
|
||
const { setScrollToTopRef } = useSectionListScrollToTopContext<TransactionItemForSectionList, TransactionSections>(); | ||
const { sections, nextPage, transactionsCount, remainingItemsLabel } = useAccountTransactions(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved the sections and transaction stuff in here instead of in |
||
const pendingTransactions = usePendingTransactionsStore(state => state.pendingTransactions[accountAddress] || []); | ||
|
||
const { colors } = useTheme(); | ||
const renderSectionHeaderWithTheme = useCallback( | ||
({ section }: { section: TransactionSections }) => renderSectionHeader({ colors, section }), | ||
[colors] | ||
); | ||
|
||
const handleScrollToTopRef = (ref: SectionList<TransactionItemForSectionList, TransactionSections> | null) => { | ||
if (!ref) return; | ||
setScrollToTopRef(ref); | ||
}; | ||
|
||
return ( | ||
<SectionList<TransactionItemForSectionList, TransactionSections> | ||
ListFooterComponent={() => remainingItemsLabel && <ListFooterComponent label={remainingItemsLabel} onPress={nextPage} />} | ||
ref={handleScrollToTopRef} | ||
alwaysBounceVertical={false} | ||
contentContainerStyle={{ paddingBottom: !transactionsCount ? 0 : 90 }} | ||
extraData={{ | ||
hasPendingTransaction: pendingTransactions.length > 0, | ||
nativeCurrency, | ||
pendingTransactionsCount: pendingTransactions.length, | ||
}} | ||
testID={'wallet-activity-list'} | ||
ListEmptyComponent={<ActivityListEmptyState />} | ||
// @ts-expect-error - mismatch between react-native-section-list-get-item-layout and SectionList | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
getItemLayout={getItemLayout} | ||
initialNumToRender={12} | ||
keyExtractor={keyExtractor} | ||
removeClippedSubviews | ||
renderSectionHeader={renderSectionHeaderWithTheme} | ||
scrollIndicatorInsets={{ | ||
bottom: safeAreaInsetValues.bottom + 14, | ||
}} | ||
sections={sections} | ||
/> | ||
); | ||
}; | ||
|
||
export default ActivityList; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was causing issues with the SectionList below, so I added a
@ts-expect-error
declaration for now. Maybe bumpingreact-native
will fix it? But not worried about it for now.