-
Notifications
You must be signed in to change notification settings - Fork 135
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
0683677
commit ef43653
Showing
1 changed file
with
37 additions
and
16 deletions.
There are no files selected for viewing
53 changes: 37 additions & 16 deletions
53
examples/ui-demo/src/components/shared/user-connection-avatar/UserAddressLink.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 |
---|---|---|
@@ -1,25 +1,46 @@ | ||
import truncateAddress from "@/utils/truncate-address"; | ||
import { useConnection } from "@account-kit/react"; | ||
import { useMemo } from "react"; | ||
import { | ||
TooltipProvider, | ||
Tooltip, | ||
TooltipTrigger, | ||
TooltipContent, | ||
TooltipArrow, | ||
} from "@radix-ui/react-tooltip"; | ||
import { useState } from "react"; | ||
|
||
export const UserAddressLink = ({ address }: { address: string | null }) => { | ||
const connection = useConnection(); | ||
const [showCopied, setShowCopied] = useState(false); | ||
const [isOpen, setIsOpen] = useState(false); | ||
const truncatedAddress = truncateAddress(address ?? ""); | ||
const addressBlockExplorerUrl = useMemo(() => { | ||
if (!address || !connection.chain.blockExplorers) { | ||
return null; | ||
} | ||
|
||
return `${connection.chain.blockExplorers?.default.url}/address/${address}`; | ||
}, [address, connection]); | ||
const handleClick = async () => { | ||
if (!address) return; | ||
await navigator.clipboard.writeText(address); | ||
setShowCopied(true); | ||
setIsOpen(true); | ||
setTimeout(() => setShowCopied(false), 2000); | ||
}; | ||
|
||
return ( | ||
<a | ||
target="_blank" | ||
className="text-fg-primary underline text-md md:text-sm" | ||
href={addressBlockExplorerUrl ?? "#"} | ||
> | ||
{truncatedAddress} | ||
</a> | ||
<TooltipProvider> | ||
<Tooltip open={isOpen} onOpenChange={setIsOpen}> | ||
<TooltipTrigger asChild> | ||
<button | ||
onClick={handleClick} | ||
className="text-fg-primary underline text-md md:text-sm" | ||
> | ||
{truncatedAddress} | ||
</button> | ||
</TooltipTrigger> | ||
<TooltipContent | ||
align="start" | ||
alignOffset={-16} | ||
className="bg-foreground text-background px-3 py-2 rounded-md" | ||
> | ||
<TooltipArrow /> | ||
<p className="text-xs">{showCopied ? "Copied!" : address}</p> | ||
</TooltipContent> | ||
</Tooltip> | ||
</TooltipProvider> | ||
); | ||
}; |