2019-07-31 21:56:44 +02:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import Fuse from 'fuse.js'
|
|
|
|
import Identicon from '../../../../components/ui/identicon'
|
2019-12-03 21:50:55 +01:00
|
|
|
import { isValidAddress } from '../../../../helpers/utils/util'
|
2019-07-31 21:56:44 +02:00
|
|
|
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'
|
2019-12-03 21:50:55 +01:00
|
|
|
import { ellipsify } from '../../send.utils'
|
2020-08-19 18:41:56 +02:00
|
|
|
import Button from '../../../../components/ui/button'
|
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,
|
|
|
|
ensResolutionError: PropTypes.string,
|
|
|
|
addressBookEntryName: PropTypes.string,
|
|
|
|
contacts: PropTypes.array,
|
|
|
|
nonContacts: PropTypes.array,
|
2020-11-07 00:16:51 +01:00
|
|
|
setInternalSearch: PropTypes.func,
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(props) {
|
2019-07-31 21:56:44 +02:00
|
|
|
super(props)
|
|
|
|
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 }],
|
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 },
|
|
|
|
],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
metricsEvent: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
|
|
|
isShowingTransfer: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
selectRecipient = (to, nickname = '') => {
|
|
|
|
const { updateSendTo, updateGas } = this.props
|
|
|
|
|
|
|
|
updateSendTo(to, nickname)
|
|
|
|
updateGas({ to })
|
|
|
|
}
|
|
|
|
|
|
|
|
searchForContacts = () => {
|
|
|
|
const { query, contacts } = this.props
|
|
|
|
|
|
|
|
let _contacts = contacts
|
|
|
|
|
|
|
|
if (query) {
|
|
|
|
this.contactFuse.setCollection(contacts)
|
|
|
|
_contacts = this.contactFuse.search(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
return _contacts
|
|
|
|
}
|
|
|
|
|
|
|
|
searchForRecents = () => {
|
|
|
|
const { query, nonContacts } = this.props
|
|
|
|
|
|
|
|
let _nonContacts = nonContacts
|
|
|
|
|
|
|
|
if (query) {
|
|
|
|
this.recentFuse.setCollection(nonContacts)
|
|
|
|
_nonContacts = this.recentFuse.search(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
return _nonContacts
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2019-07-31 21:56:44 +02:00
|
|
|
const { ensResolution, query, addressBookEntryName } = this.props
|
|
|
|
const { isShowingTransfer } = this.state
|
|
|
|
|
|
|
|
let content
|
|
|
|
|
|
|
|
if (isValidAddress(query)) {
|
|
|
|
content = this.renderExplicitAddress(query)
|
|
|
|
} else if (ensResolution) {
|
2020-11-03 00:41:28 +01:00
|
|
|
content = this.renderExplicitAddress(
|
|
|
|
ensResolution,
|
|
|
|
addressBookEntryName || query,
|
|
|
|
)
|
2019-07-31 21:56:44 +02:00
|
|
|
} else if (isShowingTransfer) {
|
|
|
|
content = this.renderTransfer()
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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">
|
|
|
|
{name || ellipsify(address)}
|
|
|
|
</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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderTransfer() {
|
2020-11-07 00:16:51 +01:00
|
|
|
let { ownedAccounts } = this.props
|
|
|
|
const { query, setInternalSearch } = this.props
|
2019-07-31 21:56:44 +02:00
|
|
|
const { t } = this.context
|
2020-11-07 00:16:51 +01:00
|
|
|
const { isShowingTransfer } = this.state
|
|
|
|
|
|
|
|
if (isShowingTransfer && query) {
|
|
|
|
ownedAccounts = ownedAccounts.filter(
|
|
|
|
(item) =>
|
|
|
|
item.name.toLowerCase().indexOf(query.toLowerCase()) > -1 ||
|
|
|
|
item.address.toLowerCase().indexOf(query.toLowerCase()) > -1,
|
|
|
|
)
|
|
|
|
}
|
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={() => {
|
|
|
|
setInternalSearch(false)
|
|
|
|
this.setState({ isShowingTransfer: false })
|
|
|
|
}}
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderMain() {
|
2019-07-31 21:56:44 +02:00
|
|
|
const { t } = this.context
|
2020-11-07 00:16:51 +01:00
|
|
|
const {
|
|
|
|
query,
|
|
|
|
ownedAccounts = [],
|
|
|
|
addressBook,
|
|
|
|
setInternalSearch,
|
|
|
|
} = 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={() => {
|
|
|
|
setInternalSearch(true)
|
|
|
|
this.setState({ isShowingTransfer: true })
|
|
|
|
}}
|
2020-11-03 00:41:28 +01:00
|
|
|
>
|
|
|
|
{t('transferBetweenAccounts')}
|
|
|
|
</Button>
|
|
|
|
)}
|
2019-07-31 21:56:44 +02:00
|
|
|
</ContactList>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderDialogs() {
|
2020-08-28 17:27:07 +02:00
|
|
|
const { toError, ensResolutionError, ensResolution } = this.props
|
2019-07-31 21:56:44 +02:00
|
|
|
const { t } = this.context
|
|
|
|
const contacts = this.searchForContacts()
|
|
|
|
const recents = this.searchForRecents()
|
|
|
|
|
|
|
|
if (contacts.length || recents.length) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toError && toError !== 'required' && !ensResolution) {
|
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:06:57 +02:00
|
|
|
return null
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
}
|