import React, { useContext, useState } from 'react'; import PropTypes from 'prop-types'; import { useSelector } from 'react-redux'; import TokenList from '../token-list'; import { PRIMARY, SECONDARY } from '../../../helpers/constants/common'; import { useUserPreferencedCurrency } from '../../../hooks/useUserPreferencedCurrency'; import { getSelectedAccountCachedBalance, getShouldShowFiat, getNativeCurrencyImage, getDetectedTokensInCurrentNetwork, getIstokenDetectionInactiveOnNonMainnetSupportedNetwork, getTokenList, } from '../../../selectors'; import { getNativeCurrency } from '../../../ducks/metamask/metamask'; import { useCurrencyDisplay } from '../../../hooks/useCurrencyDisplay'; import Box from '../../ui/box/box'; import { MetaMetricsContext } from '../../../contexts/metametrics'; import { MetaMetricsEventCategory, MetaMetricsEventName, } from '../../../../shared/constants/metametrics'; import DetectedToken from '../detected-token/detected-token'; import { DetectedTokensBanner, MultichainTokenListItem, MultichainImportTokenLink, } from '../../multichain'; const AssetList = ({ onClickAsset }) => { const [showDetectedTokens, setShowDetectedTokens] = useState(false); const selectedAccountBalance = useSelector(getSelectedAccountCachedBalance); const nativeCurrency = useSelector(getNativeCurrency); const showFiat = useSelector(getShouldShowFiat); const trackEvent = useContext(MetaMetricsContext); const balance = useSelector(getSelectedAccountCachedBalance); const balanceIsLoading = !balance; const { currency: primaryCurrency, numberOfDecimals: primaryNumberOfDecimals, } = useUserPreferencedCurrency(PRIMARY, { ethNumberOfDecimals: 4 }); const { currency: secondaryCurrency, numberOfDecimals: secondaryNumberOfDecimals, } = useUserPreferencedCurrency(SECONDARY, { ethNumberOfDecimals: 4 }); const [, primaryCurrencyProperties] = useCurrencyDisplay( selectedAccountBalance, { numberOfDecimals: primaryNumberOfDecimals, currency: primaryCurrency, }, ); const [secondaryCurrencyDisplay, secondaryCurrencyProperties] = useCurrencyDisplay(selectedAccountBalance, { numberOfDecimals: secondaryNumberOfDecimals, currency: secondaryCurrency, }); const primaryTokenImage = useSelector(getNativeCurrencyImage); const detectedTokens = useSelector(getDetectedTokensInCurrentNetwork) || []; const istokenDetectionInactiveOnNonMainnetSupportedNetwork = useSelector( getIstokenDetectionInactiveOnNonMainnetSupportedNetwork, ); const tokenList = useSelector(getTokenList); const tokenData = Object.values(tokenList).find( (token) => token.symbol === primaryCurrencyProperties.suffix, ); const title = tokenData?.name || primaryCurrencyProperties.suffix; return ( <> onClickAsset(nativeCurrency)} title={title} primary={ primaryCurrencyProperties.value ?? secondaryCurrencyProperties.value } tokenSymbol={primaryCurrencyProperties.suffix} secondary={showFiat ? secondaryCurrencyDisplay : undefined} tokenImage={balanceIsLoading ? null : primaryTokenImage} /> { onClickAsset(tokenAddress); trackEvent({ event: MetaMetricsEventName.TokenScreenOpened, category: MetaMetricsEventCategory.Navigation, properties: { token_symbol: primaryCurrencyProperties.suffix, location: 'Home', }, }); }} /> {detectedTokens.length > 0 && !istokenDetectionInactiveOnNonMainnetSupportedNetwork ? ( setShowDetectedTokens(true)} margin={4} /> ) : null} 0 ? 0 : 4}> {showDetectedTokens && ( )} ); }; AssetList.propTypes = { onClickAsset: PropTypes.func.isRequired, }; export default AssetList;