|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { Copy, Check, ExternalLink } from "lucide-react"; |
| 5 | +import { cn } from "@/lib/utils"; |
| 6 | + |
| 7 | +interface WalletAddressProps { |
| 8 | + address: string; |
| 9 | + chain?: string; |
| 10 | + className?: string; |
| 11 | + showCopy?: boolean; |
| 12 | + showLink?: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Get block explorer URL for a chain |
| 17 | + */ |
| 18 | +function getExplorerUrl(chain: string, address: string): string { |
| 19 | + const explorers: Record<string, string> = { |
| 20 | + gnosis: "https://gnosisscan.io/address/", |
| 21 | + ethereum: "https://etherscan.io/address/", |
| 22 | + polygon: "https://polygonscan.com/address/", |
| 23 | + arbitrum: "https://arbiscan.io/address/", |
| 24 | + optimism: "https://optimistic.etherscan.io/address/", |
| 25 | + base: "https://basescan.org/address/", |
| 26 | + celo: "https://celoscan.io/address/", |
| 27 | + }; |
| 28 | + const baseUrl = explorers[chain.toLowerCase()] || "https://etherscan.io/address/"; |
| 29 | + return `${baseUrl}${address}`; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Shorten an address for display |
| 34 | + */ |
| 35 | +function shortenAddress(address: string): string { |
| 36 | + if (!address || address.length < 10) return address; |
| 37 | + return `${address.slice(0, 6)}…${address.slice(-4)}`; |
| 38 | +} |
| 39 | + |
| 40 | +export function WalletAddress({ |
| 41 | + address, |
| 42 | + chain = "ethereum", |
| 43 | + className, |
| 44 | + showCopy = true, |
| 45 | + showLink = true, |
| 46 | +}: WalletAddressProps) { |
| 47 | + const [copied, setCopied] = useState(false); |
| 48 | + |
| 49 | + const handleCopy = async (e: React.MouseEvent) => { |
| 50 | + e.preventDefault(); |
| 51 | + e.stopPropagation(); |
| 52 | + try { |
| 53 | + await navigator.clipboard.writeText(address); |
| 54 | + setCopied(true); |
| 55 | + setTimeout(() => setCopied(false), 2000); |
| 56 | + } catch (err) { |
| 57 | + console.error("Failed to copy address:", err); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + const explorerUrl = getExplorerUrl(chain, address); |
| 62 | + |
| 63 | + return ( |
| 64 | + <span |
| 65 | + className={cn( |
| 66 | + "inline-flex items-center gap-1 font-mono text-xs text-muted-foreground", |
| 67 | + className |
| 68 | + )} |
| 69 | + > |
| 70 | + {showLink ? ( |
| 71 | + <a |
| 72 | + href={explorerUrl} |
| 73 | + target="_blank" |
| 74 | + rel="noopener noreferrer" |
| 75 | + className="hover:text-foreground transition-colors hover:underline" |
| 76 | + onClick={(e) => e.stopPropagation()} |
| 77 | + title={`View on ${chain} explorer`} |
| 78 | + > |
| 79 | + {shortenAddress(address)} |
| 80 | + </a> |
| 81 | + ) : ( |
| 82 | + <span title={address}>{shortenAddress(address)}</span> |
| 83 | + )} |
| 84 | + {showCopy && ( |
| 85 | + <button |
| 86 | + onClick={handleCopy} |
| 87 | + className="p-0.5 hover:bg-muted rounded transition-colors" |
| 88 | + title={copied ? "Copied!" : "Copy address"} |
| 89 | + > |
| 90 | + {copied ? ( |
| 91 | + <Check className="w-3 h-3 text-green-500" /> |
| 92 | + ) : ( |
| 93 | + <Copy className="w-3 h-3" /> |
| 94 | + )} |
| 95 | + </button> |
| 96 | + )} |
| 97 | + {showLink && ( |
| 98 | + <a |
| 99 | + href={explorerUrl} |
| 100 | + target="_blank" |
| 101 | + rel="noopener noreferrer" |
| 102 | + className="p-0.5 hover:bg-muted rounded transition-colors" |
| 103 | + onClick={(e) => e.stopPropagation()} |
| 104 | + title={`View on ${chain} explorer`} |
| 105 | + > |
| 106 | + <ExternalLink className="w-3 h-3" /> |
| 107 | + </a> |
| 108 | + )} |
| 109 | + </span> |
| 110 | + ); |
| 111 | +} |
0 commit comments