2017-07-18 14:25:16 +02:00
|
|
|
const Component = require('react').Component
|
|
|
|
const PropTypes = require('react').PropTypes
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const actions = require('../actions')
|
|
|
|
const genAccountLink = require('../../lib/account-link.js')
|
|
|
|
const connect = require('react-redux').connect
|
|
|
|
const Dropdown = require('./dropdown').Dropdown
|
|
|
|
const DropdownMenuItem = require('./dropdown').DropdownMenuItem
|
|
|
|
const Identicon = require('./identicon')
|
|
|
|
const ethUtil = require('ethereumjs-util')
|
|
|
|
const copyToClipboard = require('copy-to-clipboard')
|
|
|
|
|
|
|
|
class AccountDropdowns extends Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {
|
2017-07-18 18:29:52 +02:00
|
|
|
accountSelectorActive: false,
|
|
|
|
optionsMenuActive: false,
|
2017-07-18 14:25:16 +02:00
|
|
|
}
|
2017-07-29 00:31:03 +02:00
|
|
|
this.accountSelectorToggleClassName = 'fa-angle-down';
|
|
|
|
this.optionsMenuToggleClassName = 'fa-ellipsis-h';
|
2017-07-18 14:25:16 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 18:29:52 +02:00
|
|
|
renderAccounts () {
|
2017-07-18 14:25:16 +02:00
|
|
|
const { identities, selected } = this.props
|
|
|
|
|
|
|
|
return Object.keys(identities).map((key) => {
|
|
|
|
const identity = identities[key]
|
|
|
|
const isSelected = identity.address === selected
|
|
|
|
|
|
|
|
return h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
closeMenu: () => {},
|
|
|
|
onClick: () => {
|
|
|
|
this.props.actions.showAccountDetail(identity.address)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[
|
|
|
|
h(
|
|
|
|
Identicon,
|
|
|
|
{
|
|
|
|
address: identity.address,
|
|
|
|
diameter: 16,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
h('span', { style: { marginLeft: '10px' } }, identity.name || ''),
|
|
|
|
h('span', { style: { marginLeft: '10px' } }, isSelected ? h('.check', '✓') : null),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-18 18:29:52 +02:00
|
|
|
renderAccountSelector () {
|
|
|
|
const { actions } = this.props
|
|
|
|
const { accountSelectorActive } = this.state
|
|
|
|
|
|
|
|
return h(
|
|
|
|
Dropdown,
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
marginLeft: '-125px',
|
|
|
|
minWidth: '180px',
|
2017-07-26 21:43:18 +02:00
|
|
|
overflowY: 'auto',
|
|
|
|
maxHeight: '300px',
|
2017-07-18 18:29:52 +02:00
|
|
|
},
|
|
|
|
isOpen: accountSelectorActive,
|
2017-07-29 00:31:03 +02:00
|
|
|
onClickOutside: (event) => {
|
|
|
|
const { classList } = event.target
|
|
|
|
const isNotToggleElement = !classList.contains(this.accountSelectorToggleClassName)
|
|
|
|
if (accountSelectorActive && isNotToggleElement) {
|
|
|
|
this.setState({ accountSelectorActive: false })
|
|
|
|
}
|
|
|
|
},
|
2017-07-18 18:29:52 +02:00
|
|
|
},
|
|
|
|
[
|
|
|
|
...this.renderAccounts(),
|
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
closeMenu: () => {},
|
|
|
|
onClick: () => actions.addNewAccount(),
|
|
|
|
},
|
|
|
|
[
|
|
|
|
h(
|
|
|
|
Identicon,
|
|
|
|
{
|
|
|
|
diameter: 16,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
h('span', { style: { marginLeft: '10px' } }, 'Create Account'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
closeMenu: () => {},
|
|
|
|
onClick: () => actions.showImportPage(),
|
|
|
|
},
|
|
|
|
[
|
|
|
|
h(
|
|
|
|
Identicon,
|
|
|
|
{
|
|
|
|
diameter: 16,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
h('span', { style: { marginLeft: '10px' } }, 'Import Account'),
|
|
|
|
]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderAccountOptions () {
|
|
|
|
const { actions } = this.props
|
|
|
|
const { optionsMenuActive } = this.state
|
|
|
|
|
|
|
|
return h(
|
|
|
|
Dropdown,
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
marginLeft: '-162px',
|
|
|
|
minWidth: '180px',
|
|
|
|
},
|
|
|
|
isOpen: optionsMenuActive,
|
2017-07-29 00:31:03 +02:00
|
|
|
onClickOutside: () => {
|
|
|
|
const { classList } = event.target
|
|
|
|
const isNotToggleElement = !classList.contains(this.optionsMenuToggleClassName)
|
|
|
|
if (optionsMenuActive && isNotToggleElement) {
|
|
|
|
this.setState({ optionsMenuActive: false })
|
|
|
|
}
|
|
|
|
},
|
2017-07-18 18:29:52 +02:00
|
|
|
},
|
|
|
|
[
|
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
closeMenu: () => {},
|
|
|
|
onClick: () => {
|
|
|
|
const { selected, network } = this.props
|
|
|
|
const url = genAccountLink(selected, network)
|
|
|
|
global.platform.openWindow({ url })
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'View account on Etherscan',
|
|
|
|
),
|
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
closeMenu: () => {},
|
|
|
|
onClick: () => {
|
|
|
|
const { selected } = this.props
|
|
|
|
const checkSumAddress = selected && ethUtil.toChecksumAddress(selected)
|
|
|
|
copyToClipboard(checkSumAddress)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'Copy Address to clipboard',
|
|
|
|
),
|
|
|
|
h(
|
|
|
|
DropdownMenuItem,
|
|
|
|
{
|
|
|
|
closeMenu: () => {},
|
|
|
|
onClick: () => {
|
|
|
|
actions.requestAccountExport()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'Export Private Key',
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-07-18 14:25:16 +02:00
|
|
|
render () {
|
2017-07-18 18:29:52 +02:00
|
|
|
const { style } = this.props
|
|
|
|
const { optionsMenuActive, accountSelectorActive } = this.state
|
2017-07-18 14:25:16 +02:00
|
|
|
|
|
|
|
return h(
|
|
|
|
'span',
|
|
|
|
{
|
|
|
|
style: style,
|
|
|
|
},
|
|
|
|
[
|
|
|
|
h(
|
|
|
|
'i.fa.fa-angle-down',
|
|
|
|
{
|
|
|
|
style: {},
|
|
|
|
onClick: (event) => {
|
|
|
|
event.stopPropagation()
|
|
|
|
this.setState({
|
2017-07-18 18:29:52 +02:00
|
|
|
accountSelectorActive: !accountSelectorActive,
|
|
|
|
optionsMenuActive: false,
|
2017-07-18 14:25:16 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
2017-07-18 18:29:52 +02:00
|
|
|
this.renderAccountSelector(),
|
2017-07-18 14:25:16 +02:00
|
|
|
),
|
|
|
|
h(
|
|
|
|
'i.fa.fa-ellipsis-h',
|
|
|
|
{
|
|
|
|
style: { 'marginLeft': '10px'},
|
|
|
|
onClick: (event) => {
|
|
|
|
event.stopPropagation()
|
|
|
|
this.setState({
|
2017-07-18 18:29:52 +02:00
|
|
|
accountSelectorActive: false,
|
|
|
|
optionsMenuActive: !optionsMenuActive,
|
2017-07-18 14:25:16 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
2017-07-18 18:29:52 +02:00
|
|
|
this.renderAccountOptions()
|
2017-07-18 14:25:16 +02:00
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AccountDropdowns.propTypes = {
|
|
|
|
identities: PropTypes.objectOf(PropTypes.object),
|
|
|
|
selected: PropTypes.string,
|
|
|
|
}
|
|
|
|
|
2017-07-18 14:26:31 +02:00
|
|
|
const mapDispatchToProps = (dispatch) => {
|
2017-07-18 14:25:16 +02:00
|
|
|
return {
|
|
|
|
actions: {
|
|
|
|
showConfigPage: () => dispatch(actions.showConfigPage()),
|
|
|
|
requestAccountExport: () => dispatch(actions.requestExportAccount()),
|
|
|
|
showAccountDetail: (address) => dispatch(actions.showAccountDetail(address)),
|
|
|
|
addNewAccount: () => dispatch(actions.addNewAccount()),
|
|
|
|
showImportPage: () => dispatch(actions.showImportPage()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
AccountDropdowns: connect(null, mapDispatchToProps)(AccountDropdowns),
|
|
|
|
}
|