import React, { Component } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { addHexPrefix } from '../../../../../app/scripts/lib/util'; import { isValidDomainName } from '../../../../helpers/utils/util'; import { isBurnAddress, isValidHexAddress, } from '../../../../../shared/modules/hexstring-utils'; import { ButtonIcon, Icon, IconName, IconSize, } from '../../../../components/component-library'; import { IconColor } from '../../../../helpers/constants/design-system'; export default class DomainInput extends Component { static contextTypes = { t: PropTypes.func, metricsEvent: PropTypes.func, }; static propTypes = { className: PropTypes.string, selectedAddress: PropTypes.string, selectedName: PropTypes.string, scanQrCode: PropTypes.func, onPaste: PropTypes.func, onValidAddressTyped: PropTypes.func, internalSearch: PropTypes.bool, userInput: PropTypes.string, onChange: PropTypes.func.isRequired, onReset: PropTypes.func.isRequired, lookupEnsName: PropTypes.func.isRequired, initializeDomainSlice: PropTypes.func.isRequired, resetDomainResolution: PropTypes.func.isRequired, }; componentDidMount() { this.props.initializeDomainSlice(); } onPaste = (event) => { if (event.clipboardData.items?.length) { const clipboardItem = event.clipboardData.items[0]; clipboardItem?.getAsString((text) => { const input = text.trim(); if ( !isBurnAddress(input) && isValidHexAddress(input, { mixedCaseUseChecksum: true }) ) { this.props.onPaste(addHexPrefix(input)); } }); } }; onChange = ({ target: { value } }) => { const { onValidAddressTyped, internalSearch, onChange, lookupEnsName, resetDomainResolution, } = this.props; const input = value.trim(); onChange(input); if (internalSearch) { return null; } // Empty ENS state if input is empty // maybe scan ENS if (isValidDomainName(input)) { lookupEnsName(input); } else { resetDomainResolution(); if ( onValidAddressTyped && !isBurnAddress(input) && isValidHexAddress(input, { mixedCaseUseChecksum: true }) ) { onValidAddressTyped(addHexPrefix(input)); } } return null; }; render() { const { t } = this.context; const { className, selectedAddress, selectedName, userInput } = this.props; const hasSelectedAddress = Boolean(selectedAddress); return (