2022-03-14 19:12:38 +01:00
|
|
|
import React, { useContext } from 'react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2021-05-19 16:51:47 +02:00
|
|
|
import { getAccountLink } from '@metamask/etherscan-link';
|
2020-05-27 17:31:53 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import { showModal } from '../../../store/actions';
|
2022-08-02 23:56:02 +02:00
|
|
|
import {
|
|
|
|
CONNECTED_ROUTE,
|
|
|
|
NETWORKS_ROUTE,
|
|
|
|
} from '../../../helpers/constants/routes';
|
2021-08-25 00:28:16 +02:00
|
|
|
import { getURLHostName } from '../../../helpers/utils/util';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { Menu, MenuItem } from '../../ui/menu';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
2022-08-02 23:56:02 +02:00
|
|
|
getBlockExplorerLinkText,
|
2021-03-10 18:21:52 +01:00
|
|
|
getCurrentChainId,
|
2020-11-03 00:41:28 +01:00
|
|
|
getCurrentKeyring,
|
|
|
|
getRpcPrefsForCurrentProvider,
|
|
|
|
getSelectedIdentity,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../selectors';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
|
|
|
|
import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../shared/constants/app';
|
2022-04-22 18:09:10 +02:00
|
|
|
import { EVENT } from '../../../../shared/constants/metametrics';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2020-05-27 17:31:53 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function AccountOptionsMenu({ anchorElement, onClose }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const t = useI18nContext();
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const history = useHistory();
|
2021-05-19 16:51:47 +02:00
|
|
|
|
|
|
|
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);
|
2021-07-16 20:52:41 +02:00
|
|
|
const { blockExplorerUrl } = rpcPrefs;
|
2021-08-25 00:28:16 +02:00
|
|
|
const blockExplorerUrlSubTitle = getURLHostName(blockExplorerUrl);
|
2022-03-14 19:12:38 +01:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2022-08-02 23:56:02 +02:00
|
|
|
const blockExplorerLinkText = useSelector(getBlockExplorerLinkText);
|
2020-05-27 17:31:53 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const isRemovable = keyring.type !== 'HD Key Tree';
|
2020-05-27 17:31:53 +02:00
|
|
|
|
2022-08-02 23:56:02 +02:00
|
|
|
const routeToAddBlockExplorerUrl = () => {
|
|
|
|
history.push(`${NETWORKS_ROUTE}#blockExplorerUrl`);
|
|
|
|
};
|
|
|
|
|
|
|
|
const openBlockExplorer = () => {
|
|
|
|
trackEvent({
|
|
|
|
event: 'Clicked Block Explorer Link',
|
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
|
|
|
properties: {
|
|
|
|
link_type: 'Account Tracker',
|
|
|
|
action: 'Account Options',
|
|
|
|
block_explorer_domain: getURLHostName(addressLink),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
global.platform.openTab({
|
|
|
|
url: addressLink,
|
|
|
|
});
|
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
2020-05-27 17:31:53 +02:00
|
|
|
return (
|
|
|
|
<Menu
|
|
|
|
anchorElement={anchorElement}
|
|
|
|
className="account-options-menu"
|
|
|
|
onHide={onClose}
|
|
|
|
>
|
2021-10-18 16:57:59 +02:00
|
|
|
<MenuItem
|
2022-08-02 23:56:02 +02:00
|
|
|
onClick={
|
|
|
|
blockExplorerLinkText.firstPart === 'addBlockExplorer'
|
|
|
|
? routeToAddBlockExplorerUrl
|
|
|
|
: openBlockExplorer
|
|
|
|
}
|
2021-10-18 16:57:59 +02:00
|
|
|
subtitle={
|
|
|
|
blockExplorerUrlSubTitle ? (
|
|
|
|
<span className="account-options-menu__explorer-origin">
|
|
|
|
{blockExplorerUrlSubTitle}
|
|
|
|
</span>
|
|
|
|
) : null
|
|
|
|
}
|
|
|
|
iconClassName="fas fa-external-link-alt"
|
|
|
|
>
|
2022-08-02 23:56:02 +02:00
|
|
|
{t(
|
|
|
|
blockExplorerLinkText.firstPart,
|
|
|
|
blockExplorerLinkText.secondPart === ''
|
|
|
|
? null
|
|
|
|
: [t(blockExplorerLinkText.secondPart)],
|
|
|
|
)}
|
2021-10-18 16:57:59 +02:00
|
|
|
</MenuItem>
|
2020-11-03 00:41:28 +01:00
|
|
|
{getEnvironmentType() === ENVIRONMENT_TYPE_FULLSCREEN ? null : (
|
|
|
|
<MenuItem
|
|
|
|
onClick={() => {
|
2022-03-14 19:12:38 +01:00
|
|
|
trackEvent({
|
|
|
|
event: 'Clicked Expand View',
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
|
|
|
action: 'Account Options',
|
|
|
|
legacy_event: true,
|
|
|
|
},
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
global.platform.openExtensionInBrowser();
|
|
|
|
onClose();
|
2020-11-03 00:41:28 +01:00
|
|
|
}}
|
|
|
|
iconClassName="fas fa-expand-alt"
|
|
|
|
>
|
|
|
|
{t('expandView')}
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
2020-05-27 17:31:53 +02:00
|
|
|
<MenuItem
|
2020-05-27 22:28:33 +02:00
|
|
|
data-testid="account-options-menu__account-details"
|
2020-05-27 17:31:53 +02:00
|
|
|
onClick={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(showModal({ name: 'ACCOUNT_DETAILS' }));
|
2022-03-14 19:12:38 +01:00
|
|
|
trackEvent({
|
|
|
|
event: 'Viewed Account Details',
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
|
|
|
action: 'Account Options',
|
|
|
|
legacy_event: true,
|
|
|
|
},
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
onClose();
|
2020-05-27 17:31:53 +02:00
|
|
|
}}
|
|
|
|
iconClassName="fas fa-qrcode"
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{t('accountDetails')}
|
2020-05-27 17:31:53 +02:00
|
|
|
</MenuItem>
|
|
|
|
<MenuItem
|
2020-05-27 22:28:33 +02:00
|
|
|
data-testid="account-options-menu__connected-sites"
|
2020-05-27 17:31:53 +02:00
|
|
|
onClick={() => {
|
2022-03-14 19:12:38 +01:00
|
|
|
trackEvent({
|
|
|
|
event: 'Opened Connected Sites',
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
|
|
|
action: 'Account Options',
|
|
|
|
legacy_event: true,
|
|
|
|
},
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(CONNECTED_ROUTE);
|
|
|
|
onClose();
|
2020-05-27 17:31:53 +02:00
|
|
|
}}
|
2022-03-23 16:24:27 +01:00
|
|
|
iconClassName="fa fa-bullseye"
|
2020-05-27 17:31:53 +02:00
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{t('connectedSites')}
|
2020-05-27 17:31:53 +02:00
|
|
|
</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,
|
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
onClose();
|
2020-11-03 00:41:28 +01:00
|
|
|
}}
|
|
|
|
iconClassName="fas fa-trash-alt"
|
|
|
|
>
|
|
|
|
{t('removeAccount')}
|
|
|
|
</MenuItem>
|
|
|
|
) : null}
|
2020-05-27 17:31:53 +02:00
|
|
|
</Menu>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-27 17:31:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AccountOptionsMenu.propTypes = {
|
|
|
|
anchorElement: PropTypes.instanceOf(window.Element),
|
|
|
|
onClose: PropTypes.func.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-27 17:31:53 +02:00
|
|
|
|
|
|
|
AccountOptionsMenu.defaultProps = {
|
|
|
|
anchorElement: undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|