mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
This reverts commit f09ab8889148c406551dea1643966e3331fde4aa, reversing changes made to effc761e0ee4ea7ffb77f275b5ed650a7098d6f8. This is being temporarily reverted to make it easier to release an urgent fix for v10.15.1.
180 lines
5.6 KiB
JavaScript
180 lines
5.6 KiB
JavaScript
import React, { useContext, useEffect } 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';
|
|
import {
|
|
SEND_ROUTE,
|
|
BUILD_QUOTE_ROUTE,
|
|
} from '../../../helpers/constants/routes';
|
|
import { useTokenTracker } from '../../../hooks/useTokenTracker';
|
|
import { useTokenFiatAmount } from '../../../hooks/useTokenFiatAmount';
|
|
import { updateSendAsset } from '../../../ducks/send';
|
|
import { setSwapsFromToken } from '../../../ducks/swaps/swaps';
|
|
import {
|
|
getCurrentKeyring,
|
|
getIsSwapsChain,
|
|
} from '../../../selectors/selectors';
|
|
|
|
import SwapIcon from '../../ui/icon/swap-icon.component';
|
|
import SendIcon from '../../ui/icon/overview-send-icon.component';
|
|
|
|
import IconButton from '../../ui/icon-button';
|
|
import { INVALID_ASSET_TYPE } from '../../../helpers/constants/error-keys';
|
|
import { showModal } from '../../../store/actions';
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
|
import { EVENT } from '../../../../shared/constants/metametrics';
|
|
import { ASSET_TYPES } from '../../../../shared/constants/transaction';
|
|
import WalletOverview from './wallet-overview';
|
|
|
|
const TokenOverview = ({ className, token }) => {
|
|
const dispatch = useDispatch();
|
|
const t = useContext(I18nContext);
|
|
const trackEvent = useContext(MetaMetricsContext);
|
|
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;
|
|
const formattedFiatBalance = useTokenFiatAmount(
|
|
token.address,
|
|
balanceToRender,
|
|
token.symbol,
|
|
);
|
|
const isSwapsChain = useSelector(getIsSwapsChain);
|
|
|
|
useEffect(() => {
|
|
if (token.isERC721 && process.env.COLLECTIBLES_V1) {
|
|
dispatch(
|
|
showModal({
|
|
name: 'CONVERT_TOKEN_TO_NFT',
|
|
tokenAddress: token.address,
|
|
}),
|
|
);
|
|
}
|
|
}, [token.isERC721, token.address, dispatch]);
|
|
|
|
return (
|
|
<WalletOverview
|
|
balance={
|
|
<div className="token-overview__balance">
|
|
<CurrencyDisplay
|
|
className="token-overview__primary-balance"
|
|
displayValue={balanceToRender}
|
|
suffix={token.symbol}
|
|
/>
|
|
{formattedFiatBalance ? (
|
|
<CurrencyDisplay
|
|
className="token-overview__secondary-balance"
|
|
displayValue={formattedFiatBalance}
|
|
hideLabel
|
|
/>
|
|
) : null}
|
|
</div>
|
|
}
|
|
buttons={
|
|
<>
|
|
<IconButton
|
|
className="token-overview__button"
|
|
onClick={async () => {
|
|
trackEvent({
|
|
event: 'Clicked Send: Token',
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
|
properties: {
|
|
action: 'Home',
|
|
legacy_event: true,
|
|
},
|
|
});
|
|
try {
|
|
await dispatch(
|
|
updateSendAsset({
|
|
type: ASSET_TYPES.TOKEN,
|
|
details: token,
|
|
}),
|
|
);
|
|
history.push(SEND_ROUTE);
|
|
} catch (err) {
|
|
if (!err.message.includes(INVALID_ASSET_TYPE)) {
|
|
throw err;
|
|
}
|
|
}
|
|
}}
|
|
Icon={SendIcon}
|
|
label={t('send')}
|
|
data-testid="eth-overview-send"
|
|
disabled={token.isERC721}
|
|
/>
|
|
<IconButton
|
|
className="token-overview__button"
|
|
disabled={!isSwapsChain}
|
|
Icon={SwapIcon}
|
|
onClick={() => {
|
|
if (isSwapsChain) {
|
|
trackEvent({
|
|
event: 'Swaps Opened',
|
|
category: EVENT.CATEGORIES.SWAPS,
|
|
properties: {
|
|
source: 'Token View',
|
|
active_currency: token.symbol,
|
|
},
|
|
});
|
|
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);
|
|
}
|
|
}
|
|
}}
|
|
label={t('swap')}
|
|
tooltipRender={(contents) => (
|
|
<Tooltip
|
|
title={t('currentlyUnavailable')}
|
|
position="bottom"
|
|
disabled={isSwapsChain}
|
|
>
|
|
{contents}
|
|
</Tooltip>
|
|
)}
|
|
/>
|
|
</>
|
|
}
|
|
className={className}
|
|
icon={
|
|
<Identicon diameter={32} address={token.address} image={token.image} />
|
|
}
|
|
/>
|
|
);
|
|
};
|
|
|
|
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;
|