You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
277 lines
6.7 KiB
277 lines
6.7 KiB
7 years ago
|
const Component = require('react').Component
|
||
|
const h = require('react-hyperscript')
|
||
|
const inherits = require('util').inherits
|
||
|
const connect = require('react-redux').connect
|
||
|
const actions = require('../../actions')
|
||
7 years ago
|
const Dropdown = require('../dropdown').Dropdown
|
||
|
const DropdownMenuItem = require('../dropdown').DropdownMenuItem
|
||
7 years ago
|
|
||
|
function mapStateToProps (state) {
|
||
|
return {
|
||
7 years ago
|
provider: state.metamask.provider,
|
||
|
frequentRpcList: state.metamask.frequentRpcList || [],
|
||
|
networkDropdownOpen: state.appState.networkDropdownOpen,
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
function mapDispatchToProps (dispatch) {
|
||
|
return {
|
||
|
hideModal: () => {
|
||
|
dispatch(actions.hideModal())
|
||
|
},
|
||
7 years ago
|
setProviderType: (type) => {
|
||
|
dispatch(actions.setProviderType(type))
|
||
|
},
|
||
|
setDefaultRpcTarget: () => {
|
||
|
dispatch(actions.setDefaultRpcTarget(type))
|
||
|
},
|
||
|
setRpcTarget: (target) => {
|
||
|
dispatch(actions.setRpcTarget(target))
|
||
|
},
|
||
|
showConfigPage: () => {
|
||
|
dispatch(actions.showConfigPage())
|
||
|
},
|
||
|
showNetworkDropdown: () => {dispatch(actions.showNetworkDropdown())},
|
||
|
hideNetworkDropdown: () => {dispatch(actions.hideNetworkDropdown())},
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
inherits(NetworkDropdown, Component)
|
||
|
function NetworkDropdown () {
|
||
|
Component.call(this)
|
||
|
}
|
||
|
|
||
|
// renderNetworkDropdown
|
||
|
// renderCustomOption
|
||
|
// renderCommonRpc
|
||
|
// TODO: specify default props and proptypes
|
||
|
NetworkDropdown.prototype.render = function () {
|
||
7 years ago
|
console.log("RENDER")
|
||
7 years ago
|
const props = this.props
|
||
|
const { provider: { type: providerType, rpcTarget: activeNetwork } } = props
|
||
|
const rpcList = props.frequentRpcList
|
||
|
const state = this.state || {}
|
||
7 years ago
|
console.log("this.state", state)
|
||
|
const isOpen = this.props.networkDropdownOpen
|
||
|
|
||
|
console.log("isOpen", isOpen)
|
||
7 years ago
|
|
||
|
return h(Dropdown, {
|
||
|
useCssTransition: true,
|
||
|
isOpen,
|
||
|
onClickOutside: (event) => {
|
||
|
const { classList } = event.target
|
||
|
const isNotToggleElement = [
|
||
|
classList.contains('menu-icon'),
|
||
|
classList.contains('network-name'),
|
||
|
classList.contains('network-indicator'),
|
||
|
].filter(bool => bool).length === 0
|
||
|
// classes from three constituent nodes of the toggle element
|
||
|
|
||
|
if (isNotToggleElement) {
|
||
7 years ago
|
this.props.hideNetworkDropdown()
|
||
7 years ago
|
}
|
||
|
},
|
||
|
zIndex: 11,
|
||
|
style: {
|
||
|
position: 'absolute',
|
||
|
right: '2px',
|
||
|
top: '38px',
|
||
|
},
|
||
|
innerStyle: {
|
||
|
padding: '2px 16px 2px 0px',
|
||
|
},
|
||
|
}, [
|
||
|
|
||
|
h(
|
||
|
DropdownMenuItem,
|
||
|
{
|
||
|
key: 'main',
|
||
7 years ago
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
||
|
onClick: () => props.setProviderType('mainnet'),
|
||
7 years ago
|
style: {
|
||
|
fontSize: '18px',
|
||
|
},
|
||
|
},
|
||
|
[
|
||
|
h('.menu-icon.diamond'),
|
||
|
'Main Ethereum Network',
|
||
|
providerType === 'mainnet' ? h('.check', '✓') : null,
|
||
|
]
|
||
|
),
|
||
|
|
||
|
h(
|
||
|
DropdownMenuItem,
|
||
|
{
|
||
|
key: 'ropsten',
|
||
7 years ago
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
||
|
onClick: () => props.setProviderType('ropsten'),
|
||
7 years ago
|
style: {
|
||
|
fontSize: '18px',
|
||
|
},
|
||
|
},
|
||
|
[
|
||
|
h('.menu-icon.red-dot'),
|
||
|
'Ropsten Test Network',
|
||
|
providerType === 'ropsten' ? h('.check', '✓') : null,
|
||
|
]
|
||
|
),
|
||
|
|
||
|
h(
|
||
|
DropdownMenuItem,
|
||
|
{
|
||
|
key: 'kovan',
|
||
7 years ago
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
||
|
onClick: () => props.setProviderType('kovan'),
|
||
7 years ago
|
style: {
|
||
|
fontSize: '18px',
|
||
|
},
|
||
|
},
|
||
|
[
|
||
|
h('.menu-icon.hollow-diamond'),
|
||
|
'Kovan Test Network',
|
||
|
providerType === 'kovan' ? h('.check', '✓') : null,
|
||
|
]
|
||
|
),
|
||
|
|
||
|
h(
|
||
|
DropdownMenuItem,
|
||
|
{
|
||
|
key: 'rinkeby',
|
||
7 years ago
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
||
|
onClick: () => propssetProviderType('rinkeby'),
|
||
7 years ago
|
style: {
|
||
|
fontSize: '18px',
|
||
|
},
|
||
|
},
|
||
|
[
|
||
|
h('.menu-icon.golden-square'),
|
||
|
'Rinkeby Test Network',
|
||
|
providerType === 'rinkeby' ? h('.check', '✓') : null,
|
||
|
]
|
||
|
),
|
||
|
|
||
|
h(
|
||
|
DropdownMenuItem,
|
||
|
{
|
||
|
key: 'default',
|
||
7 years ago
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
||
|
onClick: () => props.setDefaultRpcTarget(),
|
||
7 years ago
|
style: {
|
||
|
fontSize: '18px',
|
||
|
},
|
||
|
},
|
||
|
[
|
||
|
h('i.fa.fa-question-circle.fa-lg.menu-icon'),
|
||
|
'Localhost 8545',
|
||
|
activeNetwork === 'http://localhost:8545' ? h('.check', '✓') : null,
|
||
|
]
|
||
|
),
|
||
|
|
||
|
this.renderCustomOption(props.provider),
|
||
|
this.renderCommonRpc(rpcList, props.provider),
|
||
|
|
||
|
h(
|
||
|
DropdownMenuItem,
|
||
|
{
|
||
7 years ago
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
||
|
onClick: () => this.props.showConfigPage(),
|
||
7 years ago
|
style: {
|
||
|
fontSize: '18px',
|
||
|
},
|
||
|
},
|
||
|
[
|
||
|
h('i.fa.fa-question-circle.fa-lg.menu-icon'),
|
||
|
'Custom RPC',
|
||
|
activeNetwork === 'custom' ? h('.check', '✓') : null,
|
||
|
]
|
||
|
),
|
||
|
|
||
|
])
|
||
|
}
|
||
|
|
||
|
|
||
|
NetworkDropdown.prototype.getNetworkName = function () {
|
||
|
const { provider } = this.props
|
||
|
const providerName = provider.type
|
||
|
|
||
|
let name
|
||
|
|
||
|
if (providerName === 'mainnet') {
|
||
|
name = 'Main Ethereum Network'
|
||
|
} else if (providerName === 'ropsten') {
|
||
|
name = 'Ropsten Test Network'
|
||
|
} else if (providerName === 'kovan') {
|
||
|
name = 'Kovan Test Network'
|
||
|
} else if (providerName === 'rinkeby') {
|
||
|
name = 'Rinkeby Test Network'
|
||
|
} else {
|
||
|
name = 'Unknown Private Network'
|
||
|
}
|
||
|
|
||
|
return name
|
||
|
}
|
||
|
|
||
|
NetworkDropdown.prototype.renderCommonRpc = function (rpcList, provider) {
|
||
|
const props = this.props
|
||
|
const rpcTarget = provider.rpcTarget
|
||
|
|
||
|
return rpcList.map((rpc) => {
|
||
|
if ((rpc === 'http://localhost:8545') || (rpc === rpcTarget)) {
|
||
|
return null
|
||
|
} else {
|
||
|
return h(
|
||
|
DropdownMenuItem,
|
||
|
{
|
||
|
key: `common${rpc}`,
|
||
7 years ago
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
||
|
onClick: () => props.setRpcTarget(rpc),
|
||
7 years ago
|
},
|
||
|
[
|
||
|
h('i.fa.fa-question-circle.fa-lg.menu-icon'),
|
||
|
rpc,
|
||
|
rpcTarget === rpc ? h('.check', '✓') : null,
|
||
|
]
|
||
|
)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
NetworkDropdown.prototype.renderCustomOption = function (provider) {
|
||
|
const { rpcTarget, type } = provider
|
||
|
const props = this.props
|
||
|
|
||
|
if (type !== 'rpc') return null
|
||
|
|
||
|
// Concatenate long URLs
|
||
|
let label = rpcTarget
|
||
|
if (rpcTarget.length > 31) {
|
||
|
label = label.substr(0, 34) + '...'
|
||
|
}
|
||
|
|
||
|
switch (rpcTarget) {
|
||
|
|
||
|
case 'http://localhost:8545':
|
||
|
return null
|
||
|
|
||
|
default:
|
||
|
return h(
|
||
|
DropdownMenuItem,
|
||
|
{
|
||
|
key: rpcTarget,
|
||
7 years ago
|
onClick: () => props.setRpcTarget(rpcTarget),
|
||
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
||
7 years ago
|
},
|
||
|
[
|
||
|
h('i.fa.fa-question-circle.fa-lg.menu-icon'),
|
||
|
label,
|
||
|
h('.check', '✓'),
|
||
|
]
|
||
|
)
|
||
|
}
|
||
|
}
|
||
7 years ago
|
|
||
|
const comp = connect(mapStateToProps, mapDispatchToProps)(NetworkDropdown)
|
||
|
module.exports = comp
|