33
44'use client' ;
55
6- import { Close , Search } from '@iota/apps-ui-icons' ;
6+ import { Close } from '@iota/apps-ui-icons' ;
77import {
88 Button ,
99 ButtonType ,
@@ -14,7 +14,7 @@ import {
1414} from '@iota/apps-ui-kit' ;
1515import { ConnectButton , useCurrentWallet } from '@iota/dapp-kit' ;
1616import { validateIotaName } from '@iota/iota-names-sdk' ;
17- import { useCallback , useMemo , useState } from 'react' ;
17+ import { useCallback , useEffect , useMemo , useState } from 'react' ;
1818
1919import { AuctionBidDialog } from '@/auctions/components/dialogs/AuctionBidDialog' ;
2020import { useGetAuctionMetadata } from '@/auctions/hooks/useGetAuctionMetadata' ;
@@ -31,6 +31,8 @@ interface AvailabilityCheckProps {
3131 onCompleted ?: ( ) => void ;
3232}
3333
34+ const DEBOUNCE_DELAY = 500 ;
35+
3436export function AvailabilityCheck ( { autoFocusInput, onCompleted } : AvailabilityCheckProps ) {
3537 const [ searchValue , setSearchValue ] = useState < string > ( '' ) ;
3638 const [ name , setName ] = useState < string > ( '' ) ;
@@ -61,8 +63,23 @@ export function AvailabilityCheck({ autoFocusInput, onCompleted }: AvailabilityC
6163 ) ;
6264
6365 const handleSearch = useCallback ( ( ) => {
64- if ( searchValue ) setName ( `${ searchValue } .iota` ) ;
65- } , [ searchValue ] ) ;
66+ if ( searchValue && ! validationError ) setName ( `${ searchValue } .iota` ) ;
67+ } , [ searchValue , validationError ] ) ;
68+
69+ useEffect ( ( ) => {
70+ if ( ! searchValue || validationError ) {
71+ return ;
72+ }
73+
74+ const timer = window . setTimeout ( ( ) => {
75+ setName ( ( current ) => {
76+ const newName = `${ searchValue } .iota` ;
77+ return current === newName ? current : newName ;
78+ } ) ;
79+ } , DEBOUNCE_DELAY ) ;
80+
81+ return ( ) => window . clearTimeout ( timer ) ;
82+ } , [ searchValue , validationError ] ) ;
6683
6784 function handleInputChange ( inputValue : string ) {
6885 setSearchValue ( denormalizeName ( inputValue ) ) ;
@@ -81,7 +98,6 @@ export function AvailabilityCheck({ autoFocusInput, onCompleted }: AvailabilityC
8198 auctionError ?. message || nameError ?. message || priceError ?. message || validationError || '' ;
8299 const isLoading = isLoadingAuctionMetadat || isLoadingNameRecord || isLoadingPriceLst ;
83100
84- const enableSearch = Boolean ( searchValue ) && ! errorMessage ;
85101 const isAuctionInProgress = auctionMetadata ? isAuctionActive ( auctionMetadata ) : false ;
86102 const isUnavailable = nameRecordData ?. type === 'unavailable' ;
87103 const isNameTaken = isUnavailable && ! isAuctionInProgress ;
@@ -96,13 +112,6 @@ export function AvailabilityCheck({ autoFocusInput, onCompleted }: AvailabilityC
96112 < Close />
97113 </ ButtonUnstyled >
98114 ) }
99- < ButtonUnstyled
100- className = "p-sm rounded-full [&_svg]:h-5 [&_svg]:w-5 bg-names-neutral-100 disabled:opacity-40"
101- disabled = { ! enableSearch }
102- onClick = { handleSearch }
103- >
104- < Search className = "text-black" />
105- </ ButtonUnstyled >
106115 </ div >
107116 ) ;
108117
0 commit comments