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 (
{this.context.t('networks')}
); } 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 (
{ setSelectedSettingsRpcUrl(rpcUrl); if (!isFullScreen) { history.push(NETWORKS_FORM_ROUTE); } }} >
{label || this.context.t(labelKey)} {currentProviderType !== NETWORK_TYPE_RPC && ( )}
); } renderNetworksList() { const { networksToRender, selectedNetwork, networkIsSelected, networkDefaultedToProvider, } = this.props; return (
{networksToRender.map((network) => this.renderNetworkListItem(network, selectedNetwork.rpcUrl), )}
); } 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 ? ( { 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 ? ( { if (shouldUpdateHistory) { history.push(NETWORKS_ROUTE); } }} onAddNetwork={() => { history.push(DEFAULT_ROUTE); }} rpcPrefs={rpcPrefs} networksToRender={networksToRender} setNewNetworkAdded={setNewNetworkAdded} addNewNetwork={addNewNetwork} /> ) : (
{isFullScreen ? this.renderSubHeader() : null}
{this.renderNetworksTabContent()} {!isFullScreen && !shouldRenderNetworkForm ? (
) : null}
); } }