1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 08:09:15 +01:00
metamask-extension/ui/pages/settings/networks-tab/networks-tab.js

162 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-11-04 22:48:21 +01:00
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { useLocation, useHistory } from 'react-router-dom';
2021-11-04 22:48:21 +01:00
import { useDispatch, useSelector } from 'react-redux';
import { useI18nContext } from '../../../hooks/useI18nContext';
import {
ADD_POPULAR_CUSTOM_NETWORK,
2021-11-04 22:48:21 +01:00
NETWORKS_FORM_ROUTE,
DEFAULT_ROUTE,
NETWORKS_ROUTE,
2021-11-04 22:48:21 +01:00
} from '../../../helpers/constants/routes';
import { setSelectedNetworkConfigurationId } from '../../../store/actions';
2021-11-04 22:48:21 +01:00
import Button from '../../../components/ui/button';
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../shared/constants/app';
import {
getNetworkConfigurations,
getNetworksTabSelectedNetworkConfigurationId,
2021-11-04 22:48:21 +01:00
getProvider,
} from '../../../selectors';
import {
CHAIN_IDS,
NETWORK_TYPES,
SHOULD_SHOW_LINEA_TESTNET_NETWORK,
TEST_CHAINS,
} from '../../../../shared/constants/network';
2021-11-04 22:48:21 +01:00
import { defaultNetworksData } from './networks-tab.constants';
import NetworksTabContent from './networks-tab-content';
import NetworksForm from './networks-form';
import NetworksFormSubheader from './networks-tab-subheader';
const defaultNetworks = defaultNetworksData.map((network) => ({
...network,
viewOnly: true,
isATestNetwork: TEST_CHAINS.includes(network.chainId),
2021-11-04 22:48:21 +01:00
}));
const NetworksTab = ({ addNewNetwork }) => {
const t = useI18nContext();
const dispatch = useDispatch();
const { pathname } = useLocation();
const history = useHistory();
2021-11-04 22:48:21 +01:00
const environmentType = getEnvironmentType();
const isFullScreen = environmentType === ENVIRONMENT_TYPE_FULLSCREEN;
const shouldRenderNetworkForm =
isFullScreen ||
Boolean(pathname.match(NETWORKS_FORM_ROUTE)) ||
window.location.hash.split('#')[2] === 'blockExplorerUrl';
2021-11-04 22:48:21 +01:00
const networkConfigurations = useSelector(getNetworkConfigurations);
2021-11-04 22:48:21 +01:00
const provider = useSelector(getProvider);
const networksTabSelectedNetworkConfigurationId = useSelector(
getNetworksTabSelectedNetworkConfigurationId,
);
2021-11-04 22:48:21 +01:00
const networkConfigurationsList = Object.entries(networkConfigurations)
.map(([networkConfigurationId, networkConfiguration]) => {
return {
label: networkConfiguration.nickname,
iconColor: 'var(--color-icon-alternative)',
providerType: NETWORK_TYPES.RPC,
rpcUrl: networkConfiguration.rpcUrl,
chainId: networkConfiguration.chainId,
ticker: networkConfiguration.ticker,
blockExplorerUrl: networkConfiguration.rpcPrefs?.blockExplorerUrl || '',
isATestNetwork: TEST_CHAINS.includes(networkConfiguration.chainId),
networkConfigurationId,
};
})
.filter((network) => network.chainId !== CHAIN_IDS.LINEA_TESTNET);
2021-11-04 22:48:21 +01:00
let networksToRender = [...defaultNetworks, ...networkConfigurationsList];
if (!SHOULD_SHOW_LINEA_TESTNET_NETWORK) {
networksToRender = networksToRender.filter(
(network) => network.chainId !== CHAIN_IDS.LINEA_TESTNET,
);
}
2021-11-04 22:48:21 +01:00
let selectedNetwork =
networksToRender.find(
(network) =>
network.networkConfigurationId ===
networksTabSelectedNetworkConfigurationId,
2021-11-04 22:48:21 +01:00
) || {};
const networkIsSelected = Boolean(selectedNetwork.rpcUrl);
let networkDefaultedToProvider = false;
if (!networkIsSelected) {
selectedNetwork =
networksToRender.find((network) => {
return (
network.rpcUrl === provider.rpcUrl ||
(network.providerType !== NETWORK_TYPES.RPC &&
2021-11-04 22:48:21 +01:00
network.providerType === provider.type)
);
}) || {};
networkDefaultedToProvider = true;
}
useEffect(() => {
return () => {
dispatch(setSelectedNetworkConfigurationId(''));
2021-11-04 22:48:21 +01:00
};
}, [dispatch]);
return (
<div className="networks-tab__body">
{isFullScreen ? (
<NetworksFormSubheader addNewNetwork={addNewNetwork} />
) : null}
<div
className={classnames('networks-tab__content', {
'networks-tab__content--with-networks-list-popup-footer':
!isFullScreen && !shouldRenderNetworkForm,
})}
>
2021-11-04 22:48:21 +01:00
{addNewNetwork ? (
<NetworksForm
networksToRender={networksToRender}
addNewNetwork={addNewNetwork}
submitCallback={() => history.push(DEFAULT_ROUTE)}
cancelCallback={() => history.push(NETWORKS_ROUTE)}
2021-11-04 22:48:21 +01:00
/>
) : (
<>
<NetworksTabContent
networkDefaultedToProvider={networkDefaultedToProvider}
networkIsSelected={networkIsSelected}
networksToRender={networksToRender}
providerUrl={provider.rpcUrl}
selectedNetwork={selectedNetwork}
shouldRenderNetworkForm={shouldRenderNetworkForm}
/>
{!isFullScreen && !shouldRenderNetworkForm ? (
<div className="networks-tab__networks-list-popup-footer">
<Button
type="primary"
onClick={() => {
isFullScreen
? history.push(ADD_POPULAR_CUSTOM_NETWORK)
: global.platform.openExtensionInBrowser(
ADD_POPULAR_CUSTOM_NETWORK,
);
2021-11-04 22:48:21 +01:00
}}
>
{t('addNetwork')}
</Button>
</div>
) : null}
</>
)}
</div>
</div>
);
};
NetworksTab.propTypes = {
addNewNetwork: PropTypes.bool,
};
export default NetworksTab;