Skip to content
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

Merged
merged 19 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this types declaration file was missing when converting ActivityIndicator to typescript

Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@
"@types/qs": "6.9.7",
"@types/react": "18.2.65",
"@types/react-native-extra-dimensions-android": "1.2.3",
"@types/react-native-indicators": "0.16.6",
"@types/react-test-renderer": "18.3.0",
"@types/styled-components": "5.1.7",
"@types/url-parse": "1.4.3",
Expand Down
2 changes: 1 addition & 1 deletion src/__swaps__/types/refraction.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved all txn types into @/entities

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ZerionAsset } from '@/__swaps__/types/assets';
import { ChainId, ChainName } from '@/chains/types';
import { PaginatedTransactionsApiResponse } from '@/resources/transactions/types';
import { PaginatedTransactionsApiResponse } from '@/entities';

/**
* Metadata for a message from the Zerion API.
Expand Down
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typescript conversion

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import { Centered } from './layout';
import styled from '@/styled-thing';
import { position } from '@/styles';

const Container = styled(Centered)(({ size }) => position.sizeAsObject(Number(size)));
const Container = styled(Centered)(({ size }: { size: number }) => position.sizeAsObject(Number(size)));

export default function ActivityIndicator({ color, isInteraction = false, size = 25, ...props }) {
type ActivityIndicatorProps = {
color?: string;
isInteraction?: boolean;
size?: number;
};

export default function ActivityIndicator({ color, isInteraction = false, size = 25, ...props }: ActivityIndicatorProps) {
const { colors } = useTheme();
return (
<Container size={size} {...props}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Divider.tsx
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typescript conversion

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { magicMemo } from '../utils';
import styled from '@/styled-thing';
import { borders, position } from '@/styles';
import { StyleProp, View, ViewProps, ViewStyle } from 'react-native';
import { View } from 'react-native';
import { ThemeContextProps, useTheme } from '@/theme';

export const DividerSize = 2;
Expand Down
145 changes: 0 additions & 145 deletions src/components/activity-list/ActivityList.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> .tsx

This file was deleted.

133 changes: 133 additions & 0 deletions src/components/activity-list/ActivityList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
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({
Copy link
Contributor Author

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 bumping react-native will fix it? But not worried about it for now.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 performance.now calls, but added them in case we do.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this work?
onPress sets isLoading to true, rerenders run props.onPress, then right after sets isLoading to false wouldn't that just quickly flash the loading component? unless onPress is doing some crazy expensive blocking work

is it not the same as

 const onPressWrapper = () => {
    setIsLoading(true);
    onPress();
    setIsLoading(false);
  };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I didn't originally write this

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
const { sections, nextPage, transactionsCount, remainingItemsLabel } = useAccountTransactions();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the sections and transaction stuff in here instead of in ProfileScreen since we don't need to re-render the whole page just the list.

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;
// @ts-expect-error - no idea why this is not working
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

welcoming ideas on how to get this to work if anyone has one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  const { setScrollToTopRef } = useSectionListScrollToTopContext<TransactionItemForSectionList, TransactionSections>();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THANK YOU

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ const Container = styled(Column)({
width: 200,
});

const ActivityListEmptyState = ({ children, emoji, label }) => {
type ActivityListEmptyStateProps = {
children?: React.ReactNode;
emoji: string;
label: string;
};

const ActivityListEmptyState = ({ children, emoji, label }: ActivityListEmptyStateProps) => {
const { top: topInset } = useSafeAreaInsets();
const { colors, isDarkMode } = useTheme();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { AssetListItemSkeleton } from '../asset-list';
import { Column } from '../layout';
import { times } from '@/helpers/utilities';

const LoadingState = ({ children }) => (
type LoadingStateProps = {
children: React.ReactNode;
};

const LoadingState = ({ children }: LoadingStateProps) => (
<Column flex={1}>
{children}
<Column flex={1}>
Expand Down
4 changes: 1 addition & 3 deletions src/components/cards/GasCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-expect-error
import AnimateNumber from '@bankify/react-native-animate-number';
import { useIsFocused } from '@react-navigation/native';
import * as i18n from '@/languages';
Expand Down Expand Up @@ -60,8 +59,7 @@ export const GasCard = () => {
const isCurrentGweiLoaded = currentGwei && Number(currentGwei) > 0;

const renderGweiText = useCallback(
// @ts-expect-error passed to an untyped JS component
animatedNumber => {
(animatedNumber: number) => {
const priceText =
animatedNumber === 0
? isCurrentGweiLoaded
Expand Down
Loading
Loading