1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-01 21:57:06 +01:00
metamask-extension/ui/components/multichain/network-list-menu/network-list-menu.js

200 lines
7.1 KiB
JavaScript
Raw Normal View History

import React, { useContext, useEffect, useRef } 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 Popover from '../../ui/popover/popover.component';
import { NetworkListItem } from '../network-list-item';
import {
setActiveNetwork,
showModal,
setShowTestNetworks,
setProviderType,
toggleNetworkMenu,
upsertNetworkConfiguration,
2023-03-31 19:58:25 +02:00
} from '../../../store/actions';
import { CHAIN_IDS, TEST_CHAINS } from '../../../../shared/constants/network';
import {
getShowTestNetworks,
getAllEnabledNetworks,
2023-03-31 19:58:25 +02:00
getCurrentChainId,
getNetworkConfigurations,
2023-03-31 19:58:25 +02:00
} from '../../../selectors';
import Box from '../../ui/box/box';
import ToggleButton from '../../ui/toggle-button';
import {
DISPLAY,
JustifyContent,
} from '../../../helpers/constants/design-system';
import {
BUTTON_SECONDARY_SIZES,
ButtonSecondary,
Text,
} 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,
MetaMetricsNetworkEventSource,
2023-04-27 16:28:08 +02:00
} from '../../../../shared/constants/metametrics';
2023-03-31 19:58:25 +02:00
const UNREMOVABLE_CHAIN_IDS = [CHAIN_IDS.MAINNET, ...TEST_CHAINS];
export const NetworkListMenu = ({ onClose }) => {
2023-03-31 19:58:25 +02:00
const t = useI18nContext();
const networks = useSelector(getAllEnabledNetworks);
2023-03-31 19:58:25 +02:00
const showTestNetworks = useSelector(getShowTestNetworks);
const networkConfigurations = useSelector(getNetworkConfigurations);
2023-03-31 19:58:25 +02:00
const currentChainId = useSelector(getCurrentChainId);
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
const environmentType = getEnvironmentType();
const isFullScreen = environmentType === ENVIRONMENT_TYPE_FULLSCREEN;
const showTestNetworksRef = useRef(showTestNetworks);
const networkListRef = useRef(null);
useEffect(() => {
if (showTestNetworks && !showTestNetworksRef.current) {
// Scroll to the bottom of the list
networkListRef.current.lastChild.scrollIntoView();
}
showTestNetworksRef.current = showTestNetworks;
}, [showTestNetworks, showTestNetworksRef]);
2023-03-31 19:58:25 +02:00
return (
<Popover
contentClassName="multichain-network-list-menu-content-wrapper"
onClose={onClose}
centerTitle
title={t('networkMenuHeading')}
>
2023-03-31 19:58:25 +02:00
<>
<Box className="multichain-network-list-menu" ref={networkListRef}>
2023-03-31 19:58:25 +02:00
{networks.map((network) => {
const isCurrentNetwork = currentChainId === network.chainId;
const canDeleteNetwork =
!isCurrentNetwork &&
!UNREMOVABLE_CHAIN_IDS.includes(network.chainId);
return (
<NetworkListItem
name={network.nickname}
iconSrc={network?.rpcPrefs?.imageUrl}
key={network.id || network.chainId}
selected={isCurrentNetwork}
onClick={async () => {
dispatch(toggleNetworkMenu());
2023-03-31 19:58:25 +02:00
if (network.providerType) {
dispatch(setProviderType(network.providerType));
} else {
// Linea needs to be added as a custom network because
// it is not yet supported by Infura. The following lazily
// adds Linea to the custom network configurations object
let networkId = network.id;
if (network.chainId === CHAIN_IDS.LINEA_TESTNET) {
const lineaNetworkConfiguration = Object.values(
networkConfigurations,
).find(
({ chainId }) => chainId === CHAIN_IDS.LINEA_TESTNET,
);
if (lineaNetworkConfiguration) {
networkId = lineaNetworkConfiguration.id;
} else {
networkId = await dispatch(
upsertNetworkConfiguration(network, {
setActive: true,
source:
MetaMetricsNetworkEventSource.CustomNetworkForm,
}),
);
}
}
dispatch(setActiveNetwork(networkId));
2023-03-31 19:58:25 +02:00
}
2023-04-27 16:28:08 +02:00
trackEvent({
event: MetaMetricsEventName.NavNetworkSwitched,
category: MetaMetricsEventCategory.Network,
properties: {
location: 'Network Menu',
chain_id: currentChainId,
from_network: currentChainId,
to_network: network.id || network.chainId,
},
});
2023-03-31 19:58:25 +02:00
}}
onDeleteClick={
canDeleteNetwork
? () => {
dispatch(toggleNetworkMenu());
2023-03-31 19:58:25 +02:00
dispatch(
showModal({
name: 'CONFIRM_DELETE_NETWORK',
target: network.id || network.chainId,
onConfirm: () => undefined,
}),
);
}
: null
}
/>
);
})}
</Box>
<Box
padding={4}
display={DISPLAY.FLEX}
justifyContent={JustifyContent.spaceBetween}
>
<Text>{t('showTestnetNetworks')}</Text>
<ToggleButton
value={showTestNetworks}
2023-04-27 16:28:08 +02:00
onToggle={(value) => {
const shouldShowTestNetworks = !value;
dispatch(setShowTestNetworks(shouldShowTestNetworks));
if (shouldShowTestNetworks) {
trackEvent({
event: MetaMetricsEventName.TestNetworksDisplayed,
category: MetaMetricsEventCategory.Network,
});
}
}}
2023-03-31 19:58:25 +02:00
/>
</Box>
<Box padding={4}>
<ButtonSecondary
size={BUTTON_SECONDARY_SIZES.LG}
2023-03-31 19:58:25 +02:00
block
onClick={() => {
isFullScreen
? history.push(ADD_POPULAR_CUSTOM_NETWORK)
: global.platform.openExtensionInBrowser(
ADD_POPULAR_CUSTOM_NETWORK,
);
dispatch(toggleNetworkMenu());
2023-04-27 16:28:08 +02:00
trackEvent({
event: MetaMetricsEventName.AddNetworkButtonClick,
category: MetaMetricsEventCategory.Network,
});
2023-03-31 19:58:25 +02:00
}}
>
{t('addNetwork')}
</ButtonSecondary>
2023-03-31 19:58:25 +02:00
</Box>
</>
</Popover>
);
};
NetworkListMenu.propTypes = {
/**
* Executes when the menu should be closed
*/
onClose: PropTypes.func.isRequired,
2023-03-31 19:58:25 +02:00
};