Skip to content

Commit

Permalink
fix: refactor send component and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri committed May 30, 2024
1 parent e9ea416 commit 6d83298
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 47 deletions.
6 changes: 6 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,10 @@ export default class MetamaskController extends EventEmitter {
this.assetsContractController,
),
addNft: this.nftController.addNft.bind(this.nftController),
updateNftFetchingProgressStatus:
this.nftController.updateNftFetchingProgressStatus.bind(
this.nftController,
),
getNftApi: this.nftController.getNftApi.bind(this.nftController),
getNftState: () => this.nftController.state,
// added this to track previous value of useNftDetection, should be true on very first initializing of controller[]
Expand Down Expand Up @@ -3115,6 +3119,8 @@ export default class MetamaskController extends EventEmitter {
nftController.checkAndUpdateSingleNftOwnershipStatus.bind(
nftController,
),
updateNftFetchingProgressStatus:
nftController.updateNftFetchingProgressStatus.bind(nftController),

isNftOwner: nftController.isNftOwner.bind(nftController),

Expand Down
10 changes: 9 additions & 1 deletion ui/components/app/nfts-tab/index.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
.nfts-tab {
&__fetching {
display: flex;
height: 100px;
align-items: center;
justify-content: center;
padding: 30px;
}

&__loading {
display: flex;
height: 250px;
height: 200px;
align-items: center;
justify-content: center;
padding: 30px;
Expand Down
64 changes: 56 additions & 8 deletions ui/components/app/nfts-tab/nfts-tab.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import {
Expand Down Expand Up @@ -48,6 +48,8 @@ import {
RampsCard,
} from '../../multichain/ramps-card/ramps-card';
import { useAccountTotalFiatBalance } from '../../../hooks/useAccountTotalFiatBalance';
import { getIsStillNftsFetching } from '../../../ducks/metamask/metamask';
import Spinner from '../../ui/spinner';
///: END:ONLY_INCLUDE_IF

export default function NftsTab() {
Expand All @@ -63,11 +65,16 @@ export default function NftsTab() {
const shouldHideZeroBalanceTokens = useSelector(
getShouldHideZeroBalanceTokens,
);

const isNftsStillFetched = useSelector(getIsStillNftsFetching);

const { totalFiatBalance } = useAccountTotalFiatBalance(
selectedAddress,
shouldHideZeroBalanceTokens,
);
const balanceIsZero = Number(totalFiatBalance) === 0;
const [showLoader, setShowLoader] = useState(true);
const [showRefreshLoader, setShowRefreshLoader] = useState(false);
const isBuyableChain = useSelector(getIsBuyableChain);
const showRampsCard = isBuyableChain && balanceIsZero;
///: END:ONLY_INCLUDE_IF
Expand All @@ -80,9 +87,15 @@ export default function NftsTab() {
};

const onRefresh = () => {
setShowRefreshLoader(true);
if (isMainnet) {
dispatch(detectNfts());
detectNfts();
}
const timeoutForRefresh = setTimeout(() => {
setShowRefreshLoader(false);
clearTimeout(timeoutForRefresh);
}, 200);

checkAndUpdateAllNftsOwnershipStatus();
};

Expand Down Expand Up @@ -114,10 +127,34 @@ export default function NftsTab() {
currentLocale,
]);

if (nftsLoading) {
return <div className="nfts-tab__loading">{t('loadingNFTs')}</div>;
useEffect(() => {
const timeoutId = setTimeout(() => {
setShowLoader(false);
}, 2000);
return () => clearTimeout(timeoutId);
}, []);

if (!hasAnyNfts && showLoader) {
return (
<Box className="nfts-tab__loading">
<Spinner
color="var(--color-warning-default)"
className="loading-overlay__spinner"
/>
</Box>
);
}

if (showRefreshLoader) {
return (
<Box className="nfts-tab__loading">
<Spinner
color="var(--color-warning-default)"
className="loading-overlay__spinner"
/>
</Box>
);
}
return (
<>
{
Expand All @@ -129,10 +166,21 @@ export default function NftsTab() {
}
<Box className="nfts-tab">
{hasAnyNfts > 0 || previouslyOwnedCollection.nfts.length > 0 ? (
<NftsItems
collections={collections}
previouslyOwnedCollection={previouslyOwnedCollection}
/>
<Box>
<NftsItems
collections={collections}
previouslyOwnedCollection={previouslyOwnedCollection}
/>

{isNftsStillFetched.isFetchingInProgress ? (
<Box className="nfts-tab__fetching">
<Spinner
color="var(--color-warning-default)"
className="loading-overlay__spinner"
/>
</Box>
) : null}
</Box>
) : (
<>
{isMainnet && !useNftDetection ? (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import React, { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import NftsItems from '../../../app/nfts-items/nfts-items';
import {
Box,
Text,
ButtonLink,
ButtonLinkSize,
} from '../../../component-library';
import {
TextColor,
TextVariant,
TextAlign,
Display,
JustifyContent,
AlignItems,
FlexDirection,
} from '../../../../helpers/constants/design-system';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import { TokenStandard } from '../../../../../shared/constants/transaction';
import ZENDESK_URLS from '../../../../helpers/constants/zendesk-url';
import Spinner from '../../../ui/spinner';
import { useScrollRequired } from '../../../../hooks/useScrollRequired';
import { getIsStillNftsFetching } from '../../../../ducks/metamask/metamask';
import PropTypes from 'prop-types';

type NFT = {
address: string;
description: string | null;
favorite: boolean;
image: string | null;
isCurrentlyOwned: boolean;
name: string | null;
standard: TokenStandard;
tokenId: string;
tokenURI?: string;
};

type Collection = {
collectionName: string;
collectionImage: string | null;
nfts: NFT[];
};

type AssetPickerModalNftTabProps = {
collectionDataFiltered: Collection[];
previouslyOwnedCollection: any;
onClose: () => void;
};

export function AssetPickerModalNftTab({
collectionDataFiltered,
previouslyOwnedCollection,
onClose
}: AssetPickerModalNftTabProps) {
const t = useI18nContext();

const hasAnyNfts = Object.keys(collectionDataFiltered).length > 0;
const { isScrollable, isScrolledToBottom, ref, onScroll } =
useScrollRequired();
const isNftsStillFetched = useSelector(getIsStillNftsFetching);
const [showLoader, setShowLoader] = useState(true);

useEffect(() => {
// Use setTimeout to update the message after 2000 milliseconds (2 seconds)
const timeoutId = setTimeout(() => {
setShowLoader(false);
}, 2000);

// Cleanup function to clear the timeout if the component unmounts
return () => clearTimeout(timeoutId);
}, []); // Empty dependency array ensures the effect runs only once

if (!hasAnyNfts && showLoader) {
return (
<Box className="modal-tab__loading">
<Spinner
color="var(--color-warning-default)"
className="loading-overlay__spinner"
/>
</Box>
);
}

if (hasAnyNfts) {
return (
<Box
className="modal-tab__main-view"
ref={ref}
onScroll={onScroll}
>
<NftsItems
collections={collectionDataFiltered}
previouslyOwnedCollection={previouslyOwnedCollection}
isModal={true}
onCloseModal={() => onClose()}
showTokenId={true}
displayPreviouslyOwnedCollection={false}
/>
{isScrollable &&
isScrolledToBottom &&
isNftsStillFetched.isFetchingInProgress ? (
<Box className="modal-tab__loading">
<Spinner
color="var(--color-warning-default)"
className="loading-overlay__spinner"
/>
</Box>
) : null}
</Box>
);
}
return (
<Box
padding={12}
display={Display.Flex}
flexDirection={FlexDirection.Column}
alignItems={AlignItems.center}
justifyContent={JustifyContent.center}
>
<Box justifyContent={JustifyContent.center}>
<img src="./images/no-nfts.svg" />
</Box>
<Box
marginTop={4}
marginBottom={12}
display={Display.Flex}
justifyContent={JustifyContent.center}
alignItems={AlignItems.center}
flexDirection={FlexDirection.Column}
className="nfts-tab__link"
>
<Text
color={TextColor.textMuted}
variant={TextVariant.headingSm}
textAlign={TextAlign.Center}
as="h4"
>
{t('noNFTs')}
</Text>
<ButtonLink
size={ButtonLinkSize.Sm}
href={ZENDESK_URLS.NFT_TOKENS}
externalLink
>
{t('learnMoreUpperCase')}
</ButtonLink>
</Box>
</Box>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,22 @@ import { useSelector } from 'react-redux';
import classnames from 'classnames';
import { isEqual } from 'lodash';
import { Tab, Tabs } from '../../../ui/tabs';
import NftsItems from '../../../app/nfts-items/nfts-items';
import {
Modal,
ModalContent,
ModalOverlay,
ModalHeader,
TextFieldSearch,
Box,
Text,
ButtonLink,
ButtonLinkSize,
ButtonIconSize,
TextFieldSearchSize,
} from '../../../component-library';
import {
BlockSize,
BorderRadius,
BackgroundColor,
TextColor,
TextVariant,
TextAlign,
Display,
JustifyContent,
AlignItems,
FlexDirection,
FlexWrap,
} from '../../../../helpers/constants/design-system';
import { useI18nContext } from '../../../../hooks/useI18nContext';
Expand All @@ -54,7 +45,7 @@ import { useCurrencyDisplay } from '../../../../hooks/useCurrencyDisplay';
import TokenCell from '../../../app/token-cell';
import { TokenListItem } from '../../token-list-item';
import { useNftsCollections } from '../../../../hooks/useNftsCollections';
import ZENDESK_URLS from '../../../../helpers/constants/zendesk-url';
import { AssetPickerModalNftTab } from './asset-picker-modal-nft-tab';

type AssetPickerModalProps = {
isOpen: boolean;
Expand Down Expand Up @@ -121,8 +112,6 @@ export function AssetPickerModal({

const { collections, previouslyOwnedCollection } = useNftsCollections();

const hasAnyNfts = Object.keys(collections).length > 0;

const {
currency: primaryCurrency,
numberOfDecimals: primaryNumberOfDecimals,
Expand Down Expand Up @@ -365,8 +354,17 @@ export function AssetPickerModal({
size={TextFieldSearchSize.Lg}
/>
</Box>
{hasAnyNfts ? (
<Box className="modal-tab__main-view">
<AssetPickerModalNftTab
collectionDataFiltered={collectionDataFiltered}
previouslyOwnedCollection={previouslyOwnedCollection}
onClose={onClose}
/>
{/* {hasAnyNfts ? (
<Box
className="modal-tab__main-view"
ref={ref}
onScroll={onScroll}
>
<NftsItems
collections={collectionDataFiltered}
previouslyOwnedCollection={previouslyOwnedCollection}
Expand All @@ -375,6 +373,16 @@ export function AssetPickerModal({
showTokenId={true}
displayPreviouslyOwnedCollection={false}
/>
{isScrollable &&
isScrolledToBottom &&
isNftsStillFetched.isFetchingInProgress ? (
<Box className="modal-tab__loading">
<Spinner
color="var(--color-warning-default)"
className="loading-overlay__spinner"
/>
</Box>
) : null}
</Box>
) : (
<Box
Expand Down Expand Up @@ -413,7 +421,7 @@ export function AssetPickerModal({
</ButtonLink>
</Box>
</Box>
)}
)} */}
</Tab>
}
</Tabs>
Expand Down
Loading

0 comments on commit 6d83298

Please sign in to comment.