2023-07-17 18:42:58 +02:00
|
|
|
import React, { useContext, useEffect } from 'react';
|
2023-03-31 19:58:25 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
|
|
|
import { NetworkListItem } from '../network-list-item';
|
|
|
|
import {
|
|
|
|
setActiveNetwork,
|
|
|
|
showModal,
|
|
|
|
setShowTestNetworks,
|
|
|
|
setProviderType,
|
2023-04-13 18:54:03 +02:00
|
|
|
toggleNetworkMenu,
|
2023-03-31 19:58:25 +02:00
|
|
|
} from '../../../store/actions';
|
|
|
|
import { CHAIN_IDS, TEST_CHAINS } from '../../../../shared/constants/network';
|
|
|
|
import {
|
|
|
|
getShowTestNetworks,
|
|
|
|
getCurrentChainId,
|
2023-07-05 12:01:45 +02:00
|
|
|
getNonTestNetworks,
|
|
|
|
getTestNetworks,
|
2023-03-31 19:58:25 +02:00
|
|
|
} from '../../../selectors';
|
|
|
|
import ToggleButton from '../../ui/toggle-button';
|
|
|
|
import {
|
2023-06-28 17:03:50 +02:00
|
|
|
Display,
|
2023-03-31 19:58:25 +02:00
|
|
|
JustifyContent,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
2023-05-08 16:50:04 +02:00
|
|
|
import {
|
|
|
|
BUTTON_SECONDARY_SIZES,
|
|
|
|
ButtonSecondary,
|
2023-07-07 16:08:24 +02:00
|
|
|
Modal,
|
|
|
|
ModalContent,
|
|
|
|
ModalHeader,
|
|
|
|
ModalOverlay,
|
2023-07-04 17:17:05 +02:00
|
|
|
Box,
|
2023-07-18 07:02:02 +02:00
|
|
|
Text,
|
2023-05-08 16:50:04 +02:00
|
|
|
} from '../../component-library';
|
2023-03-31 19:58:25 +02:00
|
|
|
import { ADD_POPULAR_CUSTOM_NETWORK } from '../../../helpers/constants/routes';
|
|
|
|
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
|
|
|
|
import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../shared/constants/app';
|
2023-04-27 16:28:08 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
|
|
|
import {
|
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventName,
|
|
|
|
} from '../../../../shared/constants/metametrics';
|
2023-06-16 18:35:33 +02:00
|
|
|
import {
|
|
|
|
getCompletedOnboarding,
|
|
|
|
isLineaMainnetNetworkReleased,
|
|
|
|
} from '../../../ducks/metamask/metamask';
|
2023-03-31 19:58:25 +02:00
|
|
|
|
2023-06-16 18:35:33 +02:00
|
|
|
const UNREMOVABLE_CHAIN_IDS = [
|
|
|
|
CHAIN_IDS.MAINNET,
|
|
|
|
CHAIN_IDS.LINEA_MAINNET,
|
|
|
|
...TEST_CHAINS,
|
|
|
|
];
|
2023-03-31 19:58:25 +02:00
|
|
|
|
2023-04-13 18:54:03 +02:00
|
|
|
export const NetworkListMenu = ({ onClose }) => {
|
2023-03-31 19:58:25 +02:00
|
|
|
const t = useI18nContext();
|
2023-07-05 12:01:45 +02:00
|
|
|
|
|
|
|
const nonTestNetworks = useSelector(getNonTestNetworks);
|
|
|
|
const testNetworks = useSelector(getTestNetworks);
|
2023-03-31 19:58:25 +02:00
|
|
|
const showTestNetworks = useSelector(getShowTestNetworks);
|
|
|
|
const currentChainId = useSelector(getCurrentChainId);
|
2023-07-13 20:29:53 +02:00
|
|
|
|
2023-03-31 19:58:25 +02:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const history = useHistory();
|
2023-04-27 16:28:08 +02:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2023-03-31 19:58:25 +02:00
|
|
|
|
2023-07-14 15:21:53 +02:00
|
|
|
const currentlyOnTestNetwork = TEST_CHAINS.includes(currentChainId);
|
|
|
|
|
2023-03-31 19:58:25 +02:00
|
|
|
const environmentType = getEnvironmentType();
|
|
|
|
const isFullScreen = environmentType === ENVIRONMENT_TYPE_FULLSCREEN;
|
|
|
|
|
2023-06-01 23:14:38 +02:00
|
|
|
const completedOnboarding = useSelector(getCompletedOnboarding);
|
|
|
|
|
2023-06-16 18:35:33 +02:00
|
|
|
const lineaMainnetReleased = useSelector(isLineaMainnetNetworkReleased);
|
|
|
|
|
2023-07-17 18:42:58 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (currentlyOnTestNetwork) {
|
|
|
|
dispatch(setShowTestNetworks(currentlyOnTestNetwork));
|
|
|
|
}
|
|
|
|
}, [dispatch, currentlyOnTestNetwork]);
|
|
|
|
|
2023-07-05 12:01:45 +02:00
|
|
|
const generateMenuItems = (desiredNetworks) => {
|
|
|
|
return desiredNetworks.map((network, index) => {
|
|
|
|
if (!lineaMainnetReleased && network.providerType === 'linea-mainnet') {
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-13 20:29:53 +02:00
|
|
|
|
2023-07-14 15:21:53 +02:00
|
|
|
const isCurrentNetwork = currentChainId === network.chainId;
|
2023-07-05 12:01:45 +02:00
|
|
|
const canDeleteNetwork =
|
|
|
|
!isCurrentNetwork && !UNREMOVABLE_CHAIN_IDS.includes(network.chainId);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<NetworkListItem
|
|
|
|
name={network.nickname}
|
|
|
|
iconSrc={network?.rpcPrefs?.imageUrl}
|
|
|
|
key={`${network.id || network.chainId}-${index}`}
|
|
|
|
selected={isCurrentNetwork}
|
2023-07-18 15:31:02 +02:00
|
|
|
onClick={() => {
|
2023-07-05 12:01:45 +02:00
|
|
|
dispatch(toggleNetworkMenu());
|
|
|
|
if (network.providerType) {
|
|
|
|
dispatch(setProviderType(network.providerType));
|
|
|
|
} else {
|
|
|
|
dispatch(setActiveNetwork(network.id));
|
|
|
|
}
|
|
|
|
trackEvent({
|
|
|
|
event: MetaMetricsEventName.NavNetworkSwitched,
|
|
|
|
category: MetaMetricsEventCategory.Network,
|
|
|
|
properties: {
|
|
|
|
location: 'Network Menu',
|
|
|
|
chain_id: currentChainId,
|
|
|
|
from_network: currentChainId,
|
|
|
|
to_network: network.id || network.chainId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
onDeleteClick={
|
|
|
|
canDeleteNetwork
|
|
|
|
? () => {
|
|
|
|
dispatch(toggleNetworkMenu());
|
|
|
|
dispatch(
|
|
|
|
showModal({
|
|
|
|
name: 'CONFIRM_DELETE_NETWORK',
|
|
|
|
target: network.id || network.chainId,
|
|
|
|
onConfirm: () => undefined,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
2023-05-04 22:52:57 +02:00
|
|
|
|
2023-07-17 18:42:58 +02:00
|
|
|
const handleToggle = (value) => {
|
|
|
|
const shouldShowTestNetworks = !value;
|
|
|
|
dispatch(setShowTestNetworks(shouldShowTestNetworks));
|
|
|
|
if (shouldShowTestNetworks) {
|
|
|
|
trackEvent({
|
|
|
|
event: MetaMetricsEventName.TestNetworksDisplayed,
|
|
|
|
category: MetaMetricsEventCategory.Network,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-31 19:58:25 +02:00
|
|
|
return (
|
2023-07-07 16:08:24 +02:00
|
|
|
<Modal isOpen onClose={onClose}>
|
|
|
|
<ModalOverlay />
|
|
|
|
<ModalContent
|
|
|
|
className="multichain-network-list-menu-content-wrapper"
|
|
|
|
modalDialogProps={{ padding: 0 }}
|
|
|
|
>
|
|
|
|
<ModalHeader
|
|
|
|
paddingTop={4}
|
|
|
|
paddingRight={4}
|
|
|
|
paddingBottom={6}
|
|
|
|
onClose={onClose}
|
2023-03-31 19:58:25 +02:00
|
|
|
>
|
2023-07-07 16:08:24 +02:00
|
|
|
{t('networkMenuHeading')}
|
|
|
|
</ModalHeader>
|
|
|
|
<>
|
2023-07-05 12:01:45 +02:00
|
|
|
<Box className="multichain-network-list-menu">
|
2023-07-07 16:08:24 +02:00
|
|
|
{generateMenuItems(nonTestNetworks)}
|
|
|
|
</Box>
|
|
|
|
<Box
|
|
|
|
padding={4}
|
|
|
|
display={Display.Flex}
|
|
|
|
justifyContent={JustifyContent.spaceBetween}
|
|
|
|
>
|
|
|
|
<Text>{t('showTestnetNetworks')}</Text>
|
|
|
|
<ToggleButton
|
|
|
|
value={showTestNetworks}
|
2023-07-14 15:21:53 +02:00
|
|
|
disabled={currentlyOnTestNetwork}
|
2023-07-17 18:42:58 +02:00
|
|
|
onToggle={handleToggle}
|
2023-07-07 16:08:24 +02:00
|
|
|
/>
|
2023-07-05 12:01:45 +02:00
|
|
|
</Box>
|
2023-07-14 15:21:53 +02:00
|
|
|
{showTestNetworks || currentlyOnTestNetwork ? (
|
2023-07-07 16:08:24 +02:00
|
|
|
<Box className="multichain-network-list-menu">
|
|
|
|
{generateMenuItems(testNetworks)}
|
|
|
|
</Box>
|
|
|
|
) : null}
|
|
|
|
<Box padding={4}>
|
|
|
|
<ButtonSecondary
|
|
|
|
size={BUTTON_SECONDARY_SIZES.LG}
|
|
|
|
block
|
|
|
|
onClick={() => {
|
|
|
|
if (isFullScreen) {
|
|
|
|
if (completedOnboarding) {
|
|
|
|
history.push(ADD_POPULAR_CUSTOM_NETWORK);
|
|
|
|
} else {
|
|
|
|
dispatch(showModal({ name: 'ONBOARDING_ADD_NETWORK' }));
|
|
|
|
}
|
2023-06-01 23:14:38 +02:00
|
|
|
} else {
|
2023-07-07 16:08:24 +02:00
|
|
|
global.platform.openExtensionInBrowser(
|
|
|
|
ADD_POPULAR_CUSTOM_NETWORK,
|
|
|
|
);
|
2023-06-01 23:14:38 +02:00
|
|
|
}
|
2023-07-07 16:08:24 +02:00
|
|
|
dispatch(toggleNetworkMenu());
|
|
|
|
trackEvent({
|
|
|
|
event: MetaMetricsEventName.AddNetworkButtonClick,
|
|
|
|
category: MetaMetricsEventCategory.Network,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t('addNetwork')}
|
|
|
|
</ButtonSecondary>
|
|
|
|
</Box>
|
|
|
|
</>
|
|
|
|
</ModalContent>
|
|
|
|
</Modal>
|
2023-03-31 19:58:25 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
NetworkListMenu.propTypes = {
|
|
|
|
/**
|
|
|
|
* Executes when the menu should be closed
|
|
|
|
*/
|
2023-04-13 18:54:03 +02:00
|
|
|
onClose: PropTypes.func.isRequired,
|
2023-03-31 19:58:25 +02:00
|
|
|
};
|