2019-05-09 19:27:14 +02:00
|
|
|
import React, { PureComponent } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2020-08-18 21:18:25 +02:00
|
|
|
import classnames from 'classnames'
|
2019-05-09 19:27:14 +02:00
|
|
|
import Button from '../../../components/ui/button'
|
2020-08-19 17:01:58 +02:00
|
|
|
import LockIcon from '../../../components/ui/lock-icon'
|
2020-10-28 20:42:42 +01:00
|
|
|
import {
|
|
|
|
NETWORKS_ROUTE,
|
|
|
|
NETWORKS_FORM_ROUTE,
|
|
|
|
} from '../../../helpers/constants/routes'
|
2019-05-09 19:27:14 +02:00
|
|
|
import NetworkDropdownIcon from '../../../components/app/dropdowns/components/network-dropdown-icon'
|
2020-08-18 21:18:25 +02:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-10-27 22:55:09 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentWillUnmount() {
|
2020-10-27 22:55:09 +01:00
|
|
|
this.props.setSelectedSettingsRpcUrl('')
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
isCurrentPath(pathname) {
|
2019-05-09 19:27:14 +02:00
|
|
|
return this.props.location.pathname === pathname
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderSubHeader() {
|
|
|
|
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
|
2019-06-17 16:05:47 +02:00
|
|
|
type="secondary"
|
2020-02-15 21:34:12 +01:00
|
|
|
onClick={(event) => {
|
2019-05-09 19:27:14 +02:00
|
|
|
event.preventDefault()
|
2020-10-27 22:55:09 +01:00
|
|
|
setSelectedSettingsRpcUrl('')
|
2019-05-09 19:27:14 +02:00
|
|
|
setNetworksTabAddMode(true)
|
|
|
|
}}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('addNetwork')}
|
2019-05-09 19:27:14 +02:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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,
|
2019-05-09 19:27:14 +02:00
|
|
|
} = this.props
|
|
|
|
const {
|
|
|
|
border,
|
|
|
|
iconColor,
|
|
|
|
label,
|
|
|
|
labelKey,
|
|
|
|
rpcUrl,
|
|
|
|
providerType: currentProviderType,
|
|
|
|
} = network
|
|
|
|
|
|
|
|
const listItemNetworkIsSelected = selectRpcUrl && selectRpcUrl === rpcUrl
|
|
|
|
const listItemUrlIsProviderUrl = rpcUrl === providerUrl
|
2020-11-03 00:41:28 +01:00
|
|
|
const listItemTypeIsProviderNonRpcType =
|
|
|
|
providerType !== 'rpc' && currentProviderType === providerType
|
|
|
|
const listItemNetworkIsCurrentProvider =
|
|
|
|
!networkIsSelected &&
|
|
|
|
!networksTabIsInAddMode &&
|
|
|
|
(listItemUrlIsProviderUrl || listItemTypeIsProviderNonRpcType)
|
|
|
|
const displayNetworkListItemAsSelected =
|
|
|
|
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={() => {
|
2019-05-09 19:27:14 +02:00
|
|
|
setNetworksTabAddMode(false)
|
|
|
|
setSelectedSettingsRpcUrl(rpcUrl)
|
2020-10-29 06:45:08 +01:00
|
|
|
if (!isFullScreen) {
|
|
|
|
history.push(NETWORKS_FORM_ROUTE)
|
|
|
|
}
|
2019-05-09 19:27:14 +02:00
|
|
|
}}
|
2019-07-31 22:17:11 +02:00
|
|
|
>
|
2019-05-09 19:27:14 +02:00
|
|
|
<NetworkDropdownIcon
|
|
|
|
backgroundColor={iconColor || 'white'}
|
|
|
|
innerBorder={border}
|
|
|
|
/>
|
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':
|
|
|
|
currentProviderType !== 'rpc' &&
|
|
|
|
!displayNetworkListItemAsSelected,
|
2019-12-03 17:35:44 +01:00
|
|
|
})}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{label || this.context.t(labelKey)}
|
|
|
|
{currentProviderType !== 'rpc' && (
|
|
|
|
<LockIcon width="14px" height="17px" fill="#cdcdcd" />
|
|
|
|
)}
|
2019-05-09 19:27:14 +02:00
|
|
|
</div>
|
|
|
|
<div className="networks-tab__networks-list-arrow" />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderNetworksList() {
|
|
|
|
const {
|
|
|
|
networksToRender,
|
|
|
|
selectedNetwork,
|
|
|
|
networkIsSelected,
|
|
|
|
networksTabIsInAddMode,
|
|
|
|
networkDefaultedToProvider,
|
|
|
|
} = 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">
|
|
|
|
<NetworkDropdownIcon
|
|
|
|
backgroundColor="white"
|
|
|
|
innerBorder="1px solid rgb(106, 115, 125)"
|
|
|
|
/>
|
|
|
|
<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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderNetworksTabContent() {
|
2019-06-17 16:05:47 +02: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,
|
2019-05-09 19:27:14 +02:00
|
|
|
} = this.props
|
|
|
|
|
|
|
|
return (
|
2020-10-27 22:55:09 +01:00
|
|
|
<>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.renderNetworksList()}
|
|
|
|
{shouldRenderNetworkForm ? (
|
|
|
|
<NetworkForm
|
|
|
|
rpcUrls={networksToRender.map((network) => network.rpcUrl)}
|
|
|
|
setRpcTarget={setRpcTarget}
|
|
|
|
editRpc={editRpc}
|
|
|
|
networkName={label || (labelKey && t(labelKey)) || ''}
|
|
|
|
rpcUrl={rpcUrl}
|
|
|
|
chainId={chainId}
|
|
|
|
ticker={ticker}
|
|
|
|
onClear={(shouldUpdateHistory = true) => {
|
|
|
|
setNetworksTabAddMode(false)
|
|
|
|
setSelectedSettingsRpcUrl('')
|
|
|
|
if (shouldUpdateHistory && !isFullScreen) {
|
|
|
|
history.push(NETWORKS_ROUTE)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
showConfirmDeleteNetworkModal={showConfirmDeleteNetworkModal}
|
|
|
|
viewOnly={viewOnly}
|
|
|
|
isCurrentRpcTarget={providerUrl === rpcUrl}
|
|
|
|
networksTabIsInAddMode={networksTabIsInAddMode}
|
|
|
|
rpcPrefs={rpcPrefs}
|
|
|
|
blockExplorerUrl={blockExplorerUrl}
|
|
|
|
isFullScreen={isFullScreen}
|
|
|
|
/>
|
|
|
|
) : null}
|
2020-10-27 22:55:09 +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,
|
2020-10-28 20:42:42 +01:00
|
|
|
} = this.props
|
2019-05-09 19:27:14 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="networks-tab__body">
|
2020-10-27 22:55:09 +01:00
|
|
|
{isFullScreen && this.renderSubHeader()}
|
|
|
|
<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) => {
|
|
|
|
event.preventDefault()
|
|
|
|
setSelectedSettingsRpcUrl('')
|
|
|
|
setNetworksTabAddMode(true)
|
|
|
|
history.push(NETWORKS_FORM_ROUTE)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|