import React from 'react' import { useCallback, useEffect, useRef, useState } from 'react' import classNames from 'classnames' import Icon from 'semantic-ui-react/dist/commonjs/elements/Icon' import { InputOnChangeData } from 'semantic-ui-react/dist/commonjs/elements/Input' import Popup from 'semantic-ui-react/dist/commonjs/modules/Popup' import { Field } from '../Field/Field' import { shorten, isValid } from './utils' import { AddressFieldErrors, Props } from './AddressField.types' import './AddressField.css' /** * @deprecated Should start using the same component migrated to UI2. */ export default function AddressField(props: Props) { const { className, fieldClassName, i18n, resolveName, onChange, ...otherProps } = props const [inputValue, setInputValue] = useState(props.value || '') const [address, setAddress] = useState('') const timeout = useRef() const [valid, setValid] = useState() const [loading, setLoading] = useState() useEffect(() => { return () => { clearTimeout(timeout.current) } }, []) const handleChange = useCallback( (evt, data: InputOnChangeData) => { setInputValue(data.value) setValid(undefined) setAddress('') if (timeout.current) { clearTimeout(timeout.current) } timeout.current = setTimeout(async () => { let address = data.value let error = undefined if (isValid(data.value)) { setValid(true) } else { // If address is not valid try to resolve it as a name setLoading(true) try { const resolvedAddress = await resolveName(data.value) if (resolvedAddress) { setValid(true) setAddress(resolvedAddress) address = resolvedAddress } else { setValid(false) error = new Error(AddressFieldErrors.INVALID_ADDRESS_OR_NAME) } } catch (e) { error = new Error(AddressFieldErrors.ERROR_RESOLVING_NAME) setValid(false) } setLoading(false) } evt.target.focus() if (onChange) { onChange(evt, { ...data, value: address, error }) } }, 800) }, [onChange] ) const additionalProps = valid ? { icon: ( ) } : {} return (
{address && ( {shorten(address)} } /> )}
) }