2017-08-13 19:49:41 +02:00
|
|
|
const Component = require('react').Component
|
2018-03-29 17:00:44 +02:00
|
|
|
const PropTypes = require('prop-types')
|
2017-08-13 19:49:41 +02:00
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
2018-03-29 17:00:44 +02:00
|
|
|
const connect = require('react-redux').connect
|
2018-04-19 02:34:28 +02:00
|
|
|
const { withRouter } = require('react-router-dom')
|
|
|
|
const { compose } = require('recompose')
|
2017-08-13 19:49:41 +02:00
|
|
|
const actions = require('../../actions')
|
2017-08-14 10:31:27 +02:00
|
|
|
const Dropdown = require('./components/dropdown').Dropdown
|
|
|
|
const DropdownMenuItem = require('./components/dropdown').DropdownMenuItem
|
2017-09-07 00:37:48 +02:00
|
|
|
const NetworkDropdownIcon = require('./components/network-dropdown-icon')
|
2017-12-01 23:19:53 +01:00
|
|
|
const R = require('ramda')
|
2018-04-19 02:34:28 +02:00
|
|
|
const { SETTINGS_ROUTE } = require('../../routes')
|
2017-12-01 23:19:53 +01:00
|
|
|
|
|
|
|
// classes from nodes of the toggle element.
|
|
|
|
const notToggleElementClassnames = [
|
|
|
|
'menu-icon',
|
|
|
|
'network-name',
|
|
|
|
'network-indicator',
|
|
|
|
'network-caret',
|
|
|
|
'network-component',
|
|
|
|
]
|
2017-08-13 19:49:41 +02:00
|
|
|
|
|
|
|
function mapStateToProps (state) {
|
|
|
|
return {
|
2017-08-13 22:15:21 +02:00
|
|
|
provider: state.metamask.provider,
|
|
|
|
frequentRpcList: state.metamask.frequentRpcList || [],
|
|
|
|
networkDropdownOpen: state.appState.networkDropdownOpen,
|
2017-08-13 19:49:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps (dispatch) {
|
|
|
|
return {
|
|
|
|
hideModal: () => {
|
|
|
|
dispatch(actions.hideModal())
|
|
|
|
},
|
2017-08-13 22:15:21 +02:00
|
|
|
setProviderType: (type) => {
|
|
|
|
dispatch(actions.setProviderType(type))
|
|
|
|
},
|
2017-08-31 13:08:11 +02:00
|
|
|
setDefaultRpcTarget: type => {
|
2017-08-13 22:15:21 +02:00
|
|
|
dispatch(actions.setDefaultRpcTarget(type))
|
|
|
|
},
|
|
|
|
setRpcTarget: (target) => {
|
|
|
|
dispatch(actions.setRpcTarget(target))
|
|
|
|
},
|
2017-12-01 23:19:53 +01:00
|
|
|
showNetworkDropdown: () => dispatch(actions.showNetworkDropdown()),
|
|
|
|
hideNetworkDropdown: () => dispatch(actions.hideNetworkDropdown()),
|
2017-08-13 19:49:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inherits(NetworkDropdown, Component)
|
|
|
|
function NetworkDropdown () {
|
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
NetworkDropdown.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
2018-04-19 02:34:28 +02:00
|
|
|
module.exports = compose(
|
|
|
|
withRouter,
|
|
|
|
connect(mapStateToProps, mapDispatchToProps)
|
|
|
|
)(NetworkDropdown)
|
2017-08-13 22:18:08 +02:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
|
2017-08-13 19:49:41 +02:00
|
|
|
// TODO: specify default props and proptypes
|
|
|
|
NetworkDropdown.prototype.render = function () {
|
|
|
|
const props = this.props
|
|
|
|
const { provider: { type: providerType, rpcTarget: activeNetwork } } = props
|
|
|
|
const rpcList = props.frequentRpcList
|
2017-08-13 22:15:21 +02:00
|
|
|
const isOpen = this.props.networkDropdownOpen
|
2017-08-31 13:08:11 +02:00
|
|
|
const dropdownMenuItemStyle = {
|
|
|
|
fontSize: '16px',
|
|
|
|
lineHeight: '20px',
|
2017-10-18 07:42:56 +02:00
|
|
|
padding: '12px 0',
|
2017-09-05 00:55:14 +02:00
|
|
|
}
|
2017-08-13 22:15:21 +02:00
|
|
|
|
2017-08-13 19:49:41 +02:00
|
|
|
return h(Dropdown, {
|
|
|
|
isOpen,
|
|
|
|
onClickOutside: (event) => {
|
|
|
|
const { classList } = event.target
|
2017-12-01 23:19:53 +01:00
|
|
|
const isInClassList = className => classList.contains(className)
|
|
|
|
const notToggleElementIndex = R.findIndex(isInClassList)(notToggleElementClassnames)
|
2017-08-13 19:49:41 +02:00
|
|
|
|
2017-12-01 23:19:53 +01:00
|
|
|
if (notToggleElementIndex === -1) {
|
2017-08-13 22:15:21 +02:00
|
|
|
this.props.hideNetworkDropdown()
|
2017-08-13 19:49:41 +02:00
|
|
|
}
|
|
|
|
},
|
2017-09-05 00:55:14 +02:00
|
|
|
containerClassName: 'network-droppo',
|
2017-11-09 17:47:10 +01:00
|
|
|
zIndex: 55,
|
2017-08-13 19:49:41 +02:00
|
|
|
style: {
|
|
|
|
position: 'absolute',
|
2017-10-19 23:10:29 +02:00
|
|
|
top: '58px',
|
2018-03-01 22:12:10 +01:00
|
|
|
width: '309px',
|
2017-11-09 17:47:10 +01:00
|
|
|
zIndex: '55px',
|
2017-08-13 19:49:41 +02:00
|
|
|
},
|
|
|
|
innerStyle: {
|
2017-10-18 07:42:56 +02:00
|
|
|
padding: '18px 8px',
|
2017-08-13 19:49:41 +02:00
|
|
|
},
|
|
|
|
}, [
|
|
|
|
|
2017-09-07 00:56:54 +02:00
|
|
|
h('div.network-dropdown-header', {}, [
|
2018-03-29 17:00:44 +02:00
|
|
|
h('div.network-dropdown-title', {}, this.context.t('networks')),
|
2017-09-07 00:56:54 +02:00
|
|
|
|
|
|
|
h('div.network-dropdown-divider'),
|
|
|
|
|
|
|
|
h('div.network-dropdown-content',
|
|
|
|
{},
|
2018-03-29 17:00:44 +02:00
|
|
|
this.context.t('defaultNetwork')
|
2017-09-07 00:56:54 +02:00
|
|
|
),
|
|
|
|
]),
|
|
|
|
|
2017-08-13 19:49:41 +02:00
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
key: 'main',
|
2017-08-13 22:15:21 +02:00
|
|
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
|
|
|
onClick: () => props.setProviderType('mainnet'),
|
2017-10-13 00:46:09 +02:00
|
|
|
style: { ...dropdownMenuItemStyle, borderColor: '#038789' },
|
2017-08-13 19:49:41 +02:00
|
|
|
},
|
|
|
|
[
|
2017-09-07 00:57:29 +02:00
|
|
|
providerType === 'mainnet' ? h('i.fa.fa-check') : h('.network-check__transparent', '✓'),
|
2017-09-07 00:37:48 +02:00
|
|
|
h(NetworkDropdownIcon, {
|
2018-03-07 19:52:10 +01:00
|
|
|
backgroundColor: '#29B6AF', // $java
|
2017-09-07 00:37:48 +02:00
|
|
|
isSelected: providerType === 'mainnet',
|
|
|
|
}),
|
2017-09-05 00:55:14 +02:00
|
|
|
h('span.network-name-item', {
|
2017-08-31 13:08:11 +02:00
|
|
|
style: {
|
2017-09-05 00:55:14 +02:00
|
|
|
color: providerType === 'mainnet' ? '#ffffff' : '#9b9b9b',
|
2017-08-31 13:08:11 +02:00
|
|
|
},
|
2018-03-29 17:00:44 +02:00
|
|
|
}, this.context.t('mainnet')),
|
2017-08-13 19:49:41 +02:00
|
|
|
]
|
|
|
|
),
|
|
|
|
|
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
key: 'ropsten',
|
2017-08-13 22:15:21 +02:00
|
|
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
|
|
|
onClick: () => props.setProviderType('ropsten'),
|
2017-08-31 13:08:11 +02:00
|
|
|
style: dropdownMenuItemStyle,
|
2017-08-13 19:49:41 +02:00
|
|
|
},
|
|
|
|
[
|
2017-09-07 00:57:29 +02:00
|
|
|
providerType === 'ropsten' ? h('i.fa.fa-check') : h('.network-check__transparent', '✓'),
|
2017-09-07 00:37:48 +02:00
|
|
|
h(NetworkDropdownIcon, {
|
2018-03-07 19:52:10 +01:00
|
|
|
backgroundColor: '#ff4a8d', // $wild-strawberry
|
2017-09-07 00:37:48 +02:00
|
|
|
isSelected: providerType === 'ropsten',
|
|
|
|
}),
|
2017-09-05 00:55:14 +02:00
|
|
|
h('span.network-name-item', {
|
2017-08-31 13:08:11 +02:00
|
|
|
style: {
|
2017-09-05 00:55:14 +02:00
|
|
|
color: providerType === 'ropsten' ? '#ffffff' : '#9b9b9b',
|
2017-08-31 13:08:11 +02:00
|
|
|
},
|
2018-03-29 17:00:44 +02:00
|
|
|
}, this.context.t('ropsten')),
|
2017-08-13 19:49:41 +02:00
|
|
|
]
|
|
|
|
),
|
|
|
|
|
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
key: 'kovan',
|
2017-08-13 22:15:21 +02:00
|
|
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
|
|
|
onClick: () => props.setProviderType('kovan'),
|
2017-08-31 13:08:11 +02:00
|
|
|
style: dropdownMenuItemStyle,
|
2017-08-13 19:49:41 +02:00
|
|
|
},
|
|
|
|
[
|
2017-09-07 00:57:29 +02:00
|
|
|
providerType === 'kovan' ? h('i.fa.fa-check') : h('.network-check__transparent', '✓'),
|
2017-09-07 00:37:48 +02:00
|
|
|
h(NetworkDropdownIcon, {
|
2018-03-07 19:52:10 +01:00
|
|
|
backgroundColor: '#7057ff', // $cornflower-blue
|
2017-09-07 00:37:48 +02:00
|
|
|
isSelected: providerType === 'kovan',
|
|
|
|
}),
|
2017-09-05 00:55:14 +02:00
|
|
|
h('span.network-name-item', {
|
2017-08-31 13:08:11 +02:00
|
|
|
style: {
|
2017-09-05 00:55:14 +02:00
|
|
|
color: providerType === 'kovan' ? '#ffffff' : '#9b9b9b',
|
2017-08-31 13:08:11 +02:00
|
|
|
},
|
2018-03-29 17:00:44 +02:00
|
|
|
}, this.context.t('kovan')),
|
2017-08-13 19:49:41 +02:00
|
|
|
]
|
|
|
|
),
|
|
|
|
|
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
key: 'rinkeby',
|
2017-08-13 22:15:21 +02:00
|
|
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
2017-08-29 16:50:48 +02:00
|
|
|
onClick: () => props.setProviderType('rinkeby'),
|
2017-08-31 13:08:11 +02:00
|
|
|
style: dropdownMenuItemStyle,
|
2017-08-13 19:49:41 +02:00
|
|
|
},
|
|
|
|
[
|
2017-09-07 00:57:29 +02:00
|
|
|
providerType === 'rinkeby' ? h('i.fa.fa-check') : h('.network-check__transparent', '✓'),
|
2017-09-07 00:37:48 +02:00
|
|
|
h(NetworkDropdownIcon, {
|
2018-03-07 19:52:10 +01:00
|
|
|
backgroundColor: '#f6c343', // $saffron
|
2017-09-07 00:37:48 +02:00
|
|
|
isSelected: providerType === 'rinkeby',
|
|
|
|
}),
|
2017-09-05 00:55:14 +02:00
|
|
|
h('span.network-name-item', {
|
2017-08-31 13:08:11 +02:00
|
|
|
style: {
|
2017-09-05 00:55:14 +02:00
|
|
|
color: providerType === 'rinkeby' ? '#ffffff' : '#9b9b9b',
|
2017-08-31 13:08:11 +02:00
|
|
|
},
|
2018-03-29 17:00:44 +02:00
|
|
|
}, this.context.t('rinkeby')),
|
2017-08-13 19:49:41 +02:00
|
|
|
]
|
|
|
|
),
|
|
|
|
|
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
key: 'default',
|
2017-08-13 22:15:21 +02:00
|
|
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
2018-03-30 19:59:39 +02:00
|
|
|
onClick: () => props.setProviderType('localhost'),
|
2017-08-31 13:08:11 +02:00
|
|
|
style: dropdownMenuItemStyle,
|
2017-08-13 19:49:41 +02:00
|
|
|
},
|
|
|
|
[
|
2018-03-30 19:59:39 +02:00
|
|
|
providerType === 'localhost' ? h('i.fa.fa-check') : h('.network-check__transparent', '✓'),
|
2017-09-07 00:37:48 +02:00
|
|
|
h(NetworkDropdownIcon, {
|
2018-03-30 19:59:39 +02:00
|
|
|
isSelected: providerType === 'localhost',
|
2017-09-07 00:37:48 +02:00
|
|
|
innerBorder: '1px solid #9b9b9b',
|
|
|
|
}),
|
2017-09-05 00:55:14 +02:00
|
|
|
h('span.network-name-item', {
|
2017-08-31 13:08:11 +02:00
|
|
|
style: {
|
2018-03-30 19:59:39 +02:00
|
|
|
color: providerType === 'localhost' ? '#ffffff' : '#9b9b9b',
|
2017-08-31 13:08:11 +02:00
|
|
|
},
|
2018-03-29 17:00:44 +02:00
|
|
|
}, this.context.t('localhost')),
|
2017-08-13 19:49:41 +02:00
|
|
|
]
|
|
|
|
),
|
|
|
|
|
|
|
|
this.renderCustomOption(props.provider),
|
|
|
|
this.renderCommonRpc(rpcList, props.provider),
|
|
|
|
|
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
2017-08-13 22:15:21 +02:00
|
|
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
2018-04-19 02:34:28 +02:00
|
|
|
onClick: () => this.props.history.push(SETTINGS_ROUTE),
|
2017-08-31 13:08:11 +02:00
|
|
|
style: dropdownMenuItemStyle,
|
2017-08-13 19:49:41 +02:00
|
|
|
},
|
|
|
|
[
|
2017-09-07 00:57:29 +02:00
|
|
|
activeNetwork === 'custom' ? h('i.fa.fa-check') : h('.network-check__transparent', '✓'),
|
2017-09-07 00:37:48 +02:00
|
|
|
h(NetworkDropdownIcon, {
|
|
|
|
isSelected: activeNetwork === 'custom',
|
|
|
|
innerBorder: '1px solid #9b9b9b',
|
|
|
|
}),
|
2017-09-05 00:55:14 +02:00
|
|
|
h('span.network-name-item', {
|
2017-08-31 13:08:11 +02:00
|
|
|
style: {
|
2017-09-05 00:55:14 +02:00
|
|
|
color: activeNetwork === 'custom' ? '#ffffff' : '#9b9b9b',
|
2017-08-31 13:08:11 +02:00
|
|
|
},
|
2018-03-29 17:00:44 +02:00
|
|
|
}, this.context.t('customRPC')),
|
2017-08-13 19:49:41 +02:00
|
|
|
]
|
|
|
|
),
|
|
|
|
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NetworkDropdown.prototype.getNetworkName = function () {
|
|
|
|
const { provider } = this.props
|
|
|
|
const providerName = provider.type
|
|
|
|
|
|
|
|
let name
|
|
|
|
|
|
|
|
if (providerName === 'mainnet') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('mainnet')
|
2017-08-13 19:49:41 +02:00
|
|
|
} else if (providerName === 'ropsten') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('ropsten')
|
2017-08-13 19:49:41 +02:00
|
|
|
} else if (providerName === 'kovan') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('kovan')
|
2017-08-13 19:49:41 +02:00
|
|
|
} else if (providerName === 'rinkeby') {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('rinkeby')
|
2017-08-13 19:49:41 +02:00
|
|
|
} else {
|
2018-03-29 17:00:44 +02:00
|
|
|
name = this.context.t('unknownNetwork')
|
2017-08-13 19:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkDropdown.prototype.renderCommonRpc = function (rpcList, provider) {
|
|
|
|
const props = this.props
|
2018-08-23 00:26:30 +02:00
|
|
|
const reversedRpcList = rpcList.slice().reverse()
|
2017-08-13 19:49:41 +02:00
|
|
|
|
2018-08-23 00:26:30 +02:00
|
|
|
return reversedRpcList.map((rpc) => {
|
2018-08-19 20:02:30 +02:00
|
|
|
const currentRpcTarget = provider.type === 'rpc' && rpc === provider.rpcTarget
|
2018-08-19 19:25:33 +02:00
|
|
|
|
|
|
|
if ((rpc === 'http://localhost:8545') || currentRpcTarget) {
|
2017-08-13 19:49:41 +02:00
|
|
|
return null
|
|
|
|
} else {
|
|
|
|
return h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
key: `common${rpc}`,
|
2017-08-13 22:15:21 +02:00
|
|
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
|
|
|
onClick: () => props.setRpcTarget(rpc),
|
2018-03-01 22:12:10 +01:00
|
|
|
style: {
|
|
|
|
fontSize: '16px',
|
|
|
|
lineHeight: '20px',
|
|
|
|
padding: '12px 0',
|
|
|
|
},
|
2017-08-13 19:49:41 +02:00
|
|
|
},
|
|
|
|
[
|
2018-08-19 19:25:33 +02:00
|
|
|
currentRpcTarget ? h('i.fa.fa-check') : h('.network-check__transparent', '✓'),
|
2018-03-01 22:12:10 +01:00
|
|
|
h('i.fa.fa-question-circle.fa-med.menu-icon-circle'),
|
|
|
|
h('span.network-name-item', {
|
|
|
|
style: {
|
2018-08-19 19:25:33 +02:00
|
|
|
color: currentRpcTarget ? '#ffffff' : '#9b9b9b',
|
2018-03-01 22:12:10 +01:00
|
|
|
},
|
|
|
|
}, rpc),
|
2017-08-13 19:49:41 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
2018-08-23 00:26:30 +02:00
|
|
|
})
|
2017-08-13 19:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NetworkDropdown.prototype.renderCustomOption = function (provider) {
|
|
|
|
const { rpcTarget, type } = provider
|
|
|
|
const props = this.props
|
|
|
|
|
|
|
|
if (type !== 'rpc') return null
|
|
|
|
|
|
|
|
switch (rpcTarget) {
|
|
|
|
|
|
|
|
case 'http://localhost:8545':
|
|
|
|
return null
|
|
|
|
|
|
|
|
default:
|
|
|
|
return h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
key: rpcTarget,
|
2017-08-13 22:15:21 +02:00
|
|
|
onClick: () => props.setRpcTarget(rpcTarget),
|
|
|
|
closeMenu: () => this.props.hideNetworkDropdown(),
|
2018-03-01 22:12:10 +01:00
|
|
|
style: {
|
|
|
|
fontSize: '16px',
|
|
|
|
lineHeight: '20px',
|
|
|
|
padding: '12px 0',
|
|
|
|
},
|
2017-08-13 19:49:41 +02:00
|
|
|
},
|
|
|
|
[
|
2018-03-01 22:12:10 +01:00
|
|
|
h('i.fa.fa-check'),
|
|
|
|
h('i.fa.fa-question-circle.fa-med.menu-icon-circle'),
|
|
|
|
h('span.network-name-item', {
|
|
|
|
style: {
|
|
|
|
color: '#ffffff',
|
|
|
|
},
|
|
|
|
}, rpcTarget),
|
2017-08-13 19:49:41 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|