1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 06:07:06 +01:00
metamask-extension/ui/components/multichain/network-list-menu/network-list-menu.js
David Walsh b89630fdd2
Release MultiChain 0.5 (#18903)
* Show portfolio icon in ETH overview

* Show new copy button in QR code modal

* Show address copy button in wallet overview

* Update connected status component

* Remove legacy MenuBar

* Remove legacy ImportTokenLink

* Remove AssetListItem

* Remove DetectedTokensLink

* Remove legacy AppHeader

* Remove MULTICHAIN flag from builds.yml

* Remove legacy AccountMenu

* FIX: Token cell snapshot

* Add data-testid for Account Picker

* Remove multichain check in LoadingNetworkScreen

* Remove MULTICHAIN check for AccountDetailsModal

* Remove MULTICHAIN check for AssetList

* Update QR dimensions

* Remove MULTICHAIN declaration from metamaskrc.dist

* Implement PickerNetwork and NetworkListMenu in onboarding

* Remove legacy NetworkDropdown and Dropdown

* Remove documentation about legacy account menu

* FIX: Fixes route tests for missing data-testid=network-display

* Fix account-menu-icon data-testid

* Fix TokenCell test

* FIX Onboarding Flow tests

* Remove unused locales from AccountMenu removal

* E2E: Fix Import Secret Recovery Phrase: logs out of the vault

* E2E: Fix Show account details: should show the QR code for the account

* E2E: Fix add-account.spec.js

* E2E: Fix state-logs.spec.js

* E2E: Fix lock-account.spec.js

* E2E: Fix settings-general.spec.js

* E2E: Fix advanced-settings.spec.js

* E2E: Fix auto-lock.spec.js

* E2E: Fix backup-restore.spec.js

* E2E: Fix clear-activity.spec.js

* E2E: Fix settings-search.spec.js

* E2E: Fix encrypt-decrypt.spec.js

* E2E: Fix dapp-interactions.spec.js

* E2E: Fix test-snap-management.spec.js

* E2E: Fix add-custom-network.spec.js

* E2E: Fix from-import-ui.spec.js

* E2E: Fix provider-api.spec.js

* E2E: Fix chain-interactions.spec.js

* E2E: Fix custom-rpc-history.spec.js

* Remove network icon from overview components

* E2E: Fix user-actions-benchmark.js

* E2E: Fix benchmark.js

* E2E: Fix add-hide-token.spec.js

* E2E: Fix address-book.spec.js

* E2E: Fix custom-token-add-approve.spec.js

* E2E: Fix incremental-security.spec.js

* E2E: Fix metamask-responsive-ui.spec.js

* E2E: Onboarding.spec.js

* E2E: Fix permissions.spec.js

* E2E: Fix send-hex-address.spec.js

* E2E: Fix send-to-contract.spec.js

* Remove dead AccountOptionsMenu test

* E2E: Fix token-details.spec.js

* E2E: Fix switch-custom-network.spec.js

* E2E: Fix metamask-ui.spec.js

* Revert "UX Multichain: updated border top for activity list (#19176)"

This reverts commit 15598f2a23.

* E2Es: Fix test-snap-management.spec.js and test-snap-notification.spec.js

* E2Es: Fix add-account.spec.js after flaky test fixes

* e2e flaky test

* adds back the mmi options

* scss fix

* test fix

* removes unnecessary double quotes

* Prevent double logos on login screen

* Update ui/components/ui/list-item/index.scss

Co-authored-by: Nidhi Kumari <nidhi.kumari@consensys.net>

---------

Co-authored-by: seaona <mariona@gmx.es>
Co-authored-by: Antonio Regadas <antonio.regadas@consensys.net>
Co-authored-by: Nidhi Kumari <nidhi.kumari@consensys.net>
2023-06-01 16:14:38 -05:00

209 lines
7.5 KiB
JavaScript

import React, { useContext, useEffect, useRef } from 'react';
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,
} from '../../../store/actions';
import { CHAIN_IDS, TEST_CHAINS } from '../../../../shared/constants/network';
import {
getShowTestNetworks,
getAllEnabledNetworks,
getCurrentChainId,
getNetworkConfigurations,
} 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';
import { ADD_POPULAR_CUSTOM_NETWORK } from '../../../helpers/constants/routes';
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../shared/constants/app';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import {
MetaMetricsEventCategory,
MetaMetricsEventName,
MetaMetricsNetworkEventSource,
} from '../../../../shared/constants/metametrics';
import { getCompletedOnboarding } from '../../../ducks/metamask/metamask';
const UNREMOVABLE_CHAIN_IDS = [CHAIN_IDS.MAINNET, ...TEST_CHAINS];
export const NetworkListMenu = ({ onClose }) => {
const t = useI18nContext();
const networks = useSelector(getAllEnabledNetworks);
const showTestNetworks = useSelector(getShowTestNetworks);
const networkConfigurations = useSelector(getNetworkConfigurations);
const currentChainId = useSelector(getCurrentChainId);
const dispatch = useDispatch();
const history = useHistory();
const trackEvent = useContext(MetaMetricsContext);
const environmentType = getEnvironmentType();
const isFullScreen = environmentType === ENVIRONMENT_TYPE_FULLSCREEN;
const showTestNetworksRef = useRef(showTestNetworks);
const networkListRef = useRef(null);
const completedOnboarding = useSelector(getCompletedOnboarding);
useEffect(() => {
if (showTestNetworks && !showTestNetworksRef.current) {
// Scroll to the bottom of the list
networkListRef.current.lastChild.scrollIntoView();
}
showTestNetworksRef.current = showTestNetworks;
}, [showTestNetworks, showTestNetworksRef]);
return (
<Popover
contentClassName="multichain-network-list-menu-content-wrapper"
onClose={onClose}
centerTitle
title={t('networkMenuHeading')}
>
<>
<Box className="multichain-network-list-menu" ref={networkListRef}>
{networks.map((network, index) => {
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}-${index}`}
selected={isCurrentNetwork}
onClick={async () => {
dispatch(toggleNetworkMenu());
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));
}
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
}
/>
);
})}
</Box>
<Box
padding={4}
display={DISPLAY.FLEX}
justifyContent={JustifyContent.spaceBetween}
>
<Text>{t('showTestnetNetworks')}</Text>
<ToggleButton
value={showTestNetworks}
onToggle={(value) => {
const shouldShowTestNetworks = !value;
dispatch(setShowTestNetworks(shouldShowTestNetworks));
if (shouldShowTestNetworks) {
trackEvent({
event: MetaMetricsEventName.TestNetworksDisplayed,
category: MetaMetricsEventCategory.Network,
});
}
}}
/>
</Box>
<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' }));
}
} else {
global.platform.openExtensionInBrowser(
ADD_POPULAR_CUSTOM_NETWORK,
);
}
dispatch(toggleNetworkMenu());
trackEvent({
event: MetaMetricsEventName.AddNetworkButtonClick,
category: MetaMetricsEventCategory.Network,
});
}}
>
{t('addNetwork')}
</ButtonSecondary>
</Box>
</>
</Popover>
);
};
NetworkListMenu.propTypes = {
/**
* Executes when the menu should be closed
*/
onClose: PropTypes.func.isRequired,
};