2022-01-19 19:42:41 +01:00
|
|
|
import React, { useContext, useEffect } from 'react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
2020-05-21 19:33:48 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import Identicon from '../../ui/identicon';
|
|
|
|
import Tooltip from '../../ui/tooltip';
|
|
|
|
import CurrencyDisplay from '../../ui/currency-display';
|
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
2021-10-28 01:54:18 +02:00
|
|
|
import { isHardwareKeyring } from '../../../helpers/utils/hardware';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
SEND_ROUTE,
|
|
|
|
BUILD_QUOTE_ROUTE,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../helpers/constants/routes';
|
|
|
|
import { useTokenTracker } from '../../../hooks/useTokenTracker';
|
|
|
|
import { useTokenFiatAmount } from '../../../hooks/useTokenFiatAmount';
|
2022-03-16 23:15:03 +01:00
|
|
|
import { updateSendAsset } from '../../../ducks/send';
|
2021-04-14 09:16:27 +02:00
|
|
|
import { setSwapsFromToken } from '../../../ducks/swaps/swaps';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getCurrentKeyring,
|
2021-03-18 11:20:06 +01:00
|
|
|
getIsSwapsChain,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../selectors/selectors';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import SwapIcon from '../../ui/icon/swap-icon.component';
|
|
|
|
import SendIcon from '../../ui/icon/overview-send-icon.component';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import IconButton from '../../ui/icon-button';
|
2022-01-19 19:42:41 +01:00
|
|
|
import { INVALID_ASSET_TYPE } from '../../../helpers/constants/error-keys';
|
|
|
|
import { showModal } from '../../../store/actions';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2022-04-22 18:09:10 +02:00
|
|
|
import { EVENT } from '../../../../shared/constants/metametrics';
|
2022-03-16 23:15:03 +01:00
|
|
|
import { ASSET_TYPES } from '../../../../shared/constants/transaction';
|
2021-02-04 19:15:23 +01:00
|
|
|
import WalletOverview from './wallet-overview';
|
2020-05-21 19:33:48 +02:00
|
|
|
|
2020-06-01 19:54:32 +02:00
|
|
|
const TokenOverview = ({ className, token }) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const t = useContext(I18nContext);
|
2022-03-14 19:12:38 +01:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2021-02-04 19:15:23 +01:00
|
|
|
const history = useHistory();
|
|
|
|
const keyring = useSelector(getCurrentKeyring);
|
2021-10-28 01:54:18 +02:00
|
|
|
const usingHardwareWallet = isHardwareKeyring(keyring.type);
|
2021-02-04 19:15:23 +01:00
|
|
|
const { tokensWithBalances } = useTokenTracker([token]);
|
|
|
|
const balanceToRender = tokensWithBalances[0]?.string;
|
|
|
|
const balance = tokensWithBalances[0]?.balance;
|
2020-11-03 00:41:28 +01:00
|
|
|
const formattedFiatBalance = useTokenFiatAmount(
|
|
|
|
token.address,
|
|
|
|
balanceToRender,
|
|
|
|
token.symbol,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-03-18 11:20:06 +01:00
|
|
|
const isSwapsChain = useSelector(getIsSwapsChain);
|
2020-05-21 19:33:48 +02:00
|
|
|
|
2022-01-19 19:42:41 +01:00
|
|
|
useEffect(() => {
|
2022-01-20 18:49:49 +01:00
|
|
|
if (token.isERC721 && process.env.COLLECTIBLES_V1) {
|
2022-01-19 19:42:41 +01:00
|
|
|
dispatch(
|
|
|
|
showModal({
|
|
|
|
name: 'CONVERT_TOKEN_TO_NFT',
|
|
|
|
tokenAddress: token.address,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [token.isERC721, token.address, dispatch]);
|
|
|
|
|
2020-05-21 19:33:48 +02:00
|
|
|
return (
|
|
|
|
<WalletOverview
|
2020-11-03 00:41:28 +01:00
|
|
|
balance={
|
2020-05-21 19:33:48 +02:00
|
|
|
<div className="token-overview__balance">
|
2020-06-12 20:47:39 +02:00
|
|
|
<CurrencyDisplay
|
2020-05-21 19:33:48 +02:00
|
|
|
className="token-overview__primary-balance"
|
2020-10-14 18:39:04 +02:00
|
|
|
displayValue={balanceToRender}
|
2020-06-12 20:47:39 +02:00
|
|
|
suffix={token.symbol}
|
2020-05-21 19:33:48 +02:00
|
|
|
/>
|
2020-11-03 00:41:28 +01:00
|
|
|
{formattedFiatBalance ? (
|
|
|
|
<CurrencyDisplay
|
|
|
|
className="token-overview__secondary-balance"
|
|
|
|
displayValue={formattedFiatBalance}
|
|
|
|
hideLabel
|
|
|
|
/>
|
|
|
|
) : null}
|
2020-05-21 19:33:48 +02:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
|
|
|
buttons={
|
2020-10-06 20:28:38 +02:00
|
|
|
<>
|
|
|
|
<IconButton
|
|
|
|
className="token-overview__button"
|
2022-01-19 19:42:41 +01:00
|
|
|
onClick={async () => {
|
2022-03-14 19:12:38 +01:00
|
|
|
trackEvent({
|
|
|
|
event: 'Clicked Send: Token',
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
|
|
|
action: 'Home',
|
|
|
|
legacy_event: true,
|
|
|
|
},
|
|
|
|
});
|
2022-01-19 19:42:41 +01:00
|
|
|
try {
|
|
|
|
await dispatch(
|
|
|
|
updateSendAsset({
|
|
|
|
type: ASSET_TYPES.TOKEN,
|
|
|
|
details: token,
|
|
|
|
}),
|
|
|
|
);
|
2021-06-23 23:35:25 +02:00
|
|
|
history.push(SEND_ROUTE);
|
2022-01-19 19:42:41 +01:00
|
|
|
} catch (err) {
|
|
|
|
if (!err.message.includes(INVALID_ASSET_TYPE)) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
}}
|
|
|
|
Icon={SendIcon}
|
|
|
|
label={t('send')}
|
|
|
|
data-testid="eth-overview-send"
|
2021-06-22 19:39:44 +02:00
|
|
|
disabled={token.isERC721}
|
2020-10-06 20:28:38 +02:00
|
|
|
/>
|
2021-04-14 09:16:27 +02:00
|
|
|
<IconButton
|
|
|
|
className="token-overview__button"
|
|
|
|
disabled={!isSwapsChain}
|
|
|
|
Icon={SwapIcon}
|
|
|
|
onClick={() => {
|
|
|
|
if (isSwapsChain) {
|
2022-03-25 14:21:29 +01:00
|
|
|
trackEvent({
|
|
|
|
event: 'Swaps Opened',
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.SWAPS,
|
2022-03-25 14:21:29 +01:00
|
|
|
properties: {
|
|
|
|
source: 'Token View',
|
|
|
|
active_currency: token.symbol,
|
|
|
|
},
|
|
|
|
});
|
2021-04-14 09:16:27 +02:00
|
|
|
dispatch(
|
|
|
|
setSwapsFromToken({
|
|
|
|
...token,
|
2022-03-21 14:29:38 +01:00
|
|
|
address: token.address.toLowerCase(),
|
2021-09-10 19:37:19 +02:00
|
|
|
iconUrl: token.image,
|
2021-04-14 09:16:27 +02:00
|
|
|
balance,
|
|
|
|
string: balanceToRender,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
if (usingHardwareWallet) {
|
|
|
|
global.platform.openExtensionInBrowser(BUILD_QUOTE_ROUTE);
|
|
|
|
} else {
|
|
|
|
history.push(BUILD_QUOTE_ROUTE);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2021-04-14 09:16:27 +02:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
label={t('swap')}
|
|
|
|
tooltipRender={(contents) => (
|
|
|
|
<Tooltip
|
2021-09-20 15:51:22 +02:00
|
|
|
title={t('currentlyUnavailable')}
|
2021-04-14 09:16:27 +02:00
|
|
|
position="bottom"
|
|
|
|
disabled={isSwapsChain}
|
|
|
|
>
|
|
|
|
{contents}
|
|
|
|
</Tooltip>
|
|
|
|
)}
|
|
|
|
/>
|
2020-10-06 20:28:38 +02:00
|
|
|
</>
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2020-06-01 19:54:32 +02:00
|
|
|
className={className}
|
2020-11-03 00:41:28 +01:00
|
|
|
icon={
|
2021-09-10 19:37:19 +02:00
|
|
|
<Identicon diameter={32} address={token.address} image={token.image} />
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2020-05-21 19:33:48 +02:00
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2020-05-21 19:33:48 +02:00
|
|
|
|
|
|
|
TokenOverview.propTypes = {
|
2020-06-01 19:54:32 +02:00
|
|
|
className: PropTypes.string,
|
2020-05-21 19:33:48 +02:00
|
|
|
token: PropTypes.shape({
|
|
|
|
address: PropTypes.string.isRequired,
|
|
|
|
decimals: PropTypes.number,
|
|
|
|
symbol: PropTypes.string,
|
2021-09-10 19:37:19 +02:00
|
|
|
image: PropTypes.string,
|
2021-06-22 19:39:44 +02:00
|
|
|
isERC721: PropTypes.bool,
|
2020-05-21 19:33:48 +02:00
|
|
|
}).isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-21 19:33:48 +02:00
|
|
|
|
2020-06-01 19:54:32 +02:00
|
|
|
TokenOverview.defaultProps = {
|
|
|
|
className: undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-06-01 19:54:32 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export default TokenOverview;
|