2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import { MenuItem } from '../../ui/menu';
|
|
|
|
import ConnectedAccountsListItem from './connected-accounts-list-item';
|
|
|
|
import ConnectedAccountsListOptions from './connected-accounts-list-options';
|
2020-05-15 20:53:52 +02:00
|
|
|
|
|
|
|
export default class ConnectedAccountsList extends PureComponent {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-15 20:53:52 +02:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
accountToConnect: null,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-15 20:53:52 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
accountToConnect: PropTypes.shape({
|
|
|
|
address: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
}),
|
2020-11-03 00:41:28 +01:00
|
|
|
connectedAccounts: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
address: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
lastActive: PropTypes.number,
|
|
|
|
}),
|
|
|
|
).isRequired,
|
2020-06-15 21:08:53 +02:00
|
|
|
connectAccount: PropTypes.func.isRequired,
|
2020-05-15 20:53:52 +02:00
|
|
|
selectedAddress: PropTypes.string.isRequired,
|
2020-06-15 21:08:53 +02:00
|
|
|
removePermittedAccount: PropTypes.func,
|
2020-05-15 20:53:52 +02:00
|
|
|
setSelectedAddress: PropTypes.func.isRequired,
|
2020-06-15 21:08:53 +02:00
|
|
|
shouldRenderListOptions: (props, propName, componentName) => {
|
|
|
|
if (typeof props[propName] !== 'boolean') {
|
|
|
|
return new Error(
|
2020-11-03 00:41:28 +01:00
|
|
|
`Warning: Failed prop type: '${propName}' of component '${componentName}' must be a boolean. Received: ${typeof props[
|
|
|
|
propName
|
|
|
|
]}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-08-19 18:27:05 +02:00
|
|
|
} else if (props[propName] && !props.removePermittedAccount) {
|
2020-06-15 21:08:53 +02:00
|
|
|
return new Error(
|
2020-07-14 17:20:41 +02:00
|
|
|
`Warning: Failed prop type: '${propName}' of component '${componentName}' requires prop 'removePermittedAccount'.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-15 21:08:53 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return undefined;
|
2020-06-15 21:08:53 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-15 20:53:52 +02:00
|
|
|
|
Improve account options menu (#8607)
The account options menu is now much faster, and it correctly closes
when 'Switch account' is selected.
A static width had to be set on the menu so that it could be positioned
reliably. Without this width set, it was rendered as a different size
before positioning than after, which resulted in it being positioned
incorrectly. A `z-index` had to be added (equal to the `z-index` used
by the popover component) to ensure it wasn't rendered beneath the
popover.
The menu is automatically positioned relative to the account options
button, appearing below the button by default but above it instead if
there isn't room below. It is positioned to be inside the bounds of the
popover as well.
The account options button is now a `<button>` rather than a `<i>`.
This required a few additional style rules to overrule the default
button styles. Additionally the size was increased so that it matches
the designs more closely.
The callbacks for connecting, disconnecting, and switching accounts
have been updated to use state and props to determine the correct
address to use, rather than being bound to the correct address
parameter in the render function. This means we aren't creating a new
function upon each render anymore.
The `showAccountOptions` method still needs to be bound once per
account, but this was switched to use more readable syntax (`.bind`,
instead of the double arrow function).
`react-popper` and `@popperjs/core` were both added as dependencies.
These should be used for any UI requiring relative positioning (e.g.
tooltips, menus, etc.). Older versions of these libraries are already
in our codebase as transitive dependencies of the tooltip library we're
using.
2020-05-18 19:51:29 +02:00
|
|
|
state = {
|
|
|
|
accountWithOptionsShown: null,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-15 20:53:52 +02:00
|
|
|
|
Improve account options menu (#8607)
The account options menu is now much faster, and it correctly closes
when 'Switch account' is selected.
A static width had to be set on the menu so that it could be positioned
reliably. Without this width set, it was rendered as a different size
before positioning than after, which resulted in it being positioned
incorrectly. A `z-index` had to be added (equal to the `z-index` used
by the popover component) to ensure it wasn't rendered beneath the
popover.
The menu is automatically positioned relative to the account options
button, appearing below the button by default but above it instead if
there isn't room below. It is positioned to be inside the bounds of the
popover as well.
The account options button is now a `<button>` rather than a `<i>`.
This required a few additional style rules to overrule the default
button styles. Additionally the size was increased so that it matches
the designs more closely.
The callbacks for connecting, disconnecting, and switching accounts
have been updated to use state and props to determine the correct
address to use, rather than being bound to the correct address
parameter in the render function. This means we aren't creating a new
function upon each render anymore.
The `showAccountOptions` method still needs to be bound once per
account, but this was switched to use more readable syntax (`.bind`,
instead of the double arrow function).
`react-popper` and `@popperjs/core` were both added as dependencies.
These should be used for any UI requiring relative positioning (e.g.
tooltips, menus, etc.). Older versions of these libraries are already
in our codebase as transitive dependencies of the tooltip library we're
using.
2020-05-18 19:51:29 +02:00
|
|
|
disconnectAccount = () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.hideAccountOptions();
|
|
|
|
this.props.removePermittedAccount(this.state.accountWithOptionsShown);
|
|
|
|
};
|
Improve account options menu (#8607)
The account options menu is now much faster, and it correctly closes
when 'Switch account' is selected.
A static width had to be set on the menu so that it could be positioned
reliably. Without this width set, it was rendered as a different size
before positioning than after, which resulted in it being positioned
incorrectly. A `z-index` had to be added (equal to the `z-index` used
by the popover component) to ensure it wasn't rendered beneath the
popover.
The menu is automatically positioned relative to the account options
button, appearing below the button by default but above it instead if
there isn't room below. It is positioned to be inside the bounds of the
popover as well.
The account options button is now a `<button>` rather than a `<i>`.
This required a few additional style rules to overrule the default
button styles. Additionally the size was increased so that it matches
the designs more closely.
The callbacks for connecting, disconnecting, and switching accounts
have been updated to use state and props to determine the correct
address to use, rather than being bound to the correct address
parameter in the render function. This means we aren't creating a new
function upon each render anymore.
The `showAccountOptions` method still needs to be bound once per
account, but this was switched to use more readable syntax (`.bind`,
instead of the double arrow function).
`react-popper` and `@popperjs/core` were both added as dependencies.
These should be used for any UI requiring relative positioning (e.g.
tooltips, menus, etc.). Older versions of these libraries are already
in our codebase as transitive dependencies of the tooltip library we're
using.
2020-05-18 19:51:29 +02:00
|
|
|
|
2020-06-15 21:08:53 +02:00
|
|
|
switchAccount = (address) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.hideAccountOptions();
|
|
|
|
this.props.setSelectedAddress(address);
|
|
|
|
};
|
Improve account options menu (#8607)
The account options menu is now much faster, and it correctly closes
when 'Switch account' is selected.
A static width had to be set on the menu so that it could be positioned
reliably. Without this width set, it was rendered as a different size
before positioning than after, which resulted in it being positioned
incorrectly. A `z-index` had to be added (equal to the `z-index` used
by the popover component) to ensure it wasn't rendered beneath the
popover.
The menu is automatically positioned relative to the account options
button, appearing below the button by default but above it instead if
there isn't room below. It is positioned to be inside the bounds of the
popover as well.
The account options button is now a `<button>` rather than a `<i>`.
This required a few additional style rules to overrule the default
button styles. Additionally the size was increased so that it matches
the designs more closely.
The callbacks for connecting, disconnecting, and switching accounts
have been updated to use state and props to determine the correct
address to use, rather than being bound to the correct address
parameter in the render function. This means we aren't creating a new
function upon each render anymore.
The `showAccountOptions` method still needs to be bound once per
account, but this was switched to use more readable syntax (`.bind`,
instead of the double arrow function).
`react-popper` and `@popperjs/core` were both added as dependencies.
These should be used for any UI requiring relative positioning (e.g.
tooltips, menus, etc.). Older versions of these libraries are already
in our codebase as transitive dependencies of the tooltip library we're
using.
2020-05-18 19:51:29 +02:00
|
|
|
|
|
|
|
hideAccountOptions = () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ accountWithOptionsShown: null });
|
|
|
|
};
|
Improve account options menu (#8607)
The account options menu is now much faster, and it correctly closes
when 'Switch account' is selected.
A static width had to be set on the menu so that it could be positioned
reliably. Without this width set, it was rendered as a different size
before positioning than after, which resulted in it being positioned
incorrectly. A `z-index` had to be added (equal to the `z-index` used
by the popover component) to ensure it wasn't rendered beneath the
popover.
The menu is automatically positioned relative to the account options
button, appearing below the button by default but above it instead if
there isn't room below. It is positioned to be inside the bounds of the
popover as well.
The account options button is now a `<button>` rather than a `<i>`.
This required a few additional style rules to overrule the default
button styles. Additionally the size was increased so that it matches
the designs more closely.
The callbacks for connecting, disconnecting, and switching accounts
have been updated to use state and props to determine the correct
address to use, rather than being bound to the correct address
parameter in the render function. This means we aren't creating a new
function upon each render anymore.
The `showAccountOptions` method still needs to be bound once per
account, but this was switched to use more readable syntax (`.bind`,
instead of the double arrow function).
`react-popper` and `@popperjs/core` were both added as dependencies.
These should be used for any UI requiring relative positioning (e.g.
tooltips, menus, etc.). Older versions of these libraries are already
in our codebase as transitive dependencies of the tooltip library we're
using.
2020-05-18 19:51:29 +02:00
|
|
|
|
|
|
|
showAccountOptions = (address) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ accountWithOptionsShown: address });
|
|
|
|
};
|
2020-05-15 20:53:52 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderUnconnectedAccount() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { accountToConnect, connectAccount } = this.props;
|
|
|
|
const { t } = this.context;
|
2020-05-15 20:53:52 +02:00
|
|
|
|
|
|
|
if (!accountToConnect) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2020-05-15 20:53:52 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { address, name } = accountToConnect;
|
2020-05-15 20:53:52 +02:00
|
|
|
return (
|
|
|
|
<ConnectedAccountsListItem
|
|
|
|
className="connected-accounts-list__row--highlight"
|
|
|
|
address={address}
|
|
|
|
name={`${name} (…${address.substr(-4, 4)})`}
|
2020-06-15 21:08:53 +02:00
|
|
|
status={t('statusNotConnected')}
|
2020-11-03 00:41:28 +01:00
|
|
|
action={
|
2020-06-15 21:08:53 +02:00
|
|
|
<a
|
|
|
|
className="connected-accounts-list__account-status-link"
|
|
|
|
onClick={() => connectAccount(accountToConnect.address)}
|
|
|
|
>
|
|
|
|
{t('connect')}
|
|
|
|
</a>
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2020-05-15 20:53:52 +02:00
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-15 20:53:52 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderListItemOptions(address) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { accountWithOptionsShown } = this.state;
|
|
|
|
const { t } = this.context;
|
2020-05-15 20:53:52 +02:00
|
|
|
|
2020-06-15 21:08:53 +02:00
|
|
|
return (
|
|
|
|
<ConnectedAccountsListOptions
|
|
|
|
onHideOptions={this.hideAccountOptions}
|
|
|
|
onShowOptions={this.showAccountOptions.bind(null, address)}
|
|
|
|
show={accountWithOptionsShown === address}
|
|
|
|
>
|
|
|
|
<MenuItem
|
|
|
|
iconClassName="disconnect-icon"
|
|
|
|
onClick={this.disconnectAccount}
|
|
|
|
>
|
|
|
|
{t('disconnectThisAccount')}
|
|
|
|
</MenuItem>
|
|
|
|
</ConnectedAccountsListOptions>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-15 21:08:53 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderListItemAction(address) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
2020-06-15 21:08:53 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<a
|
|
|
|
className="connected-accounts-list__account-status-link"
|
|
|
|
onClick={() => this.switchAccount(address)}
|
|
|
|
>
|
|
|
|
{t('switchToThisAccount')}
|
|
|
|
</a>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-15 21:08:53 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2020-06-15 21:08:53 +02:00
|
|
|
const {
|
|
|
|
connectedAccounts,
|
|
|
|
selectedAddress,
|
|
|
|
shouldRenderListOptions,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
|
|
|
const { t } = this.context;
|
2020-06-15 21:08:53 +02:00
|
|
|
|
2020-05-15 20:53:52 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<main className="connected-accounts-list">
|
|
|
|
{this.renderUnconnectedAccount()}
|
2020-11-03 00:41:28 +01:00
|
|
|
{connectedAccounts.map(({ address, name }, index) => {
|
|
|
|
return (
|
|
|
|
<ConnectedAccountsListItem
|
|
|
|
key={address}
|
|
|
|
address={address}
|
|
|
|
name={`${name} (…${address.substr(-4, 4)})`}
|
|
|
|
status={index === 0 ? t('active') : null}
|
|
|
|
options={
|
|
|
|
shouldRenderListOptions
|
|
|
|
? this.renderListItemOptions(address)
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
action={
|
|
|
|
address === selectedAddress
|
|
|
|
? null
|
|
|
|
: this.renderListItemAction(address)
|
|
|
|
}
|
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
})}
|
2020-05-15 20:53:52 +02:00
|
|
|
</main>
|
|
|
|
</>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-15 20:53:52 +02:00
|
|
|
}
|
|
|
|
}
|