2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
2021-05-19 16:51:47 +02:00
|
|
|
import { getAccountLink } from '@metamask/etherscan-link';
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import Button from '../../../components/ui/button';
|
2021-03-09 21:39:16 +01:00
|
|
|
import Checkbox from '../../../components/ui/check-box';
|
2021-02-19 20:03:44 +01:00
|
|
|
import Dropdown from '../../../components/ui/dropdown';
|
2018-07-05 23:45:28 +02:00
|
|
|
|
2021-08-25 00:28:16 +02:00
|
|
|
import { getURLHostName } from '../../../helpers/utils/util';
|
|
|
|
|
2022-02-16 21:54:30 +01:00
|
|
|
import { DEVICE_NAMES } from '../../../../shared/constants/hardware-wallets';
|
|
|
|
|
2018-07-06 02:59:31 +02:00
|
|
|
class AccountList extends Component {
|
2021-04-26 20:05:48 +02:00
|
|
|
state = {
|
|
|
|
pathValue: null,
|
|
|
|
};
|
|
|
|
|
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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.props.getPage(this.props.device, 1, this.props.selectedPath);
|
2020-11-03 00:41:28 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.props.onAccountRestriction();
|
2018-08-14 07:26:18 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-08-14 07:26:18 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
goToPreviousPage = () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.props.getPage(this.props.device, -1, this.props.selectedPath);
|
|
|
|
};
|
2018-08-14 07:26:18 +02:00
|
|
|
|
2021-04-26 20:05:48 +02:00
|
|
|
setPath(pathValue) {
|
|
|
|
this.setState({ pathValue });
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderHdPathSelector() {
|
2021-11-08 15:48:41 +01:00
|
|
|
const { device, selectedPath, hdPaths, onPathChange } = this.props;
|
2021-04-26 20:05:48 +02:00
|
|
|
const { pathValue } = this.state;
|
2021-02-19 20:03:44 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
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">
|
2021-02-19 20:03:44 +01:00
|
|
|
<Dropdown
|
2020-11-03 00:41:28 +01:00
|
|
|
className="hw-connect__hdPath__select"
|
2021-11-08 15:48:41 +01:00
|
|
|
options={hdPaths[device.toLowerCase()]}
|
2021-04-26 20:05:48 +02:00
|
|
|
selectedOption={pathValue || selectedPath}
|
2021-02-19 20:03:44 +01:00
|
|
|
onChange={(value) => {
|
2021-04-26 20:05:48 +02:00
|
|
|
this.setPath(value);
|
2021-11-08 15:48:41 +01:00
|
|
|
onPathChange(value);
|
2020-11-03 00:41:28 +01:00
|
|
|
}}
|
|
|
|
/>
|
2019-11-23 17:22:52 +01:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2018-08-17 02:41:23 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
capitalizeDevice(device) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return device.slice(0, 1).toUpperCase() + device.slice(1);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2018-08-17 02:41:23 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderHeader() {
|
2021-11-08 15:48:41 +01:00
|
|
|
const { device } = this.props;
|
2022-02-16 21:54:30 +01:00
|
|
|
const shouldShowHDPaths = [
|
|
|
|
DEVICE_NAMES.LEDGER,
|
|
|
|
DEVICE_NAMES.LATTICE,
|
|
|
|
DEVICE_NAMES.TREZOR,
|
|
|
|
].includes(device.toLowerCase());
|
2020-11-03 00:41:28 +01:00
|
|
|
return (
|
|
|
|
<div className="hw-connect">
|
|
|
|
<h3 className="hw-connect__unlock-title">
|
2021-04-26 20:05:48 +02:00
|
|
|
{this.context.t('selectAnAccount')}
|
2020-11-03 00:41:28 +01:00
|
|
|
</h3>
|
2021-11-08 15:48:41 +01:00
|
|
|
{shouldShowHDPaths ? this.renderHdPathSelector() : null}
|
2020-11-03 00:41:28 +01:00
|
|
|
<h3 className="hw-connect__hdPath__title">
|
|
|
|
{this.context.t('selectAnAccount')}
|
|
|
|
</h3>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2018-07-18 00:53:37 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderAccounts() {
|
2021-05-19 16:51:47 +02:00
|
|
|
const { accounts, connectedAccounts, rpcPrefs, chainId } = this.props;
|
2021-03-09 21:39:16 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return (
|
|
|
|
<div className="hw-account-list">
|
2021-03-09 21:39:16 +01:00
|
|
|
{accounts.map((account, idx) => {
|
|
|
|
const accountAlreadyConnected = connectedAccounts.includes(
|
|
|
|
account.address.toLowerCase(),
|
|
|
|
);
|
|
|
|
const value = account.index;
|
|
|
|
const checked =
|
|
|
|
this.props.selectedAccounts.includes(account.index) ||
|
|
|
|
accountAlreadyConnected;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="hw-account-list__item"
|
|
|
|
key={account.address}
|
|
|
|
title={
|
|
|
|
accountAlreadyConnected
|
|
|
|
? this.context.t('selectAnAccountAlreadyConnected')
|
|
|
|
: ''
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div className="hw-account-list__item__checkbox">
|
|
|
|
<Checkbox
|
|
|
|
id={`address-${idx}`}
|
|
|
|
checked={checked}
|
|
|
|
disabled={accountAlreadyConnected}
|
|
|
|
onClick={() => {
|
|
|
|
this.props.onAccountChange(value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<label
|
|
|
|
className="hw-account-list__item__label"
|
|
|
|
htmlFor={`address-${idx}`}
|
|
|
|
>
|
|
|
|
<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>
|
|
|
|
<a
|
|
|
|
className="hw-account-list__item__link"
|
2021-05-19 16:51:47 +02:00
|
|
|
onClick={() => {
|
|
|
|
const accountLink = getAccountLink(
|
|
|
|
account.address,
|
|
|
|
chainId,
|
|
|
|
rpcPrefs,
|
|
|
|
);
|
|
|
|
this.context.trackEvent({
|
|
|
|
category: 'Account',
|
|
|
|
event: 'Clicked Block Explorer Link',
|
|
|
|
properties: {
|
|
|
|
actions: 'Hardware Connect',
|
|
|
|
link_type: 'Account Tracker',
|
2021-08-25 00:28:16 +02:00
|
|
|
block_explorer_domain: getURLHostName(accountLink),
|
2021-05-19 16:51:47 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
global.platform.openTab({
|
|
|
|
url: accountLink,
|
|
|
|
});
|
|
|
|
}}
|
2021-03-09 21:39:16 +01:00
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
title={this.context.t('etherscanView')}
|
2019-11-23 17:22:52 +01:00
|
|
|
>
|
2021-03-09 21:39:16 +01:00
|
|
|
<img src="images/popout.svg" alt="" />
|
|
|
|
</a>
|
2019-11-23 17:22:52 +01:00
|
|
|
</div>
|
2021-03-09 21:39:16 +01:00
|
|
|
);
|
|
|
|
})}
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
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>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2018-07-18 00:53:37 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderButtons() {
|
2021-03-09 21:39:16 +01:00
|
|
|
const disabled = this.props.selectedAccounts.length === 0;
|
2021-02-04 19:15:23 +01:00
|
|
|
const buttonProps = {};
|
2020-11-03 00:41:28 +01:00
|
|
|
if (disabled) {
|
2021-02-04 19:15:23 +01:00
|
|
|
buttonProps.disabled = true;
|
2019-07-31 22:17:11 +02:00
|
|
|
}
|
2018-07-12 03:21:36 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return (
|
|
|
|
<div className="new-external-account-form__buttons">
|
|
|
|
<Button
|
2021-10-05 21:20:42 +02:00
|
|
|
type="secondary"
|
2020-11-03 00:41:28 +01:00
|
|
|
large
|
|
|
|
className="new-external-account-form__button"
|
|
|
|
onClick={this.props.onCancel.bind(this)}
|
|
|
|
>
|
|
|
|
{this.context.t('cancel')}
|
|
|
|
</Button>
|
2020-11-13 18:55:47 +01:00
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
large
|
|
|
|
className="new-external-account-form__button unlock"
|
|
|
|
disabled={disabled}
|
2021-03-09 21:39:16 +01:00
|
|
|
onClick={this.props.onUnlockAccounts.bind(
|
|
|
|
this,
|
|
|
|
this.props.device,
|
|
|
|
this.props.selectedPath,
|
|
|
|
)}
|
2020-11-13 18:55:47 +01:00
|
|
|
>
|
|
|
|
{this.context.t('unlock')}
|
|
|
|
</Button>
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2019-07-31 22:17:11 +02:00
|
|
|
|
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>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2018-07-05 23:45:28 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="new-external-account-form account-list">
|
|
|
|
{this.renderHeader()}
|
2020-11-13 18:55:47 +01:00
|
|
|
{this.renderAccounts()}
|
|
|
|
{this.renderPagination()}
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.renderButtons()}
|
|
|
|
{this.renderForgetDevice()}
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2018-07-05 23:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AccountList.propTypes = {
|
2019-07-31 22:17:11 +02:00
|
|
|
onPathChange: PropTypes.func.isRequired,
|
|
|
|
selectedPath: PropTypes.string.isRequired,
|
|
|
|
device: PropTypes.string.isRequired,
|
|
|
|
accounts: PropTypes.array.isRequired,
|
2021-03-09 21:39:16 +01:00
|
|
|
connectedAccounts: PropTypes.array.isRequired,
|
2019-07-31 22:17:11 +02:00
|
|
|
onAccountChange: PropTypes.func.isRequired,
|
|
|
|
onForgetDevice: PropTypes.func.isRequired,
|
|
|
|
getPage: PropTypes.func.isRequired,
|
2021-03-10 18:21:52 +01:00
|
|
|
chainId: PropTypes.string,
|
|
|
|
rpcPrefs: PropTypes.object,
|
2021-03-09 21:39:16 +01:00
|
|
|
selectedAccounts: PropTypes.array.isRequired,
|
|
|
|
onUnlockAccounts: PropTypes.func,
|
2019-07-31 22:17:11 +02:00
|
|
|
onCancel: PropTypes.func,
|
|
|
|
onAccountRestriction: PropTypes.func,
|
2021-03-09 21:39:16 +01:00
|
|
|
hdPaths: PropTypes.array.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-07-05 23:45:28 +02:00
|
|
|
|
|
|
|
AccountList.contextTypes = {
|
2019-07-31 22:17:11 +02:00
|
|
|
t: PropTypes.func,
|
2021-05-19 16:51:47 +02:00
|
|
|
trackEvent: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-07-06 02:59:31 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export default AccountList;
|