1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-12 12:47:14 +01:00
metamask-extension/ui/app/components/pages/create-account/connect-hardware/account-list.js

206 lines
5.9 KiB
JavaScript
Raw Normal View History

2018-07-05 23:45:28 +02:00
const { Component } = require('react')
const PropTypes = require('prop-types')
const h = require('react-hyperscript')
2018-07-06 02:59:31 +02:00
const genAccountLink = require('../../../../../lib/account-link.js')
2018-08-14 01:29:43 +02:00
const Select = require('react-select').default
import Button from '../../../button'
2018-07-05 23:45:28 +02:00
2018-07-06 02:59:31 +02:00
class AccountList extends Component {
2018-07-05 23:45:28 +02:00
constructor (props, context) {
super(props)
}
2018-08-14 01:29:43 +02:00
getHdPaths () {
return [
{
2018-08-14 07:26:18 +02:00
label: `Ledger Live`,
value: `m/44'/60'/0'/0/0`,
2018-08-14 01:29:43 +02:00
},
{
2018-08-14 07:26:18 +02:00
label: `Legacy (MEW / MyCrypto)`,
value: `m/44'/60'/0'`,
2018-08-14 01:29:43 +02:00
},
]
}
2018-08-14 07:26:18 +02:00
goToNextPage = () => {
2018-08-14 09:42:23 +02:00
// If we have < 5 accounts, it's restricted by BIP-44
if (this.props.accounts.length === 5) {
2018-08-14 07:26:18 +02:00
this.props.getPage(this.props.device, 1, this.props.selectedPath)
} else {
this.props.onAccountRestriction()
}
}
goToPreviousPage = () => {
this.props.getPage(this.props.device, -1, this.props.selectedPath)
}
2018-08-14 01:29:43 +02:00
renderHdPathSelector () {
const { onPathChange, selectedPath } = this.props
const options = this.getHdPaths()
2018-08-14 07:26:18 +02:00
return h('div', [
2018-08-17 02:41:23 +02:00
h('h3.hw-connect__hdPath__title', {}, this.context.t('selectHdPath')),
h('p.hw-connect__msg', {}, this.context.t('selectPathHelp')),
2018-08-14 07:26:18 +02:00
h('div.hw-connect__hdPath', [
h(Select, {
className: 'hw-connect__hdPath__select',
name: 'hd-path-select',
clearable: false,
value: selectedPath,
options,
onChange: (opt) => {
onPathChange(opt.value)
},
}),
]),
2018-08-14 01:29:43 +02:00
])
}
2018-08-17 02:41:23 +02:00
capitalizeDevice (device) {
return device.slice(0, 1).toUpperCase() + device.slice(1)
}
renderHeader () {
2018-08-14 01:29:43 +02:00
const { device } = this.props
return (
h('div.hw-connect', [
2018-08-14 01:29:43 +02:00
2018-08-17 02:41:23 +02:00
h('h3.hw-connect__unlock-title', {}, `${this.context.t('unlock')} ${this.capitalizeDevice(device)}`),
2018-08-14 01:29:43 +02:00
device.toLowerCase() === 'ledger' ? this.renderHdPathSelector() : null,
2018-08-17 02:41:23 +02:00
h('h3.hw-connect__hdPath__title', {}, this.context.t('selectAnAccount')),
h('p.hw-connect__msg', {}, this.context.t('selectAnAccountHelp')),
])
)
}
2018-07-05 23:45:28 +02:00
renderAccounts () {
return h('div.hw-account-list', [
2018-07-06 02:59:31 +02:00
this.props.accounts.map((a, i) => {
2018-07-05 23:45:28 +02:00
2018-07-06 02:59:31 +02:00
return h('div.hw-account-list__item', { key: a.address }, [
h('div.hw-account-list__item__radio', [
h('input', {
type: 'radio',
name: 'selectedAccount',
id: `address-${i}`,
value: a.index,
onChange: (e) => this.props.onAccountChange(e.target.value),
checked: this.props.selectedAccount === a.index.toString(),
}),
h(
'label.hw-account-list__item__label',
{
htmlFor: `address-${i}`,
},
[
h('span.hw-account-list__item__index', a.index + 1),
`${a.address.slice(0, 4)}...${a.address.slice(-4)}`,
h('span.hw-account-list__item__balance', `${a.balance}`),
]),
2018-07-06 02:59:31 +02:00
]),
2018-07-05 23:45:28 +02:00
h(
2018-07-06 02:59:31 +02:00
'a.hw-account-list__item__link',
{
href: genAccountLink(a.address, this.props.network),
target: '_blank',
title: this.context.t('etherscanView'),
},
h('img', { src: 'images/popout.svg' })
2018-07-05 23:45:28 +02:00
),
2018-07-06 02:59:31 +02:00
])
}),
2018-07-05 23:45:28 +02:00
])
}
renderPagination () {
return h('div.hw-list-pagination', [
h(
2018-07-19 08:31:13 +02:00
'button.hw-list-pagination__button',
2018-07-05 23:45:28 +02:00
{
2018-08-14 07:26:18 +02:00
onClick: this.goToPreviousPage,
2018-07-05 23:45:28 +02:00
},
`< ${this.context.t('prev')}`
),
h(
2018-07-19 08:31:13 +02:00
'button.hw-list-pagination__button',
2018-07-05 23:45:28 +02:00
{
2018-08-14 07:26:18 +02:00
onClick: this.goToNextPage,
2018-07-05 23:45:28 +02:00
},
`${this.context.t('next')} >`
),
])
}
renderButtons () {
const disabled = this.props.selectedAccount === null
const buttonProps = {}
if (disabled) {
buttonProps.disabled = true
}
return h('div.new-account-connect-form__buttons', {}, [
h(Button, {
type: 'default',
large: true,
className: 'new-account-connect-form__button',
onClick: this.props.onCancel.bind(this),
}, [this.context.t('cancel')]),
h(Button, {
type: 'confirm',
large: true,
className: 'new-account-connect-form__button unlock',
disabled,
onClick: this.props.onUnlockAccount.bind(this, this.props.device),
}, [this.context.t('unlock')]),
2018-07-05 23:45:28 +02:00
])
}
renderForgetDevice () {
return h('div.hw-forget-device-container', {}, [
h('a', {
onClick: this.props.onForgetDevice.bind(this, this.props.device),
}, this.context.t('forgetDevice')),
])
}
2018-07-05 23:45:28 +02:00
render () {
2018-07-19 08:31:13 +02:00
return h('div.new-account-connect-form.account-list', {}, [
this.renderHeader(),
2018-07-06 02:59:31 +02:00
this.renderAccounts(),
this.renderPagination(),
this.renderButtons(),
this.renderForgetDevice(),
2018-07-06 02:59:31 +02:00
])
2018-07-05 23:45:28 +02:00
}
}
AccountList.propTypes = {
2018-08-14 01:29:43 +02:00
onPathChange: PropTypes.func.isRequired,
selectedPath: PropTypes.string.isRequired,
device: PropTypes.string.isRequired,
2018-07-06 02:59:31 +02:00
accounts: PropTypes.array.isRequired,
2018-07-05 23:45:28 +02:00
onAccountChange: PropTypes.func.isRequired,
onForgetDevice: PropTypes.func.isRequired,
2018-07-05 23:45:28 +02:00
getPage: PropTypes.func.isRequired,
network: PropTypes.string,
2018-07-06 02:59:31 +02:00
selectedAccount: PropTypes.string,
2018-07-05 23:45:28 +02:00
history: PropTypes.object,
2018-07-06 02:59:31 +02:00
onUnlockAccount: PropTypes.func,
onCancel: PropTypes.func,
2018-08-14 07:26:18 +02:00
onAccountRestriction: PropTypes.func,
2018-07-05 23:45:28 +02:00
}
AccountList.contextTypes = {
t: PropTypes.func,
}
2018-07-06 02:59:31 +02:00
module.exports = AccountList