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';
|
2023-03-21 15:43:22 +01:00
|
|
|
import { KeyringType } from '../../../../shared/constants/keyring';
|
2023-04-03 17:31:04 +02:00
|
|
|
import {
|
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventLinkType,
|
|
|
|
MetaMetricsEventName,
|
|
|
|
} from '../../../../shared/constants/metametrics';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2023-04-19 23:16:49 +02:00
|
|
|
import { IconName } from '../../component-library';
|
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
|
|
|
|
2023-03-21 15:43:22 +01:00
|
|
|
const isRemovable = keyring.type !== KeyringType.hdKeyTree;
|
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({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.ExternalLinkClicked,
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
2022-08-02 23:56:02 +02:00
|
|
|
properties: {
|
2023-04-03 17:31:04 +02:00
|
|
|
link_type: MetaMetricsEventLinkType.AccountTracker,
|
2022-08-16 18:39:23 +02:00
|
|
|
location: 'Account Options',
|
|
|
|
url_domain: getURLHostName(addressLink),
|
2022-08-02 23:56:02 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
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
|
|
|
|
}
|
2023-04-19 23:16:49 +02:00
|
|
|
iconName={IconName.Export}
|
2021-10-18 16:57:59 +02:00
|
|
|
>
|
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({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.AppWindowExpanded,
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
2022-08-16 18:39:23 +02:00
|
|
|
location: 'Account Options',
|
2022-03-14 19:12:38 +01:00
|
|
|
},
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
global.platform.openExtensionInBrowser();
|
|
|
|
onClose();
|
2020-11-03 00:41:28 +01:00
|
|
|
}}
|
2023-04-19 23:16:49 +02:00
|
|
|
iconName={IconName.Expand}
|
2020-11-03 00:41:28 +01:00
|
|
|
>
|
|
|
|
{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({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.NavAccountDetailsOpened,
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
2022-08-16 18:39:23 +02:00
|
|
|
location: 'Account Options',
|
2022-03-14 19:12:38 +01:00
|
|
|
},
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
onClose();
|
2020-05-27 17:31:53 +02:00
|
|
|
}}
|
2023-04-19 23:16:49 +02:00
|
|
|
iconName={IconName.ScanBarcode}
|
2020-05-27 17:31:53 +02:00
|
|
|
>
|
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({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.NavConnectedSitesOpened,
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
2022-08-16 18:39:23 +02:00
|
|
|
location: 'Account Options',
|
2022-03-14 19:12:38 +01:00
|
|
|
},
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(CONNECTED_ROUTE);
|
|
|
|
onClose();
|
2020-05-27 17:31:53 +02:00
|
|
|
}}
|
2023-04-19 23:16:49 +02:00
|
|
|
iconName={IconName.Connect}
|
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
|
|
|
}}
|
2023-04-19 23:16:49 +02:00
|
|
|
iconName={IconName.Trash}
|
2020-11-03 00:41:28 +01:00
|
|
|
>
|
|
|
|
{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
|
|
|
};
|