2020-02-24 23:58:26 +01:00
|
|
|
import { compose } from 'redux'
|
2019-05-09 19:27:14 +02:00
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import { withRouter } from 'react-router-dom'
|
|
|
|
import {
|
|
|
|
setSelectedSettingsRpcUrl,
|
|
|
|
updateAndSetCustomRpc,
|
|
|
|
displayWarning,
|
|
|
|
setNetworksTabAddMode,
|
|
|
|
editRpc,
|
2019-07-02 14:13:02 +02:00
|
|
|
showModal,
|
2019-05-09 19:27:14 +02:00
|
|
|
} from '../../../store/actions'
|
2020-08-18 21:18:25 +02:00
|
|
|
import NetworksTab from './networks-tab.component'
|
2019-05-09 19:27:14 +02:00
|
|
|
import { defaultNetworksData } from './networks-tab.constants'
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const defaultNetworks = defaultNetworksData.map((network) => ({ ...network, viewOnly: true }))
|
2019-05-09 19:27:14 +02:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const mapStateToProps = (state) => {
|
2019-05-09 19:27:14 +02:00
|
|
|
const {
|
|
|
|
frequentRpcListDetail,
|
|
|
|
provider,
|
|
|
|
} = state.metamask
|
|
|
|
const {
|
|
|
|
networksTabSelectedRpcUrl,
|
|
|
|
networksTabIsInAddMode,
|
|
|
|
} = state.appState
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const frequentRpcNetworkListDetails = frequentRpcListDetail.map((rpc) => {
|
2019-05-09 19:27:14 +02:00
|
|
|
return {
|
|
|
|
label: rpc.nickname,
|
|
|
|
iconColor: '#6A737D',
|
|
|
|
providerType: 'rpc',
|
|
|
|
rpcUrl: rpc.rpcUrl,
|
|
|
|
chainId: rpc.chainId,
|
|
|
|
ticker: rpc.ticker,
|
2020-02-15 21:56:48 +01:00
|
|
|
blockExplorerUrl: (rpc.rpcPrefs && rpc.rpcPrefs.blockExplorerUrl) || '',
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-19 18:27:05 +02:00
|
|
|
const networksToRender = [...defaultNetworks, ...frequentRpcNetworkListDetails]
|
2020-02-15 21:34:12 +01:00
|
|
|
let selectedNetwork = networksToRender.find((network) => network.rpcUrl === networksTabSelectedRpcUrl) || {}
|
2019-05-09 19:27:14 +02:00
|
|
|
const networkIsSelected = Boolean(selectedNetwork.rpcUrl)
|
|
|
|
|
|
|
|
let networkDefaultedToProvider = false
|
|
|
|
if (!networkIsSelected && !networksTabIsInAddMode) {
|
2020-02-15 21:34:12 +01:00
|
|
|
selectedNetwork = networksToRender.find((network) => {
|
2020-02-15 21:56:48 +01:00
|
|
|
return network.rpcUrl === provider.rpcTarget || (network.providerType !== 'rpc' && network.providerType === provider.type)
|
2019-05-09 19:27:14 +02:00
|
|
|
}) || {}
|
|
|
|
networkDefaultedToProvider = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
selectedNetwork,
|
|
|
|
networksToRender,
|
|
|
|
networkIsSelected,
|
|
|
|
networksTabIsInAddMode,
|
|
|
|
providerType: provider.type,
|
|
|
|
providerUrl: provider.rpcTarget,
|
|
|
|
networkDefaultedToProvider,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const mapDispatchToProps = (dispatch) => {
|
2019-05-09 19:27:14 +02:00
|
|
|
return {
|
2020-02-15 21:34:12 +01:00
|
|
|
setSelectedSettingsRpcUrl: (newRpcUrl) => dispatch(setSelectedSettingsRpcUrl(newRpcUrl)),
|
2019-05-09 19:27:14 +02:00
|
|
|
setRpcTarget: (newRpc, chainId, ticker, nickname, rpcPrefs) => {
|
|
|
|
dispatch(updateAndSetCustomRpc(newRpc, chainId, ticker, nickname, rpcPrefs))
|
|
|
|
},
|
2019-07-02 14:13:02 +02:00
|
|
|
showConfirmDeleteNetworkModal: ({ target, onConfirm }) => {
|
|
|
|
return dispatch(showModal({ name: 'CONFIRM_DELETE_NETWORK', target, onConfirm }))
|
2019-06-17 16:05:47 +02:00
|
|
|
},
|
2020-02-15 21:34:12 +01:00
|
|
|
displayWarning: (warning) => dispatch(displayWarning(warning)),
|
|
|
|
setNetworksTabAddMode: (isInAddMode) => dispatch(setNetworksTabAddMode(isInAddMode)),
|
2019-05-09 19:27:14 +02:00
|
|
|
editRpc: (oldRpc, newRpc, chainId, ticker, nickname, rpcPrefs) => {
|
|
|
|
dispatch(editRpc(oldRpc, newRpc, chainId, ticker, nickname, rpcPrefs))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withRouter,
|
2020-07-14 17:20:41 +02:00
|
|
|
connect(mapStateToProps, mapDispatchToProps),
|
2019-05-09 19:27:14 +02:00
|
|
|
)(NetworksTab)
|