2021-02-04 19:15:23 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Fuse from 'fuse.js';
|
|
|
|
import Identicon from '../../../../components/ui/identicon';
|
|
|
|
import { isValidAddress } from '../../../../helpers/utils/util';
|
|
|
|
import Dialog from '../../../../components/ui/dialog';
|
|
|
|
import ContactList from '../../../../components/app/contact-list';
|
|
|
|
import RecipientGroup from '../../../../components/app/contact-list/recipient-group/recipient-group.component';
|
|
|
|
import { ellipsify } from '../../send.utils';
|
|
|
|
import Button from '../../../../components/ui/button';
|
2021-02-27 06:26:04 +01:00
|
|
|
import Confusable from '../../../../components/ui/confusable';
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
export default class AddRecipient extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
query: PropTypes.string,
|
|
|
|
ownedAccounts: PropTypes.array,
|
|
|
|
addressBook: PropTypes.array,
|
|
|
|
updateGas: PropTypes.func,
|
|
|
|
updateSendTo: PropTypes.func,
|
|
|
|
ensResolution: PropTypes.string,
|
|
|
|
toError: PropTypes.string,
|
2021-03-01 14:17:22 +01:00
|
|
|
toWarning: PropTypes.string,
|
2019-07-31 21:56:44 +02:00
|
|
|
ensResolutionError: PropTypes.string,
|
|
|
|
addressBookEntryName: PropTypes.string,
|
|
|
|
contacts: PropTypes.array,
|
|
|
|
nonContacts: PropTypes.array,
|
2020-11-07 00:16:51 +01:00
|
|
|
setInternalSearch: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(props) {
|
2021-02-04 19:15:23 +01:00
|
|
|
super(props);
|
2019-07-31 21:56:44 +02:00
|
|
|
this.recentFuse = new Fuse(props.nonContacts, {
|
|
|
|
shouldSort: true,
|
|
|
|
threshold: 0.45,
|
|
|
|
location: 0,
|
|
|
|
distance: 100,
|
|
|
|
maxPatternLength: 32,
|
|
|
|
minMatchCharLength: 1,
|
2020-11-03 00:41:28 +01:00
|
|
|
keys: [{ name: 'address', weight: 0.5 }],
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
this.contactFuse = new Fuse(props.contacts, {
|
|
|
|
shouldSort: true,
|
|
|
|
threshold: 0.45,
|
|
|
|
location: 0,
|
|
|
|
distance: 100,
|
|
|
|
maxPatternLength: 32,
|
|
|
|
minMatchCharLength: 1,
|
|
|
|
keys: [
|
|
|
|
{ name: 'name', weight: 0.5 },
|
|
|
|
{ name: 'address', weight: 0.5 },
|
|
|
|
],
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
metricsEvent: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
state = {
|
|
|
|
isShowingTransfer: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
selectRecipient = (to, nickname = '') => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { updateSendTo, updateGas } = this.props;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
updateSendTo(to, nickname);
|
|
|
|
updateGas({ to });
|
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
searchForContacts = () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { query, contacts } = this.props;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let _contacts = contacts;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
if (query) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.contactFuse.setCollection(contacts);
|
|
|
|
_contacts = this.contactFuse.search(query);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return _contacts;
|
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
searchForRecents = () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { query, nonContacts } = this.props;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let _nonContacts = nonContacts;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
if (query) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.recentFuse.setCollection(nonContacts);
|
|
|
|
_nonContacts = this.recentFuse.search(query);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return _nonContacts;
|
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { ensResolution, query, addressBookEntryName } = this.props;
|
|
|
|
const { isShowingTransfer } = this.state;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let content;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
if (isValidAddress(query)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
content = this.renderExplicitAddress(query);
|
2019-07-31 21:56:44 +02:00
|
|
|
} else if (ensResolution) {
|
2020-11-03 00:41:28 +01:00
|
|
|
content = this.renderExplicitAddress(
|
|
|
|
ensResolution,
|
|
|
|
addressBookEntryName || query,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
} else if (isShowingTransfer) {
|
2021-02-04 19:15:23 +01:00
|
|
|
content = this.renderTransfer();
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="send__select-recipient-wrapper">
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.renderDialogs()}
|
|
|
|
{content || this.renderMain()}
|
2019-07-31 21:56:44 +02:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderExplicitAddress(address, name) {
|
2019-07-31 21:56:44 +02:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key={address}
|
|
|
|
className="send__select-recipient-wrapper__group-item"
|
|
|
|
onClick={() => this.selectRecipient(address, name)}
|
|
|
|
>
|
|
|
|
<Identicon address={address} diameter={28} />
|
|
|
|
<div className="send__select-recipient-wrapper__group-item__content">
|
|
|
|
<div className="send__select-recipient-wrapper__group-item__title">
|
2021-02-27 06:26:04 +01:00
|
|
|
{name ? <Confusable input={name} /> : ellipsify(address)}
|
2019-07-31 21:56:44 +02:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
{name && (
|
|
|
|
<div className="send__select-recipient-wrapper__group-item__subtitle">
|
|
|
|
{ellipsify(address)}
|
|
|
|
</div>
|
|
|
|
)}
|
2019-07-31 21:56:44 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderTransfer() {
|
2021-02-04 19:15:23 +01:00
|
|
|
let { ownedAccounts } = this.props;
|
|
|
|
const { query, setInternalSearch } = this.props;
|
|
|
|
const { t } = this.context;
|
|
|
|
const { isShowingTransfer } = this.state;
|
2020-11-07 00:16:51 +01:00
|
|
|
|
|
|
|
if (isShowingTransfer && query) {
|
|
|
|
ownedAccounts = ownedAccounts.filter(
|
|
|
|
(item) =>
|
|
|
|
item.name.toLowerCase().indexOf(query.toLowerCase()) > -1 ||
|
|
|
|
item.address.toLowerCase().indexOf(query.toLowerCase()) > -1,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-07 00:16:51 +01:00
|
|
|
}
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="send__select-recipient-wrapper__list">
|
2020-08-19 18:41:56 +02:00
|
|
|
<Button
|
|
|
|
type="link"
|
2019-07-31 21:56:44 +02:00
|
|
|
className="send__select-recipient-wrapper__list__link"
|
2020-11-07 00:16:51 +01:00
|
|
|
onClick={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
setInternalSearch(false);
|
|
|
|
this.setState({ isShowingTransfer: false });
|
2020-11-07 00:16:51 +01:00
|
|
|
}}
|
2019-07-31 21:56:44 +02:00
|
|
|
>
|
2019-12-03 17:35:44 +01:00
|
|
|
<div className="send__select-recipient-wrapper__list__back-caret" />
|
2020-11-03 00:41:28 +01:00
|
|
|
{t('backToAll')}
|
2020-08-19 18:41:56 +02:00
|
|
|
</Button>
|
2019-07-31 21:56:44 +02:00
|
|
|
<RecipientGroup
|
|
|
|
label={t('myAccounts')}
|
|
|
|
items={ownedAccounts}
|
|
|
|
onSelect={this.selectRecipient}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderMain() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
2020-11-07 00:16:51 +01:00
|
|
|
const {
|
|
|
|
query,
|
|
|
|
ownedAccounts = [],
|
|
|
|
addressBook,
|
|
|
|
setInternalSearch,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="send__select-recipient-wrapper__list">
|
|
|
|
<ContactList
|
|
|
|
addressBook={addressBook}
|
|
|
|
searchForContacts={this.searchForContacts.bind(this)}
|
|
|
|
searchForRecents={this.searchForRecents.bind(this)}
|
|
|
|
selectRecipient={this.selectRecipient.bind(this)}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{ownedAccounts && ownedAccounts.length > 1 && !query && (
|
|
|
|
<Button
|
|
|
|
type="link"
|
|
|
|
className="send__select-recipient-wrapper__list__link"
|
2020-11-07 00:16:51 +01:00
|
|
|
onClick={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
setInternalSearch(true);
|
|
|
|
this.setState({ isShowingTransfer: true });
|
2020-11-07 00:16:51 +01:00
|
|
|
}}
|
2020-11-03 00:41:28 +01:00
|
|
|
>
|
|
|
|
{t('transferBetweenAccounts')}
|
|
|
|
</Button>
|
|
|
|
)}
|
2019-07-31 21:56:44 +02:00
|
|
|
</ContactList>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderDialogs() {
|
2021-03-01 14:17:22 +01:00
|
|
|
const {
|
|
|
|
toError,
|
|
|
|
toWarning,
|
|
|
|
ensResolutionError,
|
|
|
|
ensResolution,
|
|
|
|
} = this.props;
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
if (ensResolutionError) {
|
|
|
|
return (
|
2020-11-03 00:41:28 +01:00
|
|
|
<Dialog type="error" className="send__error-dialog">
|
2019-07-31 21:56:44 +02:00
|
|
|
{ensResolutionError}
|
|
|
|
</Dialog>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-03-01 14:17:22 +01:00
|
|
|
} else if (toError && toError !== 'required' && !ensResolution) {
|
2019-07-31 21:56:44 +02:00
|
|
|
return (
|
2020-11-03 00:41:28 +01:00
|
|
|
<Dialog type="error" className="send__error-dialog">
|
2019-07-31 21:56:44 +02:00
|
|
|
{t(toError)}
|
|
|
|
</Dialog>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-03-01 14:17:22 +01:00
|
|
|
} else if (toWarning) {
|
|
|
|
return (
|
|
|
|
<Dialog type="warning" className="send__error-dialog">
|
|
|
|
{t(toWarning)}
|
|
|
|
</Dialog>
|
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
}
|