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-01-20 16:14:40 +01:00
|
|
|
import { HardwareKeyringTypes } from '../../../../shared/constants/hardware-wallets';
|
2022-08-16 18:39:23 +02:00
|
|
|
import { EVENT, EVENT_NAMES } from '../../../../shared/constants/metametrics';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2023-01-31 15:47:00 +01:00
|
|
|
import { ICON_NAMES } 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-01-20 16:14:40 +01:00
|
|
|
const isRemovable = keyring.type !== HardwareKeyringTypes.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({
|
2022-08-16 18:39:23 +02:00
|
|
|
event: EVENT_NAMES.EXTERNAL_LINK_CLICKED,
|
2022-08-02 23:56:02 +02:00
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
|
|
|
properties: {
|
2022-08-16 18:39:23 +02:00
|
|
|
link_type: EVENT.EXTERNAL_LINK_TYPES.ACCOUNT_TRACKER,
|
|
|
|
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-01-31 15:47:00 +01:00
|
|
|
iconName={ICON_NAMES.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({
|
2022-08-16 18:39:23 +02:00
|
|
|
event: EVENT_NAMES.APP_WINDOW_EXPANDED,
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.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-01-31 15:47:00 +01:00
|
|
|
iconName={ICON_NAMES.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({
|
2022-08-16 18:39:23 +02:00
|
|
|
event: EVENT_NAMES.NAV_ACCOUNT_DETAILS_OPENED,
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.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-01-31 15:47:00 +01:00
|
|
|
iconName={ICON_NAMES.SCAN_BARCODE}
|
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({
|
2022-08-16 18:39:23 +02:00
|
|
|
event: EVENT_NAMES.NAV_CONNECTED_SITES_OPENED,
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.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-01-31 15:47:00 +01:00
|
|
|
iconName={ICON_NAMES.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-01-31 15:47:00 +01:00
|
|
|
iconName={ICON_NAMES.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
|
|
|
};
|