import React, { useContext, useEffect } from 'react'; import PropTypes from 'prop-types'; import { useDispatch } from 'react-redux'; import { useHistory } from 'react-router-dom'; import CurrencyDisplay from '../../ui/currency-display'; import { I18nContext } from '../../../contexts/i18n'; import { SEND_ROUTE } from '../../../helpers/constants/routes'; import { useTokenTracker } from '../../../hooks/useTokenTracker'; import { useTokenFiatAmount } from '../../../hooks/useTokenFiatAmount'; import { startNewDraftTransaction } from '../../../ducks/send'; import IconButton from '../../ui/icon-button'; import { INVALID_ASSET_TYPE } from '../../../helpers/constants/error-keys'; import { showModal } from '../../../store/actions'; import { AssetType } from '../../../../shared/constants/transaction'; import WalletOverview from './wallet-overview'; const TokenOverview = ({ className, token }) => { const dispatch = useDispatch(); const t = useContext(I18nContext); const history = useHistory(); const { tokensWithBalances } = useTokenTracker([token]); const balanceToRender = tokensWithBalances[0]?.string; const formattedFiatBalance = useTokenFiatAmount( token.address, balanceToRender, token.symbol, ); useEffect(() => { if (token.isERC721) { dispatch( showModal({ name: 'CONVERT_TOKEN_TO_NFT', tokenAddress: token.address, }), ); } }, [token.isERC721, token.address, dispatch]); return (
{formattedFiatBalance ? ( ) : null} } buttons={ <> { try { await dispatch( startNewDraftTransaction({ type: AssetType.token, details: token, }), ); history.push(SEND_ROUTE); } catch (err) { if (!err.message.includes(INVALID_ASSET_TYPE)) { throw err; } } }} label={t('send')} data-testid="eth-overview-send" disabled={token.isERC721} /> } className={className} /> ); }; TokenOverview.propTypes = { className: PropTypes.string, token: PropTypes.shape({ address: PropTypes.string.isRequired, decimals: PropTypes.number, symbol: PropTypes.string, image: PropTypes.string, isERC721: PropTypes.bool, }).isRequired, }; TokenOverview.defaultProps = { className: undefined, }; export default TokenOverview;