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

270 lines
7.5 KiB
JavaScript
Raw Normal View History

import PropTypes from 'prop-types'
import React, { Component } from 'react'
import Select from 'react-select'
2020-08-14 17:01:59 +02:00
import getAccountLink from '../../../../lib/account-link'
import Button from '../../../components/ui/button'
2018-07-05 23:45:28 +02:00
2018-07-06 02:59:31 +02:00
class AccountList extends Component {
state = {
showCustomInput: false,
customPathSelected: false,
}
inputRef = React.createRef()
2020-11-03 00:41:28 +01:00
getHdPaths() {
return [
{
label: `Ledger Live`,
value: `m/44'/60'/0'/0/0`,
},
{
label: `Legacy (MEW / MyCrypto)`,
value: `m/44'/60'/0'`,
},
{
label: `Custom`,
value: `custom`,
},
]
}
2018-08-14 01:29:43 +02:00
2020-11-03 00:41:28 +01:00
goToNextPage = () => {
// If we have < 5 accounts, it's restricted by BIP-44
if (this.props.accounts.length === 5) {
this.props.getPage(this.props.device, 1, this.props.selectedPath)
} else {
this.props.onAccountRestriction()
2018-08-14 07:26:18 +02:00
}
2020-11-03 00:41:28 +01:00
}
2018-08-14 07:26:18 +02:00
2020-11-03 00:41:28 +01:00
goToPreviousPage = () => {
this.props.getPage(this.props.device, -1, this.props.selectedPath)
}
2018-08-14 07:26:18 +02:00
2020-11-03 00:41:28 +01:00
renderHdPathSelector() {
const { showCustomInput } = this.state
2020-11-03 00:41:28 +01:00
const { onPathChange, selectedPath } = this.props
const options = this.getHdPaths()
return (
<div>
<h3 className="hw-connect__hdPath__title">
{this.context.t('selectHdPath')}
</h3>
<p className="hw-connect__msg">{this.context.t('selectPathHelp')}</p>
<div className="hw-connect__hdPath">
<Select
className="hw-connect__hdPath__select"
name="hd-path-select"
clearable={false}
value={showCustomInput ? 'custom' : selectedPath}
2020-11-03 00:41:28 +01:00
options={options}
onChange={(opt) => {
if (opt.value === 'custom') {
this.setState({ showCustomInput: true })
} else {
this.setState({ showCustomInput: false })
onPathChange(opt.value)
}
2020-11-03 00:41:28 +01:00
}}
/>
</div>
{showCustomInput && this.renderCustomInput()}
2020-11-03 00:41:28 +01:00
</div>
)
}
2018-08-17 02:41:23 +02:00
2020-11-03 00:41:28 +01:00
capitalizeDevice(device) {
return device.slice(0, 1).toUpperCase() + device.slice(1)
}
2018-08-17 02:41:23 +02:00
renderCustomInput() {
const hdPaths = this.getHdPaths()
return (
<input
className="hw-connect__custom-input"
type="text"
defaultValue={hdPaths[0].value}
onChange={() => {
this.setState({ customPathSelected: false })
}}
ref={this.inputRef}
autoFocus
/>
)
}
2020-11-03 00:41:28 +01:00
renderHeader() {
const { device } = this.props
return (
<div className="hw-connect">
<h3 className="hw-connect__unlock-title">
{`${this.context.t('unlock')} ${this.capitalizeDevice(device)}`}
</h3>
{device.toLowerCase() === 'ledger' ? this.renderHdPathSelector() : null}
<h3 className="hw-connect__hdPath__title">
{this.context.t('selectAnAccount')}
</h3>
<p className="hw-connect__msg">
{this.context.t('selectAnAccountHelp')}
</p>
</div>
)
}
2020-11-03 00:41:28 +01:00
renderAccounts() {
return (
<div className="hw-account-list">
{this.props.accounts.map((account, idx) => (
<div className="hw-account-list__item" key={account.address}>
<div className="hw-account-list__item__radio">
<input
type="radio"
name="selectedAccount"
id={`address-${idx}`}
value={account.index}
onChange={(e) => this.props.onAccountChange(e.target.value)}
checked={
this.props.selectedAccount === account.index.toString()
}
/>
<label
className="hw-account-list__item__label"
htmlFor={`address-${idx}`}
>
2020-11-03 00:41:28 +01:00
<span className="hw-account-list__item__index">
{account.index + 1}
</span>
{`${account.address.slice(0, 4)}...${account.address.slice(
-4,
)}`}
<span className="hw-account-list__item__balance">{`${account.balance}`}</span>
</label>
</div>
2020-11-03 00:41:28 +01:00
<a
className="hw-account-list__item__link"
href={getAccountLink(account.address, this.props.network)}
target="_blank"
rel="noopener noreferrer"
title={this.context.t('etherscanView')}
>
<img src="images/popout.svg" alt="" />
</a>
</div>
))}
</div>
)
}
2018-07-05 23:45:28 +02:00
2020-11-03 00:41:28 +01:00
renderPagination() {
return (
<div className="hw-list-pagination">
<button
className="hw-list-pagination__button"
onClick={this.goToPreviousPage}
>
{`< ${this.context.t('prev')}`}
</button>
<button
className="hw-list-pagination__button"
onClick={this.goToNextPage}
>
{`${this.context.t('next')} >`}
</button>
</div>
)
}
2020-11-03 00:41:28 +01:00
renderButtons() {
const { showCustomInput, customPathSelected } = this.state
const { onPathChange } = this.props
const showSelectButton = showCustomInput && !customPathSelected
2020-11-03 00:41:28 +01:00
const disabled = this.props.selectedAccount === null
const buttonProps = {}
if (disabled) {
buttonProps.disabled = true
}
2020-11-03 00:41:28 +01:00
return (
<div className="new-external-account-form__buttons">
<Button
type="default"
large
className="new-external-account-form__button"
onClick={this.props.onCancel.bind(this)}
>
{this.context.t('cancel')}
</Button>
{showSelectButton ? (
<Button
type="primary"
large
className="new-external-account-form__button unlock"
onClick={() => {
onPathChange(this.inputRef.current.value)
this.setState({ customPathSelected: true })
}}
>
{this.context.t('selectHdPath')}
</Button>
) : (
<Button
type="primary"
large
className="new-external-account-form__button unlock"
disabled={disabled}
onClick={this.props.onUnlockAccount.bind(this, this.props.device)}
>
{this.context.t('unlock')}
</Button>
)}
2020-11-03 00:41:28 +01:00
</div>
)
}
2020-11-03 00:41:28 +01:00
renderForgetDevice() {
return (
<div className="hw-forget-device-container">
<a onClick={this.props.onForgetDevice.bind(this, this.props.device)}>
{this.context.t('forgetDevice')}
</a>
</div>
)
}
2018-07-05 23:45:28 +02:00
2020-11-03 00:41:28 +01:00
render() {
const { showCustomInput, customPathSelected } = this.state
2020-11-03 00:41:28 +01:00
return (
<div className="new-external-account-form account-list">
{this.renderHeader()}
{(!showCustomInput || customPathSelected) && this.renderAccounts()}
{(!showCustomInput || customPathSelected) && this.renderPagination()}
2020-11-03 00:41:28 +01:00
{this.renderButtons()}
{this.renderForgetDevice()}
</div>
)
}
2018-07-05 23:45:28 +02:00
}
AccountList.propTypes = {
onPathChange: PropTypes.func.isRequired,
selectedPath: PropTypes.string.isRequired,
device: PropTypes.string.isRequired,
accounts: PropTypes.array.isRequired,
onAccountChange: PropTypes.func.isRequired,
onForgetDevice: PropTypes.func.isRequired,
getPage: PropTypes.func.isRequired,
network: PropTypes.string,
selectedAccount: PropTypes.string,
onUnlockAccount: PropTypes.func,
onCancel: PropTypes.func,
onAccountRestriction: PropTypes.func,
2018-07-05 23:45:28 +02:00
}
AccountList.contextTypes = {
t: PropTypes.func,
2018-07-05 23:45:28 +02:00
}
2018-07-06 02:59:31 +02:00
export default AccountList