1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 12:56:01 +01:00
metamask-extension/ui/app/components/dropdowns/network-dropdown.js

345 lines
9.5 KiB
JavaScript
Raw Normal View History

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