1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 20:05:27 +02:00
metamask-extension/ui/app/components/account-menu/index.js

161 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-10-13 08:10:58 +02:00
const inherits = require('util').inherits
const Component = require('react').Component
const connect = require('react-redux').connect
const h = require('react-hyperscript')
const actions = require('../../actions')
2017-10-16 07:28:25 +02:00
const { Menu, Item, Divider, CloseArea } = require('../dropdowns/components/menu')
const Identicon = require('../identicon')
const { formatBalance } = require('../../util')
2017-10-13 08:10:58 +02:00
module.exports = connect(mapStateToProps, mapDispatchToProps)(AccountMenu)
inherits(AccountMenu, Component)
function AccountMenu () { Component.call(this) }
function mapStateToProps (state) {
return {
selectedAddress: state.metamask.selectedAddress,
2017-10-16 07:28:25 +02:00
isAccountMenuOpen: state.metamask.isAccountMenuOpen,
keyrings: state.metamask.keyrings,
identities: state.metamask.identities,
accounts: state.metamask.accounts,
2017-10-13 08:10:58 +02:00
}
}
2017-10-16 07:28:25 +02:00
function mapDispatchToProps (dispatch) {
return {
toggleAccountMenu: () => dispatch(actions.toggleAccountMenu()),
2017-10-18 07:36:53 +02:00
showAccountDetail: address => {
dispatch(actions.showAccountDetail(address))
dispatch(actions.toggleAccountMenu())
},
lockMetamask: () => {
dispatch(actions.lockMetamask())
2017-10-26 19:15:05 +02:00
dispatch(actions.displayWarning(null))
2017-10-18 07:36:53 +02:00
dispatch(actions.toggleAccountMenu())
},
showConfigPage: () => {
dispatch(actions.showConfigPage())
dispatch(actions.toggleAccountMenu())
},
showNewAccountModal: () => {
dispatch(actions.showModal({ name: 'NEW_ACCOUNT' }))
dispatch(actions.toggleAccountMenu())
},
showImportPage: () => {
dispatch(actions.showImportPage())
dispatch(actions.toggleAccountMenu())
},
2017-10-25 18:26:05 +02:00
showInfoPage: () => {
dispatch(actions.showInfoPage())
dispatch(actions.toggleAccountMenu())
},
2017-10-16 07:28:25 +02:00
}
2017-10-13 08:10:58 +02:00
}
AccountMenu.prototype.render = function () {
2017-10-18 07:36:53 +02:00
const {
isAccountMenuOpen,
toggleAccountMenu,
showNewAccountModal,
showImportPage,
lockMetamask,
showConfigPage,
2017-10-25 18:26:05 +02:00
showInfoPage,
2017-10-18 07:36:53 +02:00
} = this.props
2017-10-16 07:28:25 +02:00
return h(Menu, { className: 'account-menu', isShowing: isAccountMenuOpen }, [
h(CloseArea, { onClick: toggleAccountMenu }),
2017-10-18 07:36:53 +02:00
h(Item, {
className: 'account-menu__header',
}, [
2017-10-13 08:10:58 +02:00
'My Accounts',
2017-10-24 09:16:19 +02:00
h('button.account-menu__logout-button', {
onClick: lockMetamask,
}, 'Log out'),
2017-10-13 08:10:58 +02:00
]),
h(Divider),
2017-10-16 07:28:25 +02:00
h('div.account-menu__accounts', this.renderAccounts()),
2017-10-13 08:10:58 +02:00
h(Divider),
h(Item, {
2017-10-18 07:36:53 +02:00
onClick: showNewAccountModal,
2017-10-13 08:10:58 +02:00
icon: h('img', { src: 'images/plus-btn-white.svg' }),
text: 'Create Account',
}),
h(Item, {
2017-10-18 07:36:53 +02:00
onClick: showImportPage,
2017-10-13 08:10:58 +02:00
icon: h('img', { src: 'images/import-account.svg' }),
text: 'Import Account',
}),
h(Divider),
h(Item, {
2017-10-25 18:26:05 +02:00
onClick: showInfoPage,
2017-10-13 08:10:58 +02:00
icon: h('img', { src: 'images/mm-info-icon.svg' }),
text: 'Info & Help',
}),
h(Item, {
2017-10-18 07:36:53 +02:00
onClick: showConfigPage,
2017-10-13 08:10:58 +02:00
icon: h('img', { src: 'images/settings.svg' }),
text: 'Settings',
}),
])
}
2017-10-16 07:28:25 +02:00
AccountMenu.prototype.renderAccounts = function () {
2017-10-18 07:36:53 +02:00
const {
identities,
accounts,
2017-10-24 09:13:49 +02:00
selectedAddress,
2017-10-18 07:36:53 +02:00
keyrings,
showAccountDetail,
} = this.props
2017-10-16 07:28:25 +02:00
return Object.keys(identities).map((key, index) => {
const identity = identities[key]
2017-10-24 09:13:49 +02:00
const isSelected = identity.address === selectedAddress
2017-10-16 07:28:25 +02:00
2017-10-19 21:47:44 +02:00
const balanceValue = accounts[key] ? accounts[key].balance : ''
2017-10-16 07:28:25 +02:00
const formattedBalance = balanceValue ? formatBalance(balanceValue, 6) : '...'
const simpleAddress = identity.address.substring(2).toLowerCase()
const keyring = keyrings.find((kr) => {
return kr.accounts.includes(simpleAddress) ||
kr.accounts.includes(identity.address)
})
return h(
2017-10-18 07:36:53 +02:00
'div.account-menu__account.menu__item--clickable',
{ onClick: () => showAccountDetail(identity.address) },
2017-10-16 07:28:25 +02:00
[
h('div.account-menu__check-mark', [
2017-10-24 09:13:49 +02:00
isSelected ? h('div.account-menu__check-mark-icon') : null,
2017-10-16 07:28:25 +02:00
]),
2017-10-13 08:10:58 +02:00
2017-10-16 07:28:25 +02:00
h(
Identicon,
{
address: identity.address,
diameter: 24,
},
),
h('div.account-menu__account-info', [
2017-10-18 07:36:53 +02:00
h('div.account-menu__name', identity.name || ''),
2017-10-16 07:28:25 +02:00
h('div.account-menu__balance', formattedBalance),
]),
2017-10-18 07:36:53 +02:00
this.indicateIfLoose(keyring),
2017-10-16 07:28:25 +02:00
],
)
})
}
AccountMenu.prototype.indicateIfLoose = function (keyring) {
try { // Sometimes keyrings aren't loaded yet:
const type = keyring.type
const isLoose = type !== 'HD Key Tree'
2017-10-24 08:11:47 +02:00
return isLoose ? h('.keyring-label', 'IMPORTED') : null
2017-10-16 07:28:25 +02:00
} catch (e) { return }
}