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, IconName } 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 (
setAssetOptionsOpen(true)} ariaLabel={t('assetOptions')} iconName={IconName.MoreVertical} color={Color.textDefault} /> {assetOptionsOpen ? ( setAssetOptionsOpen(false)} > { setAssetOptionsOpen(false); onViewAccountDetails(); }} > {t('accountDetails')} {t( blockExplorerLinkText.firstPart, blockExplorerLinkText.secondPart === '' ? null : [t('blockExplorerAssetAction')], )} {isNativeAsset ? null : ( { setAssetOptionsOpen(false); onRemove(); }} > {t('hideTokenSymbol', [tokenSymbol])} )} {isNativeAsset ? null : ( { setAssetOptionsOpen(false); onViewTokenDetails(); }} > {t('tokenDetails')} )} ) : null}
); }; const isNotFunc = (p) => { return typeof p !== 'function'; }; AssetOptions.propTypes = { isNativeAsset: PropTypes.bool, onClickBlockExplorer: PropTypes.func.isRequired, onViewAccountDetails: PropTypes.func.isRequired, onRemove: (props) => { if (props.isNativeAsset === false && isNotFunc(props.onRemove)) { throw new Error( 'When isNativeAsset is true, onRemove is a required prop', ); } }, onViewTokenDetails: (props) => { if (props.isNativeAsset === false && isNotFunc(props.onViewTokenDetails)) { throw new Error( 'When isNativeAsset is true, onViewTokenDetails is a required prop', ); } }, tokenSymbol: (props) => { if ( props.isNativeAsset === false && typeof props.tokenSymbol !== 'string' ) { throw new Error( 'When isNativeAsset is true, tokenSymbol is a required prop', ); } }, }; export default AssetOptions;