import React, { useContext } from 'react'; import PropTypes from 'prop-types'; import { useHistory } from 'react-router-dom'; import { useDispatch, useSelector } from 'react-redux'; import { getAccountLink } from '@metamask/etherscan-link'; import { showModal } from '../../../store/actions'; import { CONNECTED_ROUTE } from '../../../helpers/constants/routes'; import { getURLHostName } from '../../../helpers/utils/util'; import { Menu, MenuItem } from '../../ui/menu'; import { getCurrentChainId, getCurrentKeyring, getRpcPrefsForCurrentProvider, getSelectedIdentity, } from '../../../selectors'; import { useI18nContext } from '../../../hooks/useI18nContext'; import { useNewMetricEvent } from '../../../hooks/useMetricEvent'; import { getEnvironmentType } from '../../../../app/scripts/lib/util'; import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../shared/constants/app'; import { MetaMetricsContext } from '../../../contexts/metametrics.new'; export default function AccountOptionsMenu({ anchorElement, onClose }) { const t = useI18nContext(); const dispatch = useDispatch(); const history = useHistory(); const keyring = useSelector(getCurrentKeyring); const chainId = useSelector(getCurrentChainId); const rpcPrefs = useSelector(getRpcPrefsForCurrentProvider); const selectedIdentity = useSelector(getSelectedIdentity); const { address } = selectedIdentity; const addressLink = getAccountLink(address, chainId, rpcPrefs); const { blockExplorerUrl } = rpcPrefs; const blockExplorerUrlSubTitle = getURLHostName(blockExplorerUrl); const trackEvent = useContext(MetaMetricsContext); const blockExplorerLinkClickedEvent = useNewMetricEvent({ category: 'Navigation', event: 'Clicked Block Explorer Link', properties: { link_type: 'Account Tracker', action: 'Account Options', block_explorer_domain: getURLHostName(addressLink), }, }); const isRemovable = keyring.type !== 'HD Key Tree'; return ( { blockExplorerLinkClickedEvent(); global.platform.openTab({ url: addressLink, }); onClose(); }} subtitle={ blockExplorerUrlSubTitle ? ( {blockExplorerUrlSubTitle} ) : null } iconClassName="fas fa-external-link-alt" > {rpcPrefs.blockExplorerUrl ? t('viewinExplorer', [t('blockExplorerAccountAction')]) : t('viewOnEtherscan', [t('blockExplorerAccountAction')])} {getEnvironmentType() === ENVIRONMENT_TYPE_FULLSCREEN ? null : ( { trackEvent({ event: 'Clicked Expand View', category: 'Navigation', properties: { action: 'Account Options', legacy_event: true, }, }); global.platform.openExtensionInBrowser(); onClose(); }} iconClassName="fas fa-expand-alt" > {t('expandView')} )} { dispatch(showModal({ name: 'ACCOUNT_DETAILS' })); trackEvent({ event: 'Viewed Account Details', category: 'Navigation', properties: { action: 'Account Options', legacy_event: true, }, }); onClose(); }} iconClassName="fas fa-qrcode" > {t('accountDetails')} { trackEvent({ event: 'Opened Connected Sites', category: 'Navigation', properties: { action: 'Account Options', legacy_event: true, }, }); history.push(CONNECTED_ROUTE); onClose(); }} iconClassName="account-options-menu__connected-sites" > {t('connectedSites')} {isRemovable ? ( { dispatch( showModal({ name: 'CONFIRM_REMOVE_ACCOUNT', identity: selectedIdentity, }), ); onClose(); }} iconClassName="fas fa-trash-alt" > {t('removeAccount')} ) : null} ); } AccountOptionsMenu.propTypes = { anchorElement: PropTypes.instanceOf(window.Element), onClose: PropTypes.func.isRequired, }; AccountOptionsMenu.defaultProps = { anchorElement: undefined, };