import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { useDispatch, useSelector } from 'react-redux'; import { useHistory } from 'react-router-dom'; import { isEqual } from 'lodash'; import Box from '../../ui/box'; import Button from '../../ui/button'; import Typography from '../../ui/typography/typography'; import CollectiblesDetectionNotice from '../collectibles-detection-notice'; import CollectiblesItems from '../collectibles-items'; import { COLORS, TYPOGRAPHY, TEXT_ALIGN, JUSTIFY_CONTENT, FLEX_DIRECTION, FONT_WEIGHT, ALIGN_ITEMS, } from '../../../helpers/constants/design-system'; import { useI18nContext } from '../../../hooks/useI18nContext'; import { getCollectibles, getCollectibleContracts, getCollectiblesDetectionNoticeDismissed, } from '../../../ducks/metamask/metamask'; import { getIsMainnet, getUseCollectibleDetection } from '../../../selectors'; import { EXPERIMENTAL_ROUTE } from '../../../helpers/constants/routes'; import { checkAndUpdateAllCollectiblesOwnershipStatus, detectCollectibles, } from '../../../store/actions'; import { usePrevious } from '../../../hooks/usePrevious'; export default function CollectiblesTab({ onAddNFT }) { const collectibles = useSelector(getCollectibles); const collectibleContracts = useSelector(getCollectibleContracts); const useCollectibleDetection = useSelector(getUseCollectibleDetection); const isMainnet = useSelector(getIsMainnet); const collectibleDetectionNoticeDismissed = useSelector( getCollectiblesDetectionNoticeDismissed, ); const history = useHistory(); const t = useI18nContext(); const dispatch = useDispatch(); const [collections, setCollections] = useState({}); const [previouslyOwnedCollection, setPreviouslyOwnedCollection] = useState({ collectionName: 'Previously Owned', collectibles: [], }); const prevCollectibles = usePrevious(collectibles); useEffect(() => { const getCollections = () => { const newCollections = {}; const newPreviouslyOwnedCollections = { collectionName: 'Previously Owned', collectibles: [], }; collectibles.forEach((collectible) => { if (collectible?.isCurrentlyOwned === false) { newPreviouslyOwnedCollections.collectibles.push(collectible); } else if (newCollections[collectible.address]) { newCollections[collectible.address].collectibles.push(collectible); } else { const collectionContract = collectibleContracts.find( ({ address }) => address === collectible.address, ); newCollections[collectible.address] = { collectionName: collectionContract?.name || collectible.name, collectionImage: collectionContract?.logo || collectible.collectionImage, collectibles: [collectible], }; } }); setCollections(newCollections); setPreviouslyOwnedCollection(newPreviouslyOwnedCollections); }; if (!isEqual(prevCollectibles, collectibles)) { getCollections(); } }, [collectibles, prevCollectibles, collectibleContracts]); const onEnableAutoDetect = () => { history.push(EXPERIMENTAL_ROUTE); }; const onRefresh = () => { if (isMainnet) { dispatch(detectCollectibles()); } checkAndUpdateAllCollectiblesOwnershipStatus(); }; return (
{Object.keys(collections).length > 0 || previouslyOwnedCollection.collectibles.length > 0 ? ( ) : ( {isMainnet && !useCollectibleDetection && !collectibleDetectionNoticeDismissed ? ( ) : null} {t('noNFTs')} )} {t('missingNFT')} {!isMainnet && Object.keys(collections).length < 1 ? null : ( <> {isMainnet && !useCollectibleDetection ? ( ) : ( )} {t('or')} )}
); } CollectiblesTab.propTypes = { onAddNFT: PropTypes.func.isRequired, };