import React, { useContext, useEffect } from 'react'; import PropTypes from 'prop-types'; import Button from '../../components/ui/button'; import Identicon from '../../components/ui/identicon'; import TokenBalance from '../../components/ui/token-balance'; import { I18nContext } from '../../contexts/i18n'; import { MetaMetricsContext } from '../../contexts/metametrics'; import { isEqualCaseInsensitive } from '../../helpers/utils/util'; function getTokenName(name, symbol) { return typeof name === 'undefined' ? symbol : `${name} (${symbol})`; } const ConfirmAddSuggestedToken = (props) => { const { acceptWatchAsset, history, mostRecentOverviewPage, rejectWatchAsset, suggestedAssets, tokens, } = props; const metricsEvent = useContext(MetaMetricsContext); const t = useContext(I18nContext); const tokenAddedEvent = (asset) => { metricsEvent({ event: 'Token Added', category: 'Wallet', sensitiveProperties: { token_symbol: asset.symbol, token_contract_address: asset.address, token_decimal_precision: asset.decimals, unlisted: asset.unlisted, source: 'dapp', }, }); }; /** * Returns true if any suggestedAssets both: * - Share a symbol with an existing `tokens` member. * - Does not share an address with that same `tokens` member. * This should be flagged as possibly deceptive or confusing. */ const checkNameReuse = () => { const duplicates = suggestedAssets.filter(({ asset }) => { const dupes = tokens.filter( (old) => old.symbol === asset.symbol && !isEqualCaseInsensitive(old.address, asset.address), ); return dupes.length > 0; }); return duplicates.length > 0; }; const checkTokenDuplicates = () => { const pending = suggestedAssets.map(({ asset }) => asset.address.toUpperCase(), ); const existing = tokens.map((token) => token.address.toUpperCase()); const dupes = pending.filter((proposed) => { return existing.includes(proposed); }); return dupes.length > 0; }; const hasTokenDuplicates = checkTokenDuplicates(); const reusesName = checkNameReuse(); useEffect(() => { if (!suggestedAssets.length) { history.push(mostRecentOverviewPage); } }, [history, suggestedAssets, mostRecentOverviewPage]); return (