import React, { useCallback, useContext, useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { useHistory } from 'react-router-dom'; import { ASSET_ROUTE, IMPORT_TOKEN_ROUTE, } from '../../helpers/constants/routes'; 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 { getMostRecentOverviewPage } from '../../ducks/history/history'; import { getPendingTokens } from '../../ducks/metamask/metamask'; import { addTokens, clearPendingTokens } from '../../store/actions'; import { EVENT, EVENT_NAMES } from '../../../shared/constants/metametrics'; import { AssetType, TokenStandard, } from '../../../shared/constants/transaction'; const getTokenName = (name, symbol) => { return name === undefined ? symbol : `${name} (${symbol})`; }; const ConfirmImportToken = () => { const t = useContext(I18nContext); const dispatch = useDispatch(); const history = useHistory(); const trackEvent = useContext(MetaMetricsContext); const mostRecentOverviewPage = useSelector(getMostRecentOverviewPage); const pendingTokens = useSelector(getPendingTokens); const handleAddTokens = useCallback(async () => { await dispatch(addTokens(pendingTokens)); const addedTokenValues = Object.values(pendingTokens); const firstTokenAddress = addedTokenValues?.[0].address?.toLowerCase(); addedTokenValues.forEach((pendingToken) => { trackEvent({ event: EVENT_NAMES.TOKEN_ADDED, category: EVENT.CATEGORIES.WALLET, sensitiveProperties: { token_symbol: pendingToken.symbol, token_contract_address: pendingToken.address, token_decimal_precision: pendingToken.decimals, unlisted: pendingToken.unlisted, source: pendingToken.isCustom ? EVENT.SOURCE.TOKEN.CUSTOM : EVENT.SOURCE.TOKEN.LIST, token_standard: TokenStandard.ERC20, asset_type: AssetType.token, }, }); }); dispatch(clearPendingTokens()); if (firstTokenAddress) { history.push(`${ASSET_ROUTE}/${firstTokenAddress}`); } else { history.push(mostRecentOverviewPage); } }, [dispatch, history, mostRecentOverviewPage, pendingTokens, trackEvent]); useEffect(() => { if (Object.keys(pendingTokens).length === 0) { history.push(mostRecentOverviewPage); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return (
{t('importTokensCamelCase')}
{t('likeToImportTokens')}
{t('token')}
{t('balance')}
{Object.entries(pendingTokens).map(([address, token]) => { const { name, symbol } = token; return (
{getTokenName(name, symbol)}
); })}
); }; export default ConfirmImportToken;