-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made search a bit more performant Added debouncing Fixed avatar lookup
- Loading branch information
Alex Risch
authored and
Alex Risch
committed
May 30, 2024
1 parent
180dcf2
commit 94e352a
Showing
4 changed files
with
43 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {useEffect, useState} from 'react'; | ||
|
||
export function useDebounce<T>(value: T, delay: number) { | ||
const [debouncedValue, setDebouncedValue] = useState<T>(value); | ||
|
||
useEffect(() => { | ||
const handler = setTimeout(() => { | ||
setDebouncedValue(value); | ||
}, delay); | ||
|
||
return () => { | ||
clearTimeout(handler); | ||
}; | ||
}, [value, delay]); | ||
|
||
return debouncedValue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,27 @@ | ||
import {useEffect, useState} from 'react'; | ||
import {getEnsAddress} from 'viem/ens'; | ||
import {viemClient} from '../utils/viemClient'; | ||
import {useDebounce} from './useDebounce'; | ||
|
||
export const useEnsAddress = (searchText?: string) => { | ||
const [ensAddress, setEnsAddress] = useState<string | null>(null); | ||
const debouncedSearchText = useDebounce(searchText, 250); | ||
|
||
useEffect(() => { | ||
if (searchText) { | ||
if (debouncedSearchText) { | ||
getEnsAddress(viemClient, { | ||
name: searchText, | ||
}).then(address => { | ||
setEnsAddress(address); | ||
}); | ||
name: debouncedSearchText, | ||
}) | ||
.then(address => { | ||
setEnsAddress(address); | ||
}) | ||
.catch(e => { | ||
console.error('Error getting ENS address', e); | ||
}); | ||
} else { | ||
setEnsAddress(null); | ||
} | ||
}, [searchText]); | ||
}, [debouncedSearchText]); | ||
|
||
return {data: ensAddress}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters