-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd12ce0
commit feda6da
Showing
5 changed files
with
146 additions
and
75 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,76 @@ | ||
import { FC, HTMLAttributes, memo, useEffect, useState } from "react"; | ||
import { | ||
FC, | ||
HTMLAttributes, | ||
memo, | ||
ReactNode, | ||
useEffect, | ||
useState, | ||
} from "react"; | ||
import AutoIcon from "./AutoIcon"; | ||
import Avatar from "app/components/elements/Avatar"; | ||
|
||
import { useEns } from "app/hooks"; | ||
|
||
type WalletAvatarProps = HTMLAttributes<HTMLDivElement> & { | ||
seed: string; | ||
onlyEns?: boolean; | ||
fallback?: ReactNode; | ||
}; | ||
|
||
const WalletAvatar: FC<WalletAvatarProps> = memo(({ seed, className }) => { | ||
const { getEnsName, getEnsAvatar } = useEns(); | ||
|
||
const [ensAvatar, setEnsAvatar] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const isValidEthereumAddress = (seed: string) => { | ||
const ethereumAddressRegex = /^(0x)?[0-9a-fA-F]{40}$/; | ||
return ethereumAddressRegex.test(seed); | ||
}; | ||
|
||
const fetchEnsName = async () => { | ||
try { | ||
const name = await getEnsName(seed); | ||
if (name) { | ||
const avatar = await getEnsAvatar(name); | ||
setEnsAvatar(avatar); | ||
} else { | ||
setEnsAvatar(null); | ||
const WalletAvatar: FC<WalletAvatarProps> = memo( | ||
({ seed, onlyEns, fallback, className }) => { | ||
const { getEnsName, getEnsAvatar } = useEns(); | ||
|
||
const [ensAvatar, setEnsAvatar] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const isValidEthereumAddress = (seed: string) => { | ||
const ethereumAddressRegex = /^(0x)?[0-9a-fA-F]{40}$/; | ||
return ethereumAddressRegex.test(seed); | ||
}; | ||
|
||
const fetchEnsName = async () => { | ||
try { | ||
const name = await getEnsName(seed); | ||
if (name) { | ||
const avatar = await getEnsAvatar(name); | ||
setEnsAvatar(avatar); | ||
} else { | ||
setEnsAvatar(null); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
}; | ||
|
||
if (isValidEthereumAddress(seed)) { | ||
fetchEnsName(); | ||
} | ||
}; | ||
|
||
if (isValidEthereumAddress(seed)) { | ||
fetchEnsName(); | ||
} | ||
}, [getEnsName, getEnsAvatar, seed]); | ||
|
||
return ( | ||
<> | ||
{ensAvatar ? ( | ||
<Avatar | ||
src={ensAvatar} | ||
className={className} | ||
imageClassName="rounded-md" | ||
alt={"ensAvatar"} | ||
withBorder={false} | ||
/> | ||
) : ( | ||
<AutoIcon | ||
seed={seed} | ||
source="dicebear" | ||
type="personas" | ||
className={className} | ||
/> | ||
)} | ||
</> | ||
); | ||
}); | ||
}, [getEnsName, getEnsAvatar, seed]); | ||
|
||
return ( | ||
<> | ||
{ensAvatar ? ( | ||
<Avatar | ||
src={ensAvatar} | ||
className={className} | ||
imageClassName="rounded-md" | ||
alt={"ensAvatar"} | ||
withBorder={false} | ||
/> | ||
) : !onlyEns ? ( | ||
<AutoIcon | ||
seed={seed} | ||
source="dicebear" | ||
type="personas" | ||
className={className} | ||
/> | ||
) : ( | ||
fallback | ||
)} | ||
</> | ||
); | ||
}, | ||
); | ||
|
||
export default WalletAvatar; |
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