import React, { Component } 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 { getEnvironmentType } from '../../../app/scripts/lib/util'; import { ENVIRONMENT_TYPE_NOTIFICATION } from '../../../shared/constants/app'; import { isEqualCaseInsensitive } from '../../helpers/utils/util'; export default class ConfirmAddSuggestedToken extends Component { static contextTypes = { t: PropTypes.func, trackEvent: PropTypes.func, }; static propTypes = { history: PropTypes.object, acceptWatchAsset: PropTypes.func, rejectWatchAsset: PropTypes.func, mostRecentOverviewPage: PropTypes.string.isRequired, suggestedAssets: PropTypes.array, tokens: PropTypes.array, }; componentDidMount() { this._checksuggestedAssets(); } componentDidUpdate() { this._checksuggestedAssets(); } _checksuggestedAssets() { const { mostRecentOverviewPage, suggestedAssets = [], history, } = this.props; if (suggestedAssets.length > 0) { return; } if (getEnvironmentType() === ENVIRONMENT_TYPE_NOTIFICATION) { global.platform.closeCurrentWindow(); } else { history.push(mostRecentOverviewPage); } } getTokenName(name, symbol) { return typeof name === 'undefined' ? symbol : `${name} (${symbol})`; } render() { const { suggestedAssets, tokens, rejectWatchAsset, history, mostRecentOverviewPage, acceptWatchAsset, } = this.props; const hasTokenDuplicates = this.checkTokenDuplicates( suggestedAssets, tokens, ); const reusesName = this.checkNameReuse(suggestedAssets, tokens); return (
{this.context.t('addSuggestedTokens')}
{this.context.t('likeToImportTokens')}
{hasTokenDuplicates ? (
{this.context.t('knownTokenWarning')}
) : null} {reusesName ? (
{this.context.t('reusedTokenNameWarning')}
) : null}
{this.context.t('token')}
{this.context.t('balance')}
{suggestedAssets.map(({ asset }) => { return (
{this.getTokenName(asset.name, asset.symbol)}
); })}
); } checkTokenDuplicates(suggestedAssets, tokens) { 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; } /** * 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. */ checkNameReuse(suggestedAssets, tokens) { 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; } }