2021-02-04 19:15:23 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { NETWORK_TYPE_RPC } from '../../../../shared/constants/network';
|
2021-02-04 19:15:23 +01:00
|
|
|
import Button from '../../../components/ui/button';
|
|
|
|
import LockIcon from '../../../components/ui/lock-icon';
|
2020-10-28 20:42:42 +01:00
|
|
|
import {
|
|
|
|
NETWORKS_ROUTE,
|
|
|
|
NETWORKS_FORM_ROUTE,
|
2021-09-03 17:52:37 +02:00
|
|
|
DEFAULT_ROUTE,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../helpers/constants/routes';
|
|
|
|
import ColorIndicator from '../../../components/ui/color-indicator';
|
|
|
|
import { COLORS, SIZES } from '../../../helpers/constants/design-system';
|
|
|
|
import NetworkForm from './network-form';
|
2019-05-09 19:27:14 +02:00
|
|
|
|
|
|
|
export default class NetworksTab extends PureComponent {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func.isRequired,
|
|
|
|
metricsEvent: PropTypes.func.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-05-09 19:27:14 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
editRpc: PropTypes.func.isRequired,
|
|
|
|
location: PropTypes.object.isRequired,
|
|
|
|
networkIsSelected: PropTypes.bool,
|
|
|
|
networksTabIsInAddMode: PropTypes.bool,
|
2019-11-19 06:11:50 +01:00
|
|
|
networksToRender: PropTypes.arrayOf(PropTypes.object).isRequired,
|
2019-05-09 19:27:14 +02:00
|
|
|
selectedNetwork: PropTypes.object,
|
|
|
|
setNetworksTabAddMode: PropTypes.func.isRequired,
|
|
|
|
setRpcTarget: PropTypes.func.isRequired,
|
|
|
|
setSelectedSettingsRpcUrl: PropTypes.func.isRequired,
|
2019-07-02 14:13:02 +02:00
|
|
|
showConfirmDeleteNetworkModal: PropTypes.func.isRequired,
|
2019-05-09 19:27:14 +02:00
|
|
|
providerUrl: PropTypes.string,
|
|
|
|
providerType: PropTypes.string,
|
|
|
|
networkDefaultedToProvider: PropTypes.bool,
|
2020-10-28 20:42:42 +01:00
|
|
|
history: PropTypes.object.isRequired,
|
|
|
|
shouldRenderNetworkForm: PropTypes.bool.isRequired,
|
|
|
|
isFullScreen: PropTypes.bool.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-27 22:55:09 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentWillUnmount() {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.props.setSelectedSettingsRpcUrl('');
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
isCurrentPath(pathname) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.props.location.pathname === pathname;
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderSubHeader() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { setSelectedSettingsRpcUrl, setNetworksTabAddMode } = this.props;
|
2019-05-09 19:27:14 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="settings-page__sub-header">
|
2020-11-03 00:41:28 +01:00
|
|
|
<span className="settings-page__sub-header-text">
|
|
|
|
{this.context.t('networks')}
|
|
|
|
</span>
|
2019-05-09 19:27:14 +02:00
|
|
|
<div className="networks-tab__add-network-header-button-wrapper">
|
|
|
|
<Button
|
2021-10-05 21:20:42 +02:00
|
|
|
type="primary"
|
2020-02-15 21:34:12 +01:00
|
|
|
onClick={(event) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
event.preventDefault();
|
|
|
|
setSelectedSettingsRpcUrl('');
|
|
|
|
setNetworksTabAddMode(true);
|
2019-05-09 19:27:14 +02:00
|
|
|
}}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('addNetwork')}
|
2019-05-09 19:27:14 +02:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderNetworkListItem(network, selectRpcUrl) {
|
2019-05-09 19:27:14 +02:00
|
|
|
const {
|
|
|
|
setSelectedSettingsRpcUrl,
|
|
|
|
setNetworksTabAddMode,
|
|
|
|
networkIsSelected,
|
|
|
|
providerUrl,
|
|
|
|
providerType,
|
|
|
|
networksTabIsInAddMode,
|
2020-10-28 20:42:42 +01:00
|
|
|
history,
|
|
|
|
isFullScreen,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2019-05-09 19:27:14 +02:00
|
|
|
const {
|
|
|
|
label,
|
|
|
|
labelKey,
|
|
|
|
rpcUrl,
|
|
|
|
providerType: currentProviderType,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = network;
|
2019-05-09 19:27:14 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const listItemNetworkIsSelected = selectRpcUrl && selectRpcUrl === rpcUrl;
|
|
|
|
const listItemUrlIsProviderUrl = rpcUrl === providerUrl;
|
2020-11-03 00:41:28 +01:00
|
|
|
const listItemTypeIsProviderNonRpcType =
|
2021-02-04 19:15:23 +01:00
|
|
|
providerType !== NETWORK_TYPE_RPC && currentProviderType === providerType;
|
2020-11-03 00:41:28 +01:00
|
|
|
const listItemNetworkIsCurrentProvider =
|
|
|
|
!networkIsSelected &&
|
|
|
|
!networksTabIsInAddMode &&
|
2021-02-04 19:15:23 +01:00
|
|
|
(listItemUrlIsProviderUrl || listItemTypeIsProviderNonRpcType);
|
2020-11-03 00:41:28 +01:00
|
|
|
const displayNetworkListItemAsSelected =
|
2021-02-04 19:15:23 +01:00
|
|
|
listItemNetworkIsSelected || listItemNetworkIsCurrentProvider;
|
2019-05-09 19:27:14 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2020-08-19 18:27:05 +02:00
|
|
|
key={`settings-network-list-item:${rpcUrl}`}
|
2019-05-09 19:27:14 +02:00
|
|
|
className="networks-tab__networks-list-item"
|
2020-10-28 20:42:42 +01:00
|
|
|
onClick={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
setNetworksTabAddMode(false);
|
|
|
|
setSelectedSettingsRpcUrl(rpcUrl);
|
2020-10-29 06:45:08 +01:00
|
|
|
if (!isFullScreen) {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(NETWORKS_FORM_ROUTE);
|
2020-10-29 06:45:08 +01:00
|
|
|
}
|
2019-05-09 19:27:14 +02:00
|
|
|
}}
|
2019-07-31 22:17:11 +02:00
|
|
|
>
|
2021-01-27 17:51:59 +01:00
|
|
|
<ColorIndicator
|
|
|
|
color={labelKey}
|
|
|
|
type={ColorIndicator.TYPES.FILLED}
|
2021-02-01 19:59:24 +01:00
|
|
|
size={SIZES.LG}
|
2019-05-09 19:27:14 +02:00
|
|
|
/>
|
2019-12-03 17:35:44 +01:00
|
|
|
<div
|
|
|
|
className={classnames('networks-tab__networks-list-name', {
|
|
|
|
'networks-tab__networks-list-name--selected': displayNetworkListItemAsSelected,
|
2020-11-03 00:41:28 +01:00
|
|
|
'networks-tab__networks-list-name--disabled':
|
2021-01-20 17:49:05 +01:00
|
|
|
currentProviderType !== NETWORK_TYPE_RPC &&
|
2020-11-03 00:41:28 +01:00
|
|
|
!displayNetworkListItemAsSelected,
|
2019-12-03 17:35:44 +01:00
|
|
|
})}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{label || this.context.t(labelKey)}
|
2021-01-20 17:49:05 +01:00
|
|
|
{currentProviderType !== NETWORK_TYPE_RPC && (
|
2020-11-03 00:41:28 +01:00
|
|
|
<LockIcon width="14px" height="17px" fill="#cdcdcd" />
|
|
|
|
)}
|
2019-05-09 19:27:14 +02:00
|
|
|
</div>
|
|
|
|
<div className="networks-tab__networks-list-arrow" />
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderNetworksList() {
|
|
|
|
const {
|
|
|
|
networksToRender,
|
|
|
|
selectedNetwork,
|
|
|
|
networkIsSelected,
|
|
|
|
networksTabIsInAddMode,
|
|
|
|
networkDefaultedToProvider,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2019-08-02 05:57:26 +02:00
|
|
|
|
2019-05-09 19:27:14 +02:00
|
|
|
return (
|
2019-06-17 16:05:47 +02:00
|
|
|
<div
|
|
|
|
className={classnames('networks-tab__networks-list', {
|
2020-11-03 00:41:28 +01:00
|
|
|
'networks-tab__networks-list--selection':
|
|
|
|
(networkIsSelected && !networkDefaultedToProvider) ||
|
|
|
|
networksTabIsInAddMode,
|
2019-06-17 16:05:47 +02:00
|
|
|
})}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{networksToRender.map((network) =>
|
|
|
|
this.renderNetworkListItem(network, selectedNetwork.rpcUrl),
|
|
|
|
)}
|
|
|
|
{networksTabIsInAddMode && (
|
|
|
|
<div className="networks-tab__networks-list-item">
|
2021-01-27 17:51:59 +01:00
|
|
|
<ColorIndicator
|
|
|
|
type={ColorIndicator.TYPES.FILLED}
|
|
|
|
color={COLORS.WHITE}
|
|
|
|
borderColor={COLORS.UI4}
|
2021-02-01 19:59:24 +01:00
|
|
|
size={SIZES.LG}
|
2020-11-03 00:41:28 +01:00
|
|
|
/>
|
|
|
|
<div className="networks-tab__networks-list-name networks-tab__networks-list-name--selected">
|
|
|
|
{this.context.t('newNetwork')}
|
2019-06-17 16:05:47 +02:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="networks-tab__networks-list-arrow" />
|
|
|
|
</div>
|
|
|
|
)}
|
2019-05-09 19:27:14 +02:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderNetworksTabContent() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
2019-05-09 19:27:14 +02:00
|
|
|
const {
|
|
|
|
setRpcTarget,
|
2019-07-02 14:13:02 +02:00
|
|
|
showConfirmDeleteNetworkModal,
|
2019-05-09 19:27:14 +02:00
|
|
|
setSelectedSettingsRpcUrl,
|
|
|
|
setNetworksTabAddMode,
|
|
|
|
selectedNetwork: {
|
|
|
|
labelKey,
|
|
|
|
label,
|
|
|
|
rpcUrl,
|
|
|
|
chainId,
|
|
|
|
ticker,
|
|
|
|
viewOnly,
|
|
|
|
rpcPrefs,
|
|
|
|
blockExplorerUrl,
|
|
|
|
},
|
|
|
|
networksTabIsInAddMode,
|
|
|
|
editRpc,
|
2019-06-17 16:05:47 +02:00
|
|
|
providerUrl,
|
2019-11-19 06:11:50 +01:00
|
|
|
networksToRender,
|
2020-10-28 20:42:42 +01:00
|
|
|
history,
|
|
|
|
isFullScreen,
|
|
|
|
shouldRenderNetworkForm,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2019-05-09 19:27:14 +02:00
|
|
|
|
|
|
|
return (
|
2020-10-27 22:55:09 +01:00
|
|
|
<>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.renderNetworksList()}
|
|
|
|
{shouldRenderNetworkForm ? (
|
|
|
|
<NetworkForm
|
|
|
|
setRpcTarget={setRpcTarget}
|
|
|
|
editRpc={editRpc}
|
|
|
|
networkName={label || (labelKey && t(labelKey)) || ''}
|
|
|
|
rpcUrl={rpcUrl}
|
|
|
|
chainId={chainId}
|
2021-06-04 15:52:07 +02:00
|
|
|
networksToRender={networksToRender}
|
2020-11-03 00:41:28 +01:00
|
|
|
ticker={ticker}
|
|
|
|
onClear={(shouldUpdateHistory = true) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
setNetworksTabAddMode(false);
|
|
|
|
setSelectedSettingsRpcUrl('');
|
2021-09-03 17:52:37 +02:00
|
|
|
if (shouldUpdateHistory) {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(NETWORKS_ROUTE);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
|
|
|
}}
|
2021-09-03 17:52:37 +02:00
|
|
|
onAddNetwork={() => {
|
|
|
|
history.push(DEFAULT_ROUTE);
|
|
|
|
}}
|
2020-11-03 00:41:28 +01:00
|
|
|
showConfirmDeleteNetworkModal={showConfirmDeleteNetworkModal}
|
|
|
|
viewOnly={viewOnly}
|
|
|
|
isCurrentRpcTarget={providerUrl === rpcUrl}
|
|
|
|
networksTabIsInAddMode={networksTabIsInAddMode}
|
|
|
|
rpcPrefs={rpcPrefs}
|
|
|
|
blockExplorerUrl={blockExplorerUrl}
|
|
|
|
isFullScreen={isFullScreen}
|
|
|
|
/>
|
|
|
|
) : null}
|
2020-10-27 22:55:09 +01:00
|
|
|
</>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2020-10-28 20:42:42 +01:00
|
|
|
const {
|
|
|
|
setNetworksTabAddMode,
|
|
|
|
setSelectedSettingsRpcUrl,
|
|
|
|
history,
|
|
|
|
isFullScreen,
|
2020-10-28 23:29:31 +01:00
|
|
|
shouldRenderNetworkForm,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2019-05-09 19:27:14 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="networks-tab__body">
|
2021-10-21 18:11:31 +02:00
|
|
|
{isFullScreen ? this.renderSubHeader() : null}
|
2020-10-27 22:55:09 +01:00
|
|
|
<div className="networks-tab__content">
|
2020-10-28 20:42:42 +01:00
|
|
|
{this.renderNetworksTabContent()}
|
2020-11-03 00:41:28 +01:00
|
|
|
{!isFullScreen && !shouldRenderNetworkForm ? (
|
|
|
|
<div className="networks-tab__networks-list-popup-footer">
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
onClick={(event) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
event.preventDefault();
|
|
|
|
setSelectedSettingsRpcUrl('');
|
|
|
|
setNetworksTabAddMode(true);
|
|
|
|
history.push(NETWORKS_FORM_ROUTE);
|
2020-11-03 00:41:28 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{this.context.t('addNetwork')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2020-10-27 22:55:09 +01:00
|
|
|
</div>
|
2019-05-09 19:27:14 +02:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
|
|
|
}
|