1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 18:41:38 +01:00
metamask-extension/ui/components/app/menu-bar/account-options-menu.js

162 lines
4.7 KiB
JavaScript
Raw Normal View History

import React 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';
2020-11-03 00:41:28 +01:00
import {
getCurrentChainId,
2020-11-03 00:41:28 +01:00
getCurrentKeyring,
getRpcPrefsForCurrentProvider,
getSelectedIdentity,
} from '../../../selectors';
import { useI18nContext } from '../../../hooks/useI18nContext';
import {
useMetricEvent,
useNewMetricEvent,
} from '../../../hooks/useMetricEvent';
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../shared/constants/app';
2020-11-03 00:41:28 +01:00
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 openFullscreenEvent = useMetricEvent({
eventOpts: {
category: 'Navigation',
action: 'Account Options',
name: 'Clicked Expand View',
},
});
const viewAccountDetailsEvent = useMetricEvent({
eventOpts: {
category: 'Navigation',
action: 'Account Options',
name: 'Viewed Account Details',
},
});
const openConnectedSitesEvent = useMetricEvent({
eventOpts: {
category: 'Navigation',
action: 'Account Options',
name: 'Opened Connected Sites',
},
});
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 (
<Menu
anchorElement={anchorElement}
className="account-options-menu"
onHide={onClose}
>
2020-11-03 00:41:28 +01:00
{getEnvironmentType() === ENVIRONMENT_TYPE_FULLSCREEN ? null : (
<MenuItem
onClick={() => {
openFullscreenEvent();
global.platform.openExtensionInBrowser();
onClose();
2020-11-03 00:41:28 +01:00
}}
iconClassName="fas fa-expand-alt"
>
{t('expandView')}
</MenuItem>
)}
<MenuItem
data-testid="account-options-menu__account-details"
onClick={() => {
dispatch(showModal({ name: 'ACCOUNT_DETAILS' }));
viewAccountDetailsEvent();
onClose();
}}
iconClassName="fas fa-qrcode"
>
2020-11-03 00:41:28 +01:00
{t('accountDetails')}
</MenuItem>
<MenuItem
onClick={() => {
blockExplorerLinkClickedEvent();
2020-11-03 00:41:28 +01:00
global.platform.openTab({
url: addressLink,
});
onClose();
}}
subtitle={
blockExplorerUrlSubTitle ? (
2020-11-03 00:41:28 +01:00
<span className="account-options-menu__explorer-origin">
{blockExplorerUrlSubTitle}
2020-11-03 00:41:28 +01:00
</span>
) : null
}
iconClassName="fas fa-external-link-alt"
>
2020-11-03 00:41:28 +01:00
{rpcPrefs.blockExplorerUrl ? t('viewinExplorer') : t('viewOnEtherscan')}
</MenuItem>
<MenuItem
data-testid="account-options-menu__connected-sites"
onClick={() => {
openConnectedSitesEvent();
history.push(CONNECTED_ROUTE);
onClose();
}}
iconClassName="account-options-menu__connected-sites"
>
2020-11-03 00:41:28 +01:00
{t('connectedSites')}
</MenuItem>
2020-11-03 00:41:28 +01:00
{isRemovable ? (
<MenuItem
data-testid="account-options-menu__remove-account"
onClick={() => {
dispatch(
showModal({
name: 'CONFIRM_REMOVE_ACCOUNT',
identity: selectedIdentity,
}),
);
onClose();
2020-11-03 00:41:28 +01:00
}}
iconClassName="fas fa-trash-alt"
>
{t('removeAccount')}
</MenuItem>
) : null}
</Menu>
);
}
AccountOptionsMenu.propTypes = {
anchorElement: PropTypes.instanceOf(window.Element),
onClose: PropTypes.func.isRequired,
};
AccountOptionsMenu.defaultProps = {
anchorElement: undefined,
};