1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 19:10:22 +01:00
metamask-extension/ui/app/components/account-menu/index.js

162 lines
4.9 KiB
JavaScript
Raw Normal View History

2017-10-13 08:10:58 +02:00
const inherits = require('util').inherits
const Component = require('react').Component
2018-03-16 01:29:45 +01:00
const connect = require('../../metamask-connect')
2017-10-13 08:10:58 +02:00
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')
2018-03-16 01:29:45 +01:00
const t = require('../../../i18n-helper').getMessage
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.hideSidebar())
2017-10-18 07:36:53 +02:00
dispatch(actions.toggleAccountMenu())
},
lockMetamask: () => {
dispatch(actions.lockMetamask())
dispatch(actions.hideWarning())
dispatch(actions.hideSidebar())
2017-10-18 07:36:53 +02:00
dispatch(actions.toggleAccountMenu())
},
showConfigPage: () => {
dispatch(actions.showConfigPage())
dispatch(actions.hideSidebar())
2017-10-18 07:36:53 +02:00
dispatch(actions.toggleAccountMenu())
},
showNewAccountPage: (formToSelect) => {
dispatch(actions.showNewAccountPage(formToSelect))
dispatch(actions.hideSidebar())
2017-10-18 07:36:53 +02:00
dispatch(actions.toggleAccountMenu())
},
2017-10-25 18:26:05 +02:00
showInfoPage: () => {
dispatch(actions.showInfoPage())
dispatch(actions.hideSidebar())
2017-10-25 18:26:05 +02:00
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,
showNewAccountPage,
2017-10-18 07:36:53 +02:00
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',
}, [
2018-03-16 01:29:45 +01:00
t(this.props.localeMessages, 'myAccounts'),
2017-10-24 09:16:19 +02:00
h('button.account-menu__logout-button', {
onClick: lockMetamask,
2018-03-16 01:29:45 +01:00
}, t(this.props.localeMessages, 'logout')),
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, {
onClick: () => showNewAccountPage('CREATE'),
icon: h('img.account-menu__item-icon', { src: 'images/plus-btn-white.svg' }),
2018-03-16 01:29:45 +01:00
text: t(this.props.localeMessages, 'createAccount'),
2017-10-13 08:10:58 +02:00
}),
h(Item, {
onClick: () => showNewAccountPage('IMPORT'),
icon: h('img.account-menu__item-icon', { src: 'images/import-account.svg' }),
2018-03-16 01:29:45 +01:00
text: t(this.props.localeMessages, 'importAccount'),
2017-10-13 08:10:58 +02:00
}),
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' }),
2018-03-16 01:29:45 +01:00
text: t(this.props.localeMessages, 'infoHelp'),
2017-10-13 08:10:58 +02:00
}),
h(Item, {
2017-10-18 07:36:53 +02:00
onClick: showConfigPage,
icon: h('img.account-menu__item-icon', { src: 'images/settings.svg' }),
2018-03-16 01:29:45 +01:00
text: t(this.props.localeMessages, 'settings'),
2017-10-13 08:10:58 +02:00
}),
])
}
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'
2018-03-16 01:29:45 +01:00
return isLoose ? h('.keyring-label.allcaps', t(this.props.localeMessages, 'imported')) : null
2017-10-16 07:28:25 +02:00
} catch (e) { return }
}