1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 23:58:06 +01:00
metamask-extension/ui/pages/settings/networks-tab/networks-tab.component.js
Olusegun Akintayo b6673731e2
Implement Network Switcher designs (#12260)
* Show test networks toggle button in settings/advanced tab.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Apply toggle testnet settings and show/hide testnets when on/off
Add localhost to testnet.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Show add network button

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Open full screen when add network is called.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Show custonm rpc before testnet rpcs
lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Test cases for network dropdown.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Test cases for toggle test networks in advanced tab component.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Fix Locales.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* E2E Tests: Custom RPC is now called Add Network

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Lint fix

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* E2E: When Add Network button is clicked, wait for the full screen window to
be visible

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* findVisibleElement should use a class. i.e start with a dot

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Hide Dropdown when Add Netwok is clicked.
Only show full screen if it's not already showing.
E2E tests passing.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Lint fixes

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Fix tests for jest

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Testnets are not being shown by default anymore, tests should use
Mainnet instead.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Import Button from ui
Change selector name to getShowTestnetworks
Fix button to show full width

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Fix e2e tests

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Remove localhost from INFURA provider types.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Fix errors in Advanced Tab Component tests

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Lint fixes

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Fix unit tests for advanced tab component.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Remove deleted elements from e2e tests

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Make sure all tests passed.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Lint fixes

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
2021-10-28 23:31:05 +04:00

260 lines
7.6 KiB
JavaScript

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { NETWORK_TYPE_RPC } from '../../../../shared/constants/network';
import Button from '../../../components/ui/button';
import LockIcon from '../../../components/ui/lock-icon';
import {
NETWORKS_ROUTE,
NETWORKS_FORM_ROUTE,
DEFAULT_ROUTE,
ADD_NETWORK_ROUTE,
} from '../../../helpers/constants/routes';
import ColorIndicator from '../../../components/ui/color-indicator';
import { SIZES } from '../../../helpers/constants/design-system';
import NetworkForm from './network-form';
export default class NetworksTab extends PureComponent {
static contextTypes = {
t: PropTypes.func.isRequired,
metricsEvent: PropTypes.func.isRequired,
};
static propTypes = {
editRpc: PropTypes.func.isRequired,
location: PropTypes.object.isRequired,
networkIsSelected: PropTypes.bool,
networksToRender: PropTypes.arrayOf(PropTypes.object).isRequired,
selectedNetwork: PropTypes.object,
setRpcTarget: PropTypes.func.isRequired,
setSelectedSettingsRpcUrl: PropTypes.func.isRequired,
showConfirmDeleteNetworkModal: PropTypes.func.isRequired,
providerUrl: PropTypes.string,
providerType: PropTypes.string,
networkDefaultedToProvider: PropTypes.bool,
history: PropTypes.object.isRequired,
shouldRenderNetworkForm: PropTypes.bool.isRequired,
isFullScreen: PropTypes.bool.isRequired,
setNewNetworkAdded: PropTypes.func.isRequired,
addNewNetwork: PropTypes.bool,
};
componentWillUnmount() {
this.props.setSelectedSettingsRpcUrl('');
}
isCurrentPath(pathname) {
return this.props.location.pathname === pathname;
}
renderSubHeader() {
const { history } = this.props;
return (
<div className="settings-page__sub-header">
<span className="settings-page__sub-header-text">
{this.context.t('networks')}
</span>
<div className="networks-tab__add-network-header-button-wrapper">
<Button
type="primary"
onClick={(event) => {
event.preventDefault();
history.push(ADD_NETWORK_ROUTE);
}}
className="add-network-form__header-add-network-button"
>
{this.context.t('addANetwork')}
</Button>
</div>
</div>
);
}
renderNetworkListItem(network, selectRpcUrl) {
const {
setSelectedSettingsRpcUrl,
networkIsSelected,
providerUrl,
providerType,
history,
isFullScreen,
} = this.props;
const {
label,
labelKey,
rpcUrl,
providerType: currentProviderType,
} = network;
const listItemNetworkIsSelected = selectRpcUrl && selectRpcUrl === rpcUrl;
const listItemUrlIsProviderUrl = rpcUrl === providerUrl;
const listItemTypeIsProviderNonRpcType =
providerType !== NETWORK_TYPE_RPC && currentProviderType === providerType;
const listItemNetworkIsCurrentProvider =
!networkIsSelected &&
(listItemUrlIsProviderUrl || listItemTypeIsProviderNonRpcType);
const displayNetworkListItemAsSelected =
listItemNetworkIsSelected || listItemNetworkIsCurrentProvider;
return (
<div
key={`settings-network-list-item:${rpcUrl}`}
className="networks-tab__networks-list-item"
onClick={() => {
setSelectedSettingsRpcUrl(rpcUrl);
if (!isFullScreen) {
history.push(NETWORKS_FORM_ROUTE);
}
}}
>
<ColorIndicator
color={labelKey}
type={ColorIndicator.TYPES.FILLED}
size={SIZES.LG}
/>
<div
className={classnames('networks-tab__networks-list-name', {
'networks-tab__networks-list-name--selected': displayNetworkListItemAsSelected,
'networks-tab__networks-list-name--disabled':
currentProviderType !== NETWORK_TYPE_RPC &&
!displayNetworkListItemAsSelected,
})}
>
{label || this.context.t(labelKey)}
{currentProviderType !== NETWORK_TYPE_RPC && (
<LockIcon width="14px" height="17px" fill="#cdcdcd" />
)}
</div>
<div className="networks-tab__networks-list-arrow" />
</div>
);
}
renderNetworksList() {
const {
networksToRender,
selectedNetwork,
networkIsSelected,
networkDefaultedToProvider,
} = this.props;
return (
<div
className={classnames('networks-tab__networks-list', {
'networks-tab__networks-list--selection':
networkIsSelected && !networkDefaultedToProvider,
})}
>
{networksToRender.map((network) =>
this.renderNetworkListItem(network, selectedNetwork.rpcUrl),
)}
</div>
);
}
renderNetworksTabContent() {
const { t } = this.context;
const {
setRpcTarget,
showConfirmDeleteNetworkModal,
setSelectedSettingsRpcUrl,
selectedNetwork: {
labelKey,
label,
rpcUrl,
chainId,
ticker,
viewOnly,
rpcPrefs,
blockExplorerUrl,
},
editRpc,
providerUrl,
networksToRender,
history,
isFullScreen,
shouldRenderNetworkForm,
} = this.props;
return (
<>
{this.renderNetworksList()}
{shouldRenderNetworkForm ? (
<NetworkForm
setRpcTarget={setRpcTarget}
editRpc={editRpc}
networkName={label || (labelKey && t(labelKey)) || ''}
rpcUrl={rpcUrl}
chainId={chainId}
networksToRender={networksToRender}
ticker={ticker}
onClear={(shouldUpdateHistory = true) => {
setSelectedSettingsRpcUrl('');
if (shouldUpdateHistory) {
history.push(NETWORKS_ROUTE);
}
}}
showConfirmDeleteNetworkModal={showConfirmDeleteNetworkModal}
viewOnly={viewOnly}
isCurrentRpcTarget={providerUrl === rpcUrl}
rpcPrefs={rpcPrefs}
blockExplorerUrl={blockExplorerUrl}
isFullScreen={isFullScreen}
/>
) : null}
</>
);
}
render() {
const {
history,
isFullScreen,
shouldRenderNetworkForm,
setRpcTarget,
networksToRender,
setNewNetworkAdded,
selectedNetwork: { rpcPrefs },
addNewNetwork,
} = this.props;
return addNewNetwork ? (
<NetworkForm
setRpcTarget={setRpcTarget}
onClear={(shouldUpdateHistory = true) => {
if (shouldUpdateHistory) {
history.push(NETWORKS_ROUTE);
}
}}
onAddNetwork={() => {
history.push(DEFAULT_ROUTE);
}}
rpcPrefs={rpcPrefs}
networksToRender={networksToRender}
setNewNetworkAdded={setNewNetworkAdded}
addNewNetwork={addNewNetwork}
/>
) : (
<div className="networks-tab__body">
{isFullScreen ? this.renderSubHeader() : null}
<div className="networks-tab__content">
{this.renderNetworksTabContent()}
{!isFullScreen && !shouldRenderNetworkForm ? (
<div className="networks-tab__networks-list-popup-footer">
<Button
type="primary"
onClick={(event) => {
event.preventDefault();
global.platform.openExtensionInBrowser(ADD_NETWORK_ROUTE);
}}
>
{this.context.t('addNetwork')}
</Button>
</div>
) : null}
</div>
</div>
);
}
}