2021-02-04 19:15:23 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
import { ellipsify } from '../../send.utils';
|
2021-05-17 21:00:59 +02:00
|
|
|
import { isValidDomainName } from '../../../../helpers/utils/util';
|
|
|
|
import {
|
|
|
|
isBurnAddress,
|
|
|
|
isValidHexAddress,
|
|
|
|
} from '../../../../../shared/modules/hexstring-utils';
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
export default class EnsInput extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-06-23 23:35:25 +02:00
|
|
|
metricsEvent: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
selectedAddress: PropTypes.string,
|
|
|
|
selectedName: PropTypes.string,
|
|
|
|
scanQrCode: PropTypes.func,
|
|
|
|
onPaste: PropTypes.func,
|
2019-08-03 01:00:50 +02:00
|
|
|
onValidAddressTyped: PropTypes.func,
|
2020-11-07 00:16:51 +01:00
|
|
|
internalSearch: PropTypes.bool,
|
2021-06-23 23:35:25 +02:00
|
|
|
userInput: PropTypes.string,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
onReset: PropTypes.func.isRequired,
|
|
|
|
lookupEnsName: PropTypes.func.isRequired,
|
|
|
|
initializeEnsSlice: PropTypes.func.isRequired,
|
|
|
|
resetEnsResolution: PropTypes.func.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidMount() {
|
2021-06-23 23:35:25 +02:00
|
|
|
this.props.initializeEnsSlice();
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
onPaste = (event) => {
|
|
|
|
event.clipboardData.items[0].getAsString((text) => {
|
2021-07-27 16:53:13 +02:00
|
|
|
const input = text.trim();
|
2021-05-17 21:00:59 +02:00
|
|
|
if (
|
2021-07-27 16:53:13 +02:00
|
|
|
!isBurnAddress(input) &&
|
|
|
|
isValidHexAddress(input, { mixedCaseUseChecksum: true })
|
2021-05-17 21:00:59 +02:00
|
|
|
) {
|
2021-07-27 16:53:13 +02:00
|
|
|
this.props.onPaste(input);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-07-27 16:53:13 +02:00
|
|
|
onChange = ({ target: { value } }) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
onValidAddressTyped,
|
2020-11-07 00:16:51 +01:00
|
|
|
internalSearch,
|
2021-06-23 23:35:25 +02:00
|
|
|
onChange,
|
|
|
|
lookupEnsName,
|
|
|
|
resetEnsResolution,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2021-07-27 16:53:13 +02:00
|
|
|
const input = value.trim();
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
onChange(input);
|
2020-11-07 00:16:51 +01:00
|
|
|
if (internalSearch) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2020-11-07 00:16:51 +01:00
|
|
|
}
|
2019-07-31 21:56:44 +02:00
|
|
|
// Empty ENS state if input is empty
|
|
|
|
// maybe scan ENS
|
2020-02-18 02:20:01 +01:00
|
|
|
if (isValidDomainName(input)) {
|
2021-06-23 23:35:25 +02:00
|
|
|
lookupEnsName(input);
|
2019-07-31 21:56:44 +02:00
|
|
|
} else {
|
2021-06-23 23:35:25 +02:00
|
|
|
resetEnsResolution();
|
2021-07-09 16:17:02 +02:00
|
|
|
if (
|
|
|
|
onValidAddressTyped &&
|
|
|
|
!isBurnAddress(input) &&
|
|
|
|
isValidHexAddress(input, { mixedCaseUseChecksum: true })
|
|
|
|
) {
|
|
|
|
onValidAddressTyped(input);
|
|
|
|
}
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
2021-07-09 16:17:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
|
|
|
};
|
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 { t } = this.context;
|
2021-06-23 23:35:25 +02:00
|
|
|
const { className, selectedAddress, selectedName, userInput } = this.props;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
const hasSelectedAddress = Boolean(selectedAddress);
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
return (
|
2020-03-01 11:14:16 +01:00
|
|
|
<div className={classnames('ens-input', className)}>
|
2019-07-31 21:56:44 +02:00
|
|
|
<div
|
2020-03-01 11:14:16 +01:00
|
|
|
className={classnames('ens-input__wrapper', {
|
2019-07-31 21:56:44 +02:00
|
|
|
'ens-input__wrapper__status-icon--error': false,
|
|
|
|
'ens-input__wrapper__status-icon--valid': false,
|
2021-06-23 23:35:25 +02:00
|
|
|
'ens-input__wrapper--valid': hasSelectedAddress,
|
2019-07-31 21:56:44 +02:00
|
|
|
})}
|
|
|
|
>
|
2021-06-23 23:35:25 +02:00
|
|
|
<div
|
|
|
|
className={classnames('ens-input__wrapper__status-icon', {
|
|
|
|
'ens-input__wrapper__status-icon--valid': hasSelectedAddress,
|
2019-07-31 21:56:44 +02:00
|
|
|
})}
|
|
|
|
/>
|
2021-06-23 23:35:25 +02:00
|
|
|
{hasSelectedAddress ? (
|
|
|
|
<>
|
|
|
|
<div className="ens-input__wrapper__input ens-input__wrapper__input--selected">
|
|
|
|
<div className="ens-input__selected-input__title">
|
|
|
|
{selectedName || ellipsify(selectedAddress)}
|
|
|
|
</div>
|
|
|
|
{selectedName && (
|
|
|
|
<div className="ens-input__selected-input__subtitle">
|
|
|
|
{selectedAddress}
|
|
|
|
</div>
|
|
|
|
)}
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
2021-06-23 23:35:25 +02:00
|
|
|
<div
|
|
|
|
className="ens-input__wrapper__action-icon ens-input__wrapper__action-icon--erase"
|
|
|
|
onClick={this.props.onReset}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<input
|
|
|
|
className="ens-input__wrapper__input"
|
|
|
|
type="text"
|
|
|
|
dir="auto"
|
|
|
|
placeholder={t('recipientAddressPlaceholder')}
|
|
|
|
onChange={this.onChange}
|
|
|
|
onPaste={this.onPaste}
|
|
|
|
value={selectedAddress || userInput}
|
|
|
|
autoFocus
|
|
|
|
data-testid="ens-input"
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
className={classnames('ens-input__wrapper__action-icon', {
|
|
|
|
'ens-input__wrapper__action-icon--erase': userInput,
|
|
|
|
'ens-input__wrapper__action-icon--qrcode': !userInput,
|
|
|
|
})}
|
|
|
|
onClick={() => {
|
|
|
|
if (userInput) {
|
|
|
|
this.props.onReset();
|
|
|
|
} else {
|
|
|
|
this.props.scanQrCode();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
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
|
|
|
}
|
|
|
|
}
|