1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 11:01:41 +01:00
metamask-extension/ui/components/app/wallet-overview/token-overview.js

159 lines
4.7 KiB
JavaScript
Raw Normal View History

import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import Identicon from '../../ui/identicon';
import Tooltip from '../../ui/tooltip';
import CurrencyDisplay from '../../ui/currency-display';
import { I18nContext } from '../../../contexts/i18n';
import { isHardwareKeyring } from '../../../helpers/utils/hardware';
2020-11-03 00:41:28 +01:00
import {
SEND_ROUTE,
BUILD_QUOTE_ROUTE,
} from '../../../helpers/constants/routes';
2020-11-03 00:41:28 +01:00
import {
useMetricEvent,
useNewMetricEvent,
} from '../../../hooks/useMetricEvent';
import { useTokenTracker } from '../../../hooks/useTokenTracker';
import { useTokenFiatAmount } from '../../../hooks/useTokenFiatAmount';
import { ASSET_TYPES, updateSendAsset } from '../../../ducks/send';
import { setSwapsFromToken } from '../../../ducks/swaps/swaps';
2020-11-03 00:41:28 +01:00
import {
getCurrentKeyring,
getIsSwapsChain,
} from '../../../selectors/selectors';
2020-10-06 20:28:38 +02: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
import IconButton from '../../ui/icon-button';
import WalletOverview from './wallet-overview';
const TokenOverview = ({ className, token }) => {
const dispatch = useDispatch();
const t = useContext(I18nContext);
const sendTokenEvent = useMetricEvent({
eventOpts: {
category: 'Navigation',
action: 'Home',
name: 'Clicked Send: Token',
},
});
const history = useHistory();
const keyring = useSelector(getCurrentKeyring);
const usingHardwareWallet = isHardwareKeyring(keyring.type);
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,
);
const isSwapsChain = useSelector(getIsSwapsChain);
2020-11-03 00:41:28 +01:00
const enteredSwapsEvent = useNewMetricEvent({
event: 'Swaps Opened',
properties: { source: 'Token View', active_currency: token.symbol },
category: 'swaps',
});
return (
<WalletOverview
2020-11-03 00:41:28 +01:00
balance={
<div className="token-overview__balance">
<CurrencyDisplay
className="token-overview__primary-balance"
displayValue={balanceToRender}
suffix={token.symbol}
/>
2020-11-03 00:41:28 +01:00
{formattedFiatBalance ? (
<CurrencyDisplay
className="token-overview__secondary-balance"
displayValue={formattedFiatBalance}
hideLabel
/>
) : null}
</div>
2020-11-03 00:41:28 +01:00
}
buttons={
2020-10-06 20:28:38 +02:00
<>
<IconButton
className="token-overview__button"
onClick={() => {
sendTokenEvent();
dispatch(
updateSendAsset({
type: ASSET_TYPES.TOKEN,
details: token,
}),
).then(() => {
history.push(SEND_ROUTE);
});
2020-10-06 20:28:38 +02:00
}}
Icon={SendIcon}
label={t('send')}
data-testid="eth-overview-send"
disabled={token.isERC721}
2020-10-06 20:28:38 +02:00
/>
<IconButton
className="token-overview__button"
disabled={!isSwapsChain}
Icon={SwapIcon}
onClick={() => {
if (isSwapsChain) {
enteredSwapsEvent();
dispatch(
setSwapsFromToken({
...token,
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
}
}
}}
label={t('swap')}
tooltipRender={(contents) => (
<Tooltip
title={t('currentlyUnavailable')}
position="bottom"
disabled={isSwapsChain}
>
{contents}
</Tooltip>
)}
/>
2020-10-06 20:28:38 +02:00
</>
2020-11-03 00:41:28 +01:00
}
className={className}
2020-11-03 00:41:28 +01:00
icon={
<Identicon diameter={32} address={token.address} image={token.image} />
2020-11-03 00:41:28 +01:00
}
/>
);
};
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;