2022-09-19 17:00:57 +02:00
|
|
|
import React, { useContext, useState } from 'react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
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 { I18nContext } from '../../../contexts/i18n';
|
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 Tooltip from '../../ui/tooltip';
|
|
|
|
import UserPreferencedCurrencyDisplay from '../user-preferenced-currency-display';
|
|
|
|
import { PRIMARY, SECONDARY } from '../../../helpers/constants/common';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
isBalanceCached,
|
|
|
|
getShouldShowFiat,
|
|
|
|
getCurrentKeyring,
|
2021-03-18 11:20:06 +01:00
|
|
|
getSwapsDefaultToken,
|
|
|
|
getIsSwapsChain,
|
2022-01-28 14:46:26 +01:00
|
|
|
getIsBuyableChain,
|
2021-04-02 00:57:00 +02:00
|
|
|
getNativeCurrencyImage,
|
2022-06-13 21:39:48 +02:00
|
|
|
getSelectedAccountCachedBalance,
|
2023-02-02 21:15:26 +01:00
|
|
|
} from '../../../selectors';
|
2021-04-14 09:16:27 +02:00
|
|
|
import { setSwapsFromToken } from '../../../ducks/swaps/swaps';
|
2021-02-04 19:15:23 +01:00
|
|
|
import IconButton from '../../ui/icon-button';
|
2021-10-28 01:54:18 +02:00
|
|
|
import { isHardwareKeyring } from '../../../helpers/utils/hardware';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2022-08-16 18:39:23 +02:00
|
|
|
import { EVENT, EVENT_NAMES } from '../../../../shared/constants/metametrics';
|
2022-06-13 21:39:48 +02:00
|
|
|
import Spinner from '../../ui/spinner';
|
2022-07-01 15:58:35 +02:00
|
|
|
import { startNewDraftTransaction } from '../../../ducks/send';
|
2023-01-18 15:47:29 +01:00
|
|
|
import { AssetType } from '../../../../shared/constants/transaction';
|
2022-09-19 17:00:57 +02:00
|
|
|
import DepositPopover from '../deposit-popover';
|
2023-01-31 15:47:43 +01:00
|
|
|
import { Icon, ICON_NAMES } 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 EthOverview = ({ className }) => {
|
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();
|
2022-09-19 17:00:57 +02:00
|
|
|
const [showDepositPopover, setShowDepositPopover] = useState(false);
|
2021-02-04 19:15:23 +01:00
|
|
|
const keyring = useSelector(getCurrentKeyring);
|
2022-02-22 16:59:31 +01:00
|
|
|
const usingHardwareWallet = isHardwareKeyring(keyring?.type);
|
2021-02-04 19:15:23 +01:00
|
|
|
const balanceIsCached = useSelector(isBalanceCached);
|
|
|
|
const showFiat = useSelector(getShouldShowFiat);
|
2022-06-13 21:39:48 +02:00
|
|
|
const balance = useSelector(getSelectedAccountCachedBalance);
|
2021-03-18 11:20:06 +01:00
|
|
|
const isSwapsChain = useSelector(getIsSwapsChain);
|
2022-01-28 14:46:26 +01:00
|
|
|
const isBuyableChain = useSelector(getIsBuyableChain);
|
2021-04-02 00:57:00 +02:00
|
|
|
const primaryTokenImage = useSelector(getNativeCurrencyImage);
|
2021-03-18 11:20:06 +01:00
|
|
|
const defaultSwapsToken = useSelector(getSwapsDefaultToken);
|
2020-05-21 19:33:48 +02:00
|
|
|
|
|
|
|
return (
|
2022-09-19 17:00:57 +02:00
|
|
|
<>
|
|
|
|
{showDepositPopover && (
|
|
|
|
<DepositPopover onClose={() => setShowDepositPopover(false)} />
|
|
|
|
)}
|
|
|
|
<WalletOverview
|
|
|
|
loading={!balance}
|
|
|
|
balance={
|
|
|
|
<Tooltip
|
|
|
|
position="top"
|
|
|
|
title={t('balanceOutdated')}
|
|
|
|
disabled={!balanceIsCached}
|
|
|
|
>
|
|
|
|
<div className="eth-overview__balance">
|
|
|
|
<div className="eth-overview__primary-container">
|
|
|
|
{balance ? (
|
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
className={classnames('eth-overview__primary-balance', {
|
|
|
|
'eth-overview__cached-balance': balanceIsCached,
|
|
|
|
})}
|
|
|
|
data-testid="eth-overview__primary-currency"
|
|
|
|
value={balance}
|
|
|
|
type={PRIMARY}
|
|
|
|
ethNumberOfDecimals={4}
|
|
|
|
hideTitle
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Spinner
|
|
|
|
color="var(--color-secondary-default)"
|
|
|
|
className="loading-overlay__spinner"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{balanceIsCached ? (
|
|
|
|
<span className="eth-overview__cached-star">*</span>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
{showFiat && balance && (
|
2022-06-13 21:39:48 +02:00
|
|
|
<UserPreferencedCurrencyDisplay
|
2022-09-19 17:00:57 +02:00
|
|
|
className={classnames({
|
|
|
|
'eth-overview__cached-secondary-balance': balanceIsCached,
|
|
|
|
'eth-overview__secondary-balance': !balanceIsCached,
|
2022-06-13 21:39:48 +02:00
|
|
|
})}
|
2022-09-19 17:00:57 +02:00
|
|
|
data-testid="eth-overview__secondary-currency"
|
2022-06-13 21:39:48 +02:00
|
|
|
value={balance}
|
2022-09-19 17:00:57 +02:00
|
|
|
type={SECONDARY}
|
2022-06-13 21:39:48 +02:00
|
|
|
ethNumberOfDecimals={4}
|
|
|
|
hideTitle
|
|
|
|
/>
|
|
|
|
)}
|
2020-05-21 19:33:48 +02:00
|
|
|
</div>
|
2022-09-19 17:00:57 +02:00
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
buttons={
|
|
|
|
<>
|
|
|
|
<IconButton
|
|
|
|
className="eth-overview__button"
|
2023-01-31 15:47:43 +01:00
|
|
|
Icon={
|
2023-02-02 21:15:26 +01:00
|
|
|
<Icon name={ICON_NAMES.CARD} color={IconColor.primaryInverse} />
|
2023-01-31 15:47:43 +01:00
|
|
|
}
|
2022-09-19 17:00:57 +02:00
|
|
|
disabled={!isBuyableChain}
|
|
|
|
label={t('buy')}
|
|
|
|
onClick={() => {
|
|
|
|
trackEvent({
|
|
|
|
event: EVENT_NAMES.NAV_BUY_BUTTON_CLICKED,
|
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
|
|
|
properties: {
|
|
|
|
location: 'Home',
|
|
|
|
text: 'Buy',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
setShowDepositPopover(true);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<IconButton
|
|
|
|
className="eth-overview__button"
|
|
|
|
data-testid="eth-overview-send"
|
2023-01-31 15:47:43 +01:00
|
|
|
Icon={
|
2023-02-02 21:15:26 +01:00
|
|
|
<Icon
|
|
|
|
name={ICON_NAMES.SEND_1}
|
|
|
|
color={IconColor.primaryInverse}
|
|
|
|
/>
|
2023-01-31 15:47:43 +01:00
|
|
|
}
|
2022-09-19 17:00:57 +02:00
|
|
|
label={t('send')}
|
|
|
|
onClick={() => {
|
2022-03-25 14:21:29 +01:00
|
|
|
trackEvent({
|
2022-09-19 17:00:57 +02:00
|
|
|
event: EVENT_NAMES.NAV_SEND_BUTTON_CLICKED,
|
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
2022-03-25 14:21:29 +01:00
|
|
|
properties: {
|
2022-08-16 18:39:23 +02:00
|
|
|
token_symbol: 'ETH',
|
2022-09-19 17:00:57 +02:00
|
|
|
location: 'Home',
|
|
|
|
text: 'Send',
|
2022-03-25 14:21:29 +01:00
|
|
|
},
|
|
|
|
});
|
2022-09-19 17:00:57 +02:00
|
|
|
dispatch(
|
2023-01-18 15:47:29 +01:00
|
|
|
startNewDraftTransaction({ type: AssetType.native }),
|
2022-09-19 17:00:57 +02:00
|
|
|
).then(() => {
|
|
|
|
history.push(SEND_ROUTE);
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<IconButton
|
|
|
|
className="eth-overview__button"
|
|
|
|
disabled={!isSwapsChain}
|
2023-01-31 15:47:43 +01:00
|
|
|
Icon={
|
|
|
|
<Icon
|
|
|
|
name={ICON_NAMES.SWAP_HORIZONTAL}
|
2023-02-02 21:15:26 +01:00
|
|
|
color={IconColor.primaryInverse}
|
2023-01-31 15:47:43 +01:00
|
|
|
/>
|
|
|
|
}
|
2022-09-19 17:00:57 +02:00
|
|
|
onClick={() => {
|
|
|
|
if (isSwapsChain) {
|
|
|
|
trackEvent({
|
|
|
|
event: EVENT_NAMES.NAV_SWAP_BUTTON_CLICKED,
|
|
|
|
category: EVENT.CATEGORIES.SWAPS,
|
|
|
|
properties: {
|
|
|
|
token_symbol: 'ETH',
|
|
|
|
location: EVENT.SOURCE.SWAPS.MAIN_VIEW,
|
|
|
|
text: 'Swap',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
dispatch(setSwapsFromToken(defaultSwapsToken));
|
|
|
|
if (usingHardwareWallet) {
|
|
|
|
global.platform.openExtensionInBrowser(BUILD_QUOTE_ROUTE);
|
|
|
|
} else {
|
|
|
|
history.push(BUILD_QUOTE_ROUTE);
|
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2022-09-19 17:00:57 +02:00
|
|
|
}}
|
|
|
|
label={t('swap')}
|
|
|
|
tooltipRender={
|
|
|
|
isSwapsChain
|
|
|
|
? null
|
|
|
|
: (contents) => (
|
|
|
|
<Tooltip
|
|
|
|
title={t('currentlyUnavailable')}
|
|
|
|
position="bottom"
|
|
|
|
>
|
|
|
|
{contents}
|
|
|
|
</Tooltip>
|
|
|
|
)
|
2021-04-14 09:16:27 +02:00
|
|
|
}
|
2022-09-19 17:00:57 +02:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
className={className}
|
|
|
|
icon={<Identicon diameter={32} image={primaryTokenImage} imageBorder />}
|
|
|
|
/>
|
|
|
|
</>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2020-05-21 19:33:48 +02:00
|
|
|
|
|
|
|
EthOverview.propTypes = {
|
2020-06-01 19:54:32 +02:00
|
|
|
className: PropTypes.string,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-21 19:33:48 +02:00
|
|
|
|
2020-06-01 19:54:32 +02:00
|
|
|
EthOverview.defaultProps = {
|
|
|
|
className: undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-21 19:33:48 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export default EthOverview;
|