1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-28 05:12:18 +01:00
metamask-extension/ui/app/components/account-menu/index.js

186 lines
5.2 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 { compose } = require('recompose')
const { withRouter } = require('react-router-dom')
2018-03-31 01:18:48 +02:00
const PropTypes = require('prop-types')
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')
const {
SETTINGS_ROUTE,
INFO_ROUTE,
NEW_ACCOUNT_ROUTE,
IMPORT_ACCOUNT_ROUTE,
DEFAULT_ROUTE,
} = require('../../routes')
2017-10-13 08:10:58 +02:00
module.exports = compose(
withRouter,
connect(mapStateToProps, mapDispatchToProps)
)(AccountMenu)
2017-10-13 08:10:58 +02:00
AccountMenu.contextTypes = {
t: PropTypes.func,
}
2017-10-13 08:10:58 +02:00
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())
},
2017-10-25 18:26:05 +02:00
showInfoPage: () => {
dispatch(actions.showInfoPage())
dispatch(actions.hideSidebar())
2017-10-18 07:36:53 +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,
lockMetamask,
history,
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',
}, [
this.context.t('myAccounts'),
2017-10-24 09:16:19 +02:00
h('button.account-menu__logout-button', {
2017-12-04 07:24:30 +01:00
onClick: () => {
lockMetamask()
history.push(DEFAULT_ROUTE)
},
}, this.context.t('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: () => {
toggleAccountMenu()
history.push(NEW_ACCOUNT_ROUTE)
},
icon: h('img.account-menu__item-icon', { src: 'images/plus-btn-white.svg' }),
text: this.context.t('createAccount'),
2017-10-13 08:10:58 +02:00
}),
h(Item, {
onClick: () => {
toggleAccountMenu()
history.push(IMPORT_ACCOUNT_ROUTE)
},
icon: h('img.account-menu__item-icon', { src: 'images/import-account.svg' }),
text: this.context.t('importAccount'),
2017-10-13 08:10:58 +02:00
}),
h(Divider),
h(Item, {
onClick: () => {
toggleAccountMenu()
history.push(INFO_ROUTE)
},
2017-10-13 08:10:58 +02:00
icon: h('img', { src: 'images/mm-info-icon.svg' }),
text: this.context.t('infoHelp'),
2017-10-13 08:10:58 +02:00
}),
h(Item, {
onClick: () => {
toggleAccountMenu()
history.push(SETTINGS_ROUTE)
},
icon: h('img.account-menu__item-icon', { src: 'images/settings.svg' }),
text: this.context.t('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
2018-06-05 00:03:03 +02:00
const accountOrder = keyrings.reduce((list, keyring) => list.concat(keyring.accounts), [])
return accountOrder.map((address) => {
const identity = identities[address]
2017-10-24 09:13:49 +02:00
const isSelected = identity.address === selectedAddress
2017-10-16 07:28:25 +02:00
2018-06-05 00:03:03 +02:00
const balanceValue = accounts[address] ? accounts[address].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'
return isLoose ? h('.keyring-label.allcaps', this.context.t('imported')) : null
2017-10-16 07:28:25 +02:00
} catch (e) { return }
}