import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { getAccountLink } from '@metamask/etherscan-link';
import Button from '../../../components/ui/button';
import Checkbox from '../../../components/ui/check-box';
import Dropdown from '../../../components/ui/dropdown';
import Popover from '../../../components/ui/popover';
class AccountList extends Component {
state = {
showPopover: false,
pathValue: null,
};
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();
}
};
goToPreviousPage = () => {
this.props.getPage(this.props.device, -1, this.props.selectedPath);
};
setPath(pathValue) {
this.setState({ pathValue });
}
renderHdPathSelector() {
const { selectedPath, hdPaths } = this.props;
const { pathValue } = this.state;
return (
{this.context.t('selectHdPath')}
{this.context.t('selectPathHelp')}
{
this.setPath(value);
}}
/>
);
}
capitalizeDevice(device) {
return device.slice(0, 1).toUpperCase() + device.slice(1);
}
renderHeader() {
return (
{this.context.t('selectAnAccount')}
{this.context.t('selectAnAccount')}
{this.context.t('selectAnAccountHelp')}
{this.context.t('selectAnAccountHelpDirections', [
,
])}
);
}
renderAccounts() {
const { accounts, connectedAccounts, rpcPrefs, chainId } = this.props;
return (
{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 (
{
this.props.onAccountChange(value);
}}
/>
{
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',
block_explorer_domain: accountLink
? new URL(accountLink)?.hostname
: '',
},
});
global.platform.openTab({
url: accountLink,
});
}}
target="_blank"
rel="noopener noreferrer"
title={this.context.t('etherscanView')}
>
);
})}
);
}
renderPagination() {
return (
);
}
renderButtons() {
const disabled = this.props.selectedAccounts.length === 0;
const buttonProps = {};
if (disabled) {
buttonProps.disabled = true;
}
return (
);
}
renderForgetDevice() {
return (
);
}
renderSelectPathPopover() {
const { pathValue } = this.state;
const { onPathChange } = this.props;
const footer = (
);
return (
{this.renderHdPathSelector()}
);
}
render() {
const { showPopover } = this.state;
return (
{this.renderHeader()}
{this.renderAccounts()}
{this.renderPagination()}
{this.renderButtons()}
{this.renderForgetDevice()}
{showPopover && this.renderSelectPathPopover()}
);
}
}
AccountList.propTypes = {
onPathChange: PropTypes.func.isRequired,
selectedPath: PropTypes.string.isRequired,
device: PropTypes.string.isRequired,
accounts: PropTypes.array.isRequired,
connectedAccounts: PropTypes.array.isRequired,
onAccountChange: PropTypes.func.isRequired,
onForgetDevice: PropTypes.func.isRequired,
getPage: PropTypes.func.isRequired,
chainId: PropTypes.string,
rpcPrefs: PropTypes.object,
selectedAccounts: PropTypes.array.isRequired,
onUnlockAccounts: PropTypes.func,
onCancel: PropTypes.func,
onAccountRestriction: PropTypes.func,
hdPaths: PropTypes.array.isRequired,
};
AccountList.contextTypes = {
t: PropTypes.func,
trackEvent: PropTypes.func,
};
export default AccountList;