1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 15:50:28 +01:00
metamask-extension/ui/pages/asset/components/asset-options.js

142 lines
4.1 KiB
JavaScript
Raw Normal View History

import React, { useContext, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import { useHistory } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { I18nContext } from '../../../contexts/i18n';
import { Menu, MenuItem } from '../../../components/ui/menu';
import { getBlockExplorerLinkText } from '../../../selectors';
import { NETWORKS_ROUTE } from '../../../helpers/constants/routes';
import { ButtonIcon, ICON_NAMES } from '../../../components/component-library';
import { Color } from '../../../helpers/constants/design-system';
const AssetOptions = ({
onRemove,
onClickBlockExplorer,
onViewAccountDetails,
onViewTokenDetails,
tokenSymbol,
isNativeAsset,
}) => {
const t = useContext(I18nContext);
const [assetOptionsOpen, setAssetOptionsOpen] = useState(false);
const history = useHistory();
const blockExplorerLinkText = useSelector(getBlockExplorerLinkText);
const ref = useRef(false);
const routeToAddBlockExplorerUrl = () => {
history.push(`${NETWORKS_ROUTE}#blockExplorerUrl`);
};
const openBlockExplorer = () => {
setAssetOptionsOpen(false);
onClickBlockExplorer();
};
return (
<div ref={ref}>
<ButtonIcon
className="asset-options__button"
data-testid="asset-options__button"
onClick={() => setAssetOptionsOpen(true)}
ariaLabel={t('assetOptions')}
iconName={ICON_NAMES.MORE_VERTICAL}
color={Color.textDefault}
/>
{assetOptionsOpen ? (
<Menu
anchorElement={ref.current}
onHide={() => setAssetOptionsOpen(false)}
>
<MenuItem
iconName={ICON_NAMES.SCAN_BARCODE}
data-testid="asset-options__account-details"
onClick={() => {
setAssetOptionsOpen(false);
onViewAccountDetails();
}}
>
{t('accountDetails')}
</MenuItem>
<MenuItem
iconName={ICON_NAMES.EXPORT}
data-testid="asset-options__etherscan"
onClick={
blockExplorerLinkText.firstPart === 'addBlockExplorer'
? routeToAddBlockExplorerUrl
: openBlockExplorer
}
>
{t(
blockExplorerLinkText.firstPart,
blockExplorerLinkText.secondPart === ''
? null
: [t('blockExplorerAssetAction')],
)}
</MenuItem>
{isNativeAsset ? null : (
<MenuItem
iconName={ICON_NAMES.TRASH}
data-testid="asset-options__hide"
onClick={() => {
setAssetOptionsOpen(false);
onRemove();
}}
>
{t('hideTokenSymbol', [tokenSymbol])}
</MenuItem>
)}
{isNativeAsset ? null : (
<MenuItem
iconName={ICON_NAMES.INFO}
data-testid="asset-options__token-details"
onClick={() => {
setAssetOptionsOpen(false);
onViewTokenDetails();
}}
>
{t('tokenDetails')}
</MenuItem>
)}
</Menu>
) : null}
</div>
);
};
2022-06-08 23:21:53 +02:00
const isNotFunc = (p) => {
2022-06-08 01:04:59 +02:00
return typeof p !== 'function';
};
AssetOptions.propTypes = {
isNativeAsset: PropTypes.bool,
onClickBlockExplorer: PropTypes.func.isRequired,
onViewAccountDetails: PropTypes.func.isRequired,
2022-06-08 01:04:59 +02:00
onRemove: (props) => {
2022-06-08 23:21:53 +02:00
if (props.isNativeAsset === false && isNotFunc(props.onRemove)) {
2022-06-08 01:04:59 +02:00
throw new Error(
'When isNativeAsset is true, onRemove is a required prop',
);
}
},
2022-06-08 01:04:59 +02:00
onViewTokenDetails: (props) => {
2022-06-08 23:21:53 +02:00
if (props.isNativeAsset === false && isNotFunc(props.onViewTokenDetails)) {
2022-06-08 01:04:59 +02:00
throw new Error(
'When isNativeAsset is true, onViewTokenDetails is a required prop',
);
}
},
2022-06-08 01:04:59 +02:00
tokenSymbol: (props) => {
if (
props.isNativeAsset === false &&
typeof props.tokenSymbol !== 'string'
) {
throw new Error(
'When isNativeAsset is true, tokenSymbol is a required prop',
);
}
2022-06-08 01:04:59 +02:00
},
};
export default AssetOptions;