-
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.
Now you can see other's profiles, and some other changes
- Loading branch information
Showing
8 changed files
with
190 additions
and
66 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
138 changes: 138 additions & 0 deletions
138
packages/nextjs/app/explore/_components/ProfileAddress.tsx
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,138 @@ | ||
"use client"; | ||
|
||
import { useEffect, useState } from "react"; | ||
import Link from "next/link"; | ||
import { CopyToClipboard } from "react-copy-to-clipboard"; | ||
import { Address as AddressType, getAddress, isAddress } from "viem"; | ||
import { hardhat } from "viem/chains"; | ||
import { normalize } from "viem/ens"; | ||
import { useEnsAvatar, useEnsName } from "wagmi"; | ||
import { CheckCircleIcon, DocumentDuplicateIcon } from "@heroicons/react/24/outline"; | ||
import { BlockieAvatar } from "~~/components/scaffold-eth"; | ||
import { useTargetNetwork } from "~~/hooks/scaffold-eth/useTargetNetwork"; | ||
|
||
type AddressProps = { | ||
address?: AddressType; | ||
disableAddressLink?: boolean; | ||
format?: "short" | "long"; | ||
size?: "xs" | "sm" | "base" | "lg" | "xl" | "2xl" | "3xl"; | ||
}; | ||
|
||
const blockieSizeMap = { | ||
xs: 6, | ||
sm: 7, | ||
base: 8, | ||
lg: 9, | ||
xl: 10, | ||
"2xl": 12, | ||
"3xl": 15, | ||
}; | ||
|
||
/** | ||
* Displays an address (or ENS) with a Blockie image and option to copy address. | ||
*/ | ||
export const ProfileAddress = ({ address, disableAddressLink, format, size = "base" }: AddressProps) => { | ||
const [ens, setEns] = useState<string | null>(); | ||
const [ensAvatar, setEnsAvatar] = useState<string | null>(); | ||
const [addressCopied, setAddressCopied] = useState(false); | ||
const checkSumAddress = address ? getAddress(address) : undefined; | ||
|
||
const { targetNetwork } = useTargetNetwork(); | ||
|
||
const { data: fetchedEns } = useEnsName({ | ||
address: checkSumAddress, | ||
chainId: 1, | ||
query: { | ||
enabled: isAddress(checkSumAddress ?? ""), | ||
}, | ||
}); | ||
const { data: fetchedEnsAvatar } = useEnsAvatar({ | ||
name: fetchedEns ? normalize(fetchedEns) : undefined, | ||
chainId: 1, | ||
query: { | ||
enabled: Boolean(fetchedEns), | ||
gcTime: 30_000, | ||
}, | ||
}); | ||
|
||
// We need to apply this pattern to avoid Hydration errors. | ||
useEffect(() => { | ||
setEns(fetchedEns); | ||
}, [fetchedEns]); | ||
|
||
useEffect(() => { | ||
setEnsAvatar(fetchedEnsAvatar); | ||
}, [fetchedEnsAvatar]); | ||
|
||
// Skeleton UI | ||
if (!checkSumAddress) { | ||
return ( | ||
<div className="animate-pulse flex space-x-4"> | ||
<div className="rounded-md bg-slate-300 h-6 w-6"></div> | ||
<div className="flex items-center space-y-6"> | ||
<div className="h-2 w-28 bg-slate-300 rounded"></div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
if (!isAddress(checkSumAddress)) { | ||
return <span className="text-error">Wrong address</span>; | ||
} | ||
|
||
let displayAddress = checkSumAddress?.slice(0, 6) + "..." + checkSumAddress?.slice(-4); | ||
|
||
if (ens) { | ||
displayAddress = ens; | ||
} else if (format === "long") { | ||
displayAddress = checkSumAddress; | ||
} | ||
|
||
return ( | ||
<div className="flex items-center flex-shrink-0"> | ||
<div className="flex-shrink-0"> | ||
<BlockieAvatar | ||
address={checkSumAddress} | ||
ensImage={ensAvatar} | ||
size={(blockieSizeMap[size] * 24) / blockieSizeMap["base"]} | ||
/> | ||
</div> | ||
{disableAddressLink ? ( | ||
<span className={`ml-1.5 text-${size} font-normal`}>{displayAddress}</span> | ||
) : targetNetwork.id === hardhat.id ? ( | ||
<span className={`ml-1.5 text-${size} font-normal`}> | ||
<Link href={`/profile/${checkSumAddress}`} passHref> | ||
{displayAddress} | ||
</Link> | ||
</span> | ||
) : ( | ||
<div className={`ml-1.5 text-${size} font-normal`}> | ||
<Link href={`/profile/${checkSumAddress}`} passHref> | ||
{displayAddress} | ||
</Link> | ||
</div> | ||
)} | ||
{addressCopied ? ( | ||
<CheckCircleIcon | ||
className="ml-1.5 text-xl font-normal text-sky-600 h-5 w-5 cursor-pointer flex-shrink-0" | ||
aria-hidden="true" | ||
/> | ||
) : ( | ||
<CopyToClipboard | ||
text={checkSumAddress} | ||
onCopy={() => { | ||
setAddressCopied(true); | ||
setTimeout(() => { | ||
setAddressCopied(false); | ||
}, 800); | ||
}} | ||
> | ||
<DocumentDuplicateIcon | ||
className="ml-1.5 text-xl font-normal text-sky-600 h-5 w-5 cursor-pointer flex-shrink-0" | ||
aria-hidden="true" | ||
/> | ||
</CopyToClipboard> | ||
)} | ||
</div> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
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