Skip to content

Commit

Permalink
chore: Convert monolithic component into smaller pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
gambinish committed Jan 18, 2025
1 parent ed95ba4 commit 663942d
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 241 deletions.
134 changes: 6 additions & 128 deletions app/components/UI/NftGrid/NftGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import React, { useCallback, useRef, useState } from 'react';
import {
View,
TouchableOpacity,
Alert,
ScrollView,
ActivityIndicator,
Image,
FlatList,
RefreshControl,
} from 'react-native';
import { useSelector } from 'react-redux';
import CollectibleMedia from '../CollectibleMedia';
import ActionSheet from '@metamask/react-native-actionsheet';
import { strings } from '../../../../locales/i18n';
import Engine from '../../../core/Engine';
Expand All @@ -20,36 +16,24 @@ import {
isNftFetchingProgressSelector,
} from '../../../reducers/collectibles';
import { useTheme } from '../../../util/theme';
import Text, {
TextColor,
TextVariant,
} from '../../../component-library/components/Texts/Text';
import {
MetaMetricsEvents,
useMetrics,
} from '../../../components/hooks/useMetrics';
import { getDecimalChainId } from '../../../util/networks';
import { Nft } from '@metamask/assets-controllers';
import { debounce } from 'lodash';
import styleSheet from './NftGrid.styles';
import { useStyles } from '../../hooks/useStyles';
import { StackNavigationProp } from '@react-navigation/stack';
import { WalletViewSelectorsIDs } from '../../../../e2e/selectors/wallet/WalletView.selectors';
import CollectibleDetectionModal from '../CollectibleDetectionModal';
import { selectUseNftDetection } from '../../../selectors/preferencesController';
import ButtonLink from '../../../component-library/components/Buttons/Button/variants/ButtonLink';
import AppConstants from '../../../core/AppConstants';
import {
RefreshTestId,
SpinnerTestId,
} from '../CollectibleContracts/constants';
import NftGridItem from './NftGridItem';

const noNftPlaceholderSrc = require('../../../images/no-nfts-placeholder.png');

const debouncedNavigation = debounce((navigation, collectible) => {
navigation.navigate('NftDetails', { collectible });
}, 200);
import NftGridEmpty from './NftGridEmpty';
import NftGridFooter from './NftGridFooter';

interface ActionSheetType {
show: () => void;
Expand Down Expand Up @@ -98,11 +82,6 @@ function NftGrid({ navigation, chainId, selectedAddress }: NftGridProps) {
});
}, [setRefreshing]);

const onLongPressCollectible = useCallback((collectible) => {
actionSheetRef?.current?.show();
longPressedCollectible.current = collectible;
}, []);

const removeNft = () => {
const { NftController } = Engine.context;

Expand Down Expand Up @@ -160,107 +139,6 @@ function NftGrid({ navigation, chainId, selectedAddress }: NftGridProps) {
}
};

const onItemPress = useCallback(
(collectible) => {
debouncedNavigation(navigation, collectible);
},
[navigation],
);

const renderCollectible = (collectible: Nft, index: number) => {
if (!collectible) return null;
return (
<TouchableOpacity
key={collectible.address}
style={styles.collectibleCard}
onPress={() => onItemPress(collectible)}
onLongPress={() => onLongPressCollectible(collectible)}
testID={collectible.name as string}
>
<CollectibleMedia
style={styles.collectibleIcon}
collectible={collectible}
isTokenImage
/>
<Text numberOfLines={1} ellipsizeMode="tail">
{collectible.name}
</Text>
<Text numberOfLines={1} ellipsizeMode="tail">
{collectible.collection?.name}
</Text>
</TouchableOpacity>
);
};

const NftGridFooter = () => (
<View
style={{
display: 'flex',
alignItems: 'center',
}}
>
<Text variant={TextVariant.BodyMDMedium} color={TextColor.Alternative}>
{strings('wallet.no_collectibles')}
</Text>
<TouchableOpacity
onPress={() =>
navigation.push('AddAsset', { assetType: 'collectible' })
}
disabled={false}
testID={WalletViewSelectorsIDs.IMPORT_NFT_BUTTON}
>
<Text variant={TextVariant.BodyMDMedium} color={TextColor.Info}>
{strings('wallet.add_collectibles')}
</Text>
</TouchableOpacity>
</View>
);

const NftGridEmpty = () => (
<View
style={{
display: 'flex',
alignItems: 'center',
}}
>
<Image
style={{
height: 90,
width: 90,
tintColor: 'lightgray',
marginTop: 30,
marginBottom: 12,
}}
source={noNftPlaceholderSrc}
resizeMode="contain"
/>
<Text
style={styles.headingMd}
variant={TextVariant.HeadingMD}
color={TextColor.Alternative}
>
{strings('wallet.no_nfts_yet')}
</Text>
<TouchableOpacity
onPress={() =>
navigation.navigate('Webview', {
screen: 'SimpleWebview',
params: { url: AppConstants.URLS.NFT },
})
}
testID={WalletViewSelectorsIDs.IMPORT_NFT_BUTTON}
>
<Text
variant={TextVariant.BodyMDMedium}
color={TextColor.Info}
onPress={() => console.log('goToLearnMore')}
>
{strings('wallet.learn_more')}
</Text>
</TouchableOpacity>
</View>
);

return (
<View testID="collectible-contracts">
{!isNftDetectionEnabled && <CollectibleDetectionModal />}
Expand All @@ -275,16 +153,16 @@ function NftGrid({ navigation, chainId, selectedAddress }: NftGridProps) {
{/* empty state */}
{!isNftFetchingProgress && collectibles.length === 0 && (
<>
<NftGridEmpty />
<NftGridFooter />
<NftGridEmpty navigation={navigation} />
<NftGridFooter navigation={navigation} />
</>
)}
{/* nft grid */}
{!isNftFetchingProgress && collectibles.length > 0 && (
<FlatList
numColumns={3}
data={collectibles}
renderItem={({ item, index }: { item: Nft; index: number }) => (
renderItem={({ item }: { item: Nft }) => (
<NftGridItem nft={item} navigation={navigation} />
)}
keyExtractor={(_, index) => index.toString()}
Expand All @@ -297,7 +175,7 @@ function NftGrid({ navigation, chainId, selectedAddress }: NftGridProps) {
onRefresh={onRefresh}
/>
}
ListFooterComponent={<NftGridFooter />}
ListFooterComponent={<NftGridFooter navigation={navigation} />}
/>
)}
<ActionSheet
Expand Down
76 changes: 76 additions & 0 deletions app/components/UI/NftGrid/NftGridEmpty.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react';
import { View, TouchableOpacity, Image } from 'react-native';
import { strings } from '../../../../locales/i18n';
import Text, {
TextColor,
TextVariant, // TextColor,
// TextVariant,
} from '../../../component-library/components/Texts/Text';
import styleSheet from './NftGrid.styles';
import { useStyles } from '../../hooks/useStyles';
import { StackNavigationProp } from '@react-navigation/stack';
import AppConstants from '../../../core/AppConstants';
import { WalletViewSelectorsIDs } from '../../../../e2e/selectors/wallet/WalletView.selectors';

import noNftPlaceholderSrc from '../../../images/no-nfts-placeholder.png';
interface NftGridNavigationParamList {
AddAsset: { assetType: string };
[key: string]: undefined | object;
}

interface NftGridProps {
navigation: StackNavigationProp<NftGridNavigationParamList, 'AddAsset'>;
}

function NftGridEmpty({ navigation }: NftGridProps) {
const { styles } = useStyles(styleSheet, {});

return (
<View
// eslint-disable-next-line react-native/no-inline-styles
style={{
display: 'flex',
alignItems: 'center',
}}
>
<Image
// eslint-disable-next-line react-native/no-color-literals, react-native/no-inline-styles
style={{
height: 90,
width: 90,
tintColor: 'lightgray',
marginTop: 30,
marginBottom: 12,
}}
source={noNftPlaceholderSrc}
resizeMode="contain"
/>
<Text
style={styles.headingMd}
variant={TextVariant.HeadingMD}
color={TextColor.Alternative}
>
{strings('wallet.no_nfts_yet')}
</Text>
<TouchableOpacity
onPress={() =>
navigation.navigate('Webview', {
screen: 'SimpleWebview',
params: { url: AppConstants.URLS.NFT },
})
}
testID={WalletViewSelectorsIDs.IMPORT_NFT_BUTTON}
>
<Text
variant={TextVariant.BodyMDMedium}
color={TextColor.Info}
onPress={() => console.log('goToLearnMore')}
>
{strings('wallet.learn_more')}
</Text>
</TouchableOpacity>
</View>
);
}

export default NftGridEmpty;
47 changes: 47 additions & 0 deletions app/components/UI/NftGrid/NftGridFooter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import { View, TouchableOpacity } from 'react-native';
import { strings } from '../../../../locales/i18n';
import Text, {
TextColor,
TextVariant,
} from '../../../component-library/components/Texts/Text'; // TextVariant, // TextColor,
import { StackNavigationProp } from '@react-navigation/stack';
import { WalletViewSelectorsIDs } from '../../../../e2e/selectors/wallet/WalletView.selectors';

interface NftGridNavigationParamList {
AddAsset: { assetType: string };
[key: string]: undefined | object;
}

interface NftGridFooterProps {
navigation: StackNavigationProp<NftGridNavigationParamList, 'AddAsset'>;
}

function NftGridFooter({ navigation }: NftGridFooterProps) {
return (
<View
// eslint-disable-next-line react-native/no-inline-styles
style={{
display: 'flex',
alignItems: 'center',
}}
>
<Text variant={TextVariant.BodyMDMedium} color={TextColor.Alternative}>
{strings('wallet.no_collectibles')}
</Text>
<TouchableOpacity
onPress={() =>
navigation.push('AddAsset', { assetType: 'collectible' })
}
disabled={false}
testID={WalletViewSelectorsIDs.IMPORT_NFT_BUTTON}
>
<Text variant={TextVariant.BodyMDMedium} color={TextColor.Info}>
{strings('wallet.add_collectibles')}
</Text>
</TouchableOpacity>
</View>
);
}

export default NftGridFooter;
Loading

0 comments on commit 663942d

Please sign in to comment.