Skip to content

Commit

Permalink
Merge pull request #110 from xmtp-labs/ar/fix-ens
Browse files Browse the repository at this point in the history
fix: ENS lookup and search
  • Loading branch information
alexrisch authored May 30, 2024
2 parents 180dcf2 + 94e352a commit 1a772d9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
17 changes: 17 additions & 0 deletions src/hooks/useDebounce.ts
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;
}
18 changes: 12 additions & 6 deletions src/hooks/useEnsAddress.ts
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};
};
10 changes: 4 additions & 6 deletions src/screens/SearchScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export const SearchScreen = () => {
for (const address of participants) {
if (!canMessage?.[address.toLowerCase()]) {
setErrorString(translate('not_on_xmtp_group'));

return;
}
}
Expand All @@ -172,10 +171,7 @@ export const SearchScreen = () => {
mapping: Set<string>;
}>(
({filtered, mapping}, curr) => {
if (
// curr.name?.toLowerCase().includes(searchText.toLowerCase()) ||
curr.address.toLowerCase().includes(searchText.toLowerCase())
) {
if (curr.address.toLowerCase().includes(searchText.toLowerCase())) {
filtered.push(curr);
mapping.add(curr.address);
}
Expand Down Expand Up @@ -274,6 +270,8 @@ export const SearchScreen = () => {
<Input
testID={TestIds.SEARCH_INPUT}
variant={'unstyled'}
autoCapitalize="none"
autoCorrect={false}
leftElement={
<Box paddingLeft={'8px'}>
<Icon
Expand All @@ -295,7 +293,7 @@ export const SearchScreen = () => {
) : undefined
}
backgroundColor={colors.backgroundTertiary}
value={searchText}
defaultValue={searchText}
onChangeText={setSearchText}
marginX={'16px'}
paddingY={'12px'}
Expand Down
14 changes: 10 additions & 4 deletions src/utils/getEnsInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import {getEnsAvatar, getEnsName, normalize} from 'viem/ens';
import {viemClient} from './viemClient';

export const getEnsInfo = async (address: string) => {
const [ens, avatarUrl] = await Promise.all([
getEnsName(viemClient, {address: address as `0x${string}`}),
getEnsAvatar(viemClient, {name: normalize(address) as `0x${string}`}),
]);
const ens = await getEnsName(viemClient, {address: address as `0x${string}`});
if (!ens) {
return {
ens: null,
avatarUrl: null,
};
}
const avatarUrl = await getEnsAvatar(viemClient, {
name: normalize(ens),
});
return {
ens,
avatarUrl,
Expand Down

0 comments on commit 1a772d9

Please sign in to comment.