2023-03-01 09:45:27 +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-07-01 15:58:35 +02:00
|
|
|
import { startNewDraftTransaction } 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,
|
2023-03-01 09:45:27 +01:00
|
|
|
getIsBuyableChain,
|
2023-02-02 21:15:26 +01:00
|
|
|
} from '../../../selectors';
|
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';
|
2023-02-21 16:32:08 +01:00
|
|
|
import {
|
2023-04-03 17:31:04 +02:00
|
|
|
MetaMetricsContextProp,
|
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventName,
|
|
|
|
MetaMetricsSwapsEventSource,
|
2023-02-21 16:32:08 +01:00
|
|
|
} from '../../../../shared/constants/metametrics';
|
2023-01-18 15:47:29 +01:00
|
|
|
import { AssetType } from '../../../../shared/constants/transaction';
|
2023-03-01 09:45:27 +01:00
|
|
|
import useRamps from '../../../hooks/experiences/useRamps';
|
2023-01-31 15:47:43 +01:00
|
|
|
|
2023-04-19 23:16:49 +02:00
|
|
|
import { Icon, IconName } from '../../component-library';
|
2023-02-02 21:15:26 +01:00
|
|
|
import { IconColor } from '../../../helpers/constants/design-system';
|
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);
|
2022-09-28 16:41:56 +02:00
|
|
|
|
2023-03-01 09:45:27 +01:00
|
|
|
const isBuyableChain = useSelector(getIsBuyableChain);
|
|
|
|
|
|
|
|
const { openBuyCryptoInPdapp } = useRamps();
|
2020-05-21 19:33:48 +02:00
|
|
|
|
2022-01-19 19:42:41 +01:00
|
|
|
useEffect(() => {
|
2023-03-13 20:29:37 +01:00
|
|
|
if (token.isERC721) {
|
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 (
|
2023-03-01 09:45:27 +01:00
|
|
|
<WalletOverview
|
|
|
|
balance={
|
|
|
|
<div className="token-overview__balance">
|
|
|
|
<CurrencyDisplay
|
|
|
|
className="token-overview__primary-balance"
|
|
|
|
displayValue={balanceToRender}
|
|
|
|
suffix={token.symbol}
|
|
|
|
/>
|
|
|
|
{formattedFiatBalance ? (
|
2020-11-03 00:41:28 +01:00
|
|
|
<CurrencyDisplay
|
2023-03-01 09:45:27 +01:00
|
|
|
className="token-overview__secondary-balance"
|
|
|
|
displayValue={formattedFiatBalance}
|
|
|
|
hideLabel
|
2020-11-03 00:41:28 +01:00
|
|
|
/>
|
2023-03-01 09:45:27 +01:00
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
buttons={
|
|
|
|
<>
|
|
|
|
<IconButton
|
|
|
|
className="token-overview__button"
|
2023-04-19 23:16:49 +02:00
|
|
|
Icon={<Icon name={IconName.Add} color={IconColor.primaryInverse} />}
|
2023-03-01 09:45:27 +01:00
|
|
|
label={t('buy')}
|
|
|
|
data-testid="token-overview-buy"
|
|
|
|
onClick={() => {
|
|
|
|
openBuyCryptoInPdapp();
|
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.NavBuyButtonClicked,
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
2023-03-01 09:45:27 +01:00
|
|
|
properties: {
|
|
|
|
location: 'Token Overview',
|
|
|
|
text: 'Buy',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
disabled={token.isERC721 || !isBuyableChain}
|
|
|
|
/>
|
|
|
|
<IconButton
|
|
|
|
className="token-overview__button"
|
|
|
|
onClick={async () => {
|
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.NavSendButtonClicked,
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
2023-03-01 09:45:27 +01:00
|
|
|
properties: {
|
|
|
|
token_symbol: token.symbol,
|
2023-04-03 17:31:04 +02:00
|
|
|
location: MetaMetricsSwapsEventSource.TokenView,
|
2023-03-01 09:45:27 +01:00
|
|
|
text: 'Send',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await dispatch(
|
|
|
|
startNewDraftTransaction({
|
|
|
|
type: AssetType.token,
|
|
|
|
details: token,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
history.push(SEND_ROUTE);
|
|
|
|
} catch (err) {
|
|
|
|
if (!err.message.includes(INVALID_ASSET_TYPE)) {
|
|
|
|
throw err;
|
2023-01-31 15:47:43 +01:00
|
|
|
}
|
2023-03-01 09:45:27 +01:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
Icon={
|
|
|
|
<Icon
|
2023-04-19 23:16:49 +02:00
|
|
|
name={IconName.Arrow2UpRight}
|
2023-03-01 09:45:27 +01:00
|
|
|
color={IconColor.primaryInverse}
|
2022-09-19 17:00:57 +02:00
|
|
|
/>
|
2023-03-01 09:45:27 +01:00
|
|
|
}
|
|
|
|
label={t('send')}
|
|
|
|
data-testid="eth-overview-send"
|
|
|
|
disabled={token.isERC721}
|
|
|
|
/>
|
|
|
|
<IconButton
|
|
|
|
className="token-overview__button"
|
|
|
|
disabled={!isSwapsChain}
|
|
|
|
Icon={
|
|
|
|
<Icon
|
2023-04-19 23:16:49 +02:00
|
|
|
name={IconName.SwapHorizontal}
|
2023-03-01 09:45:27 +01:00
|
|
|
color={IconColor.primaryInverse}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
onClick={() => {
|
|
|
|
if (isSwapsChain) {
|
2022-03-25 14:21:29 +01:00
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.NavSwapButtonClicked,
|
|
|
|
category: MetaMetricsEventCategory.Swaps,
|
2022-03-25 14:21:29 +01:00
|
|
|
properties: {
|
2022-08-16 18:39:23 +02:00
|
|
|
token_symbol: token.symbol,
|
2023-04-03 17:31:04 +02:00
|
|
|
location: MetaMetricsSwapsEventSource.TokenView,
|
2023-03-01 09:45:27 +01:00
|
|
|
text: 'Swap',
|
2022-03-25 14:21:29 +01:00
|
|
|
},
|
|
|
|
});
|
2023-03-01 09:45:27 +01:00
|
|
|
dispatch(
|
|
|
|
setSwapsFromToken({
|
|
|
|
...token,
|
|
|
|
address: token.address.toLowerCase(),
|
|
|
|
iconUrl: token.image,
|
|
|
|
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
|
|
|
}
|
2023-03-01 09:45:27 +01:00
|
|
|
}}
|
|
|
|
label={t('swap')}
|
|
|
|
tooltipRender={
|
|
|
|
isSwapsChain
|
|
|
|
? null
|
|
|
|
: (contents) => (
|
|
|
|
<Tooltip
|
|
|
|
title={t('currentlyUnavailable')}
|
|
|
|
position="bottom"
|
|
|
|
disabled={isSwapsChain}
|
|
|
|
>
|
|
|
|
{contents}
|
|
|
|
</Tooltip>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<IconButton
|
|
|
|
className="eth-overview__button"
|
|
|
|
Icon={
|
2023-04-19 23:16:49 +02:00
|
|
|
<Icon name={IconName.Diagram} color={IconColor.primaryInverse} />
|
2023-03-01 09:45:27 +01:00
|
|
|
}
|
|
|
|
label={t('portfolio')}
|
|
|
|
data-testid="home__portfolio-site"
|
|
|
|
onClick={() => {
|
|
|
|
const portfolioUrl = process.env.PORTFOLIO_URL;
|
|
|
|
global.platform.openTab({
|
|
|
|
url: `${portfolioUrl}?metamaskEntry=ext`,
|
|
|
|
});
|
|
|
|
trackEvent(
|
|
|
|
{
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Home,
|
|
|
|
event: MetaMetricsEventName.PortfolioLinkClicked,
|
2023-03-01 09:45:27 +01:00
|
|
|
properties: {
|
|
|
|
url: portfolioUrl,
|
2023-02-21 16:32:08 +01:00
|
|
|
},
|
2023-03-01 09:45:27 +01:00
|
|
|
},
|
|
|
|
{
|
2023-04-03 17:31:04 +02:00
|
|
|
contextPropsIntoEventProperties: [
|
|
|
|
MetaMetricsContextProp.PageTitle,
|
|
|
|
],
|
2023-03-01 09:45:27 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}}
|
2021-04-14 09:16:27 +02:00
|
|
|
/>
|
2023-03-01 09:45:27 +01:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
className={className}
|
|
|
|
icon={
|
|
|
|
<Identicon diameter={32} address={token.address} image={token.image} />
|
|
|
|
}
|
|
|
|
/>
|
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;
|