1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

110 lines
3.2 KiB
JavaScript

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 (
<WalletOverview
showAddress={false}
balance={
<div className="token-overview__balance">
<div className="token-overview__primary-container">
<CurrencyDisplay
style={{ display: 'contents' }}
className="token-overview__primary-balance"
displayValue={balanceToRender}
suffix={token.symbol}
/>
</div>
{formattedFiatBalance ? (
<CurrencyDisplay
className="token-overview__secondary-balance"
displayValue={formattedFiatBalance}
hideLabel
/>
) : null}
</div>
}
buttons={
<>
<IconButton
className="token-overview__button"
onClick={async () => {
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;