diff --git a/src/components/web3/AddressMini.tsx b/src/components/web3/AddressMini.tsx index 9dbe2d6..f12e668 100644 --- a/src/components/web3/AddressMini.tsx +++ b/src/components/web3/AddressMini.tsx @@ -1,21 +1,63 @@ +import { useState, useEffect } from 'react' +import toast from 'react-hot-toast' + interface AddressMiniProps { value: string withSidebar?: boolean } const AddressMini: React.FC = ({ value, withSidebar = true }) => { - // Assuming use of a utility to shorten addresses; you'll need to implement this based on your needs + const [copied, setCopied] = useState(false) const shortAddress = `${value.substring(0, 6)}…${value.substring(value.length - 4)}` + const handleCopyAddress = async () => { + // Fix 1: Check clipboard API availability + if (!navigator.clipboard || typeof navigator.clipboard.writeText !== 'function') { + toast.error('Clipboard API not available in this browser or context.') + return + } + + try { + await navigator.clipboard.writeText(value) + setCopied(true) + toast.success('Address copied to clipboard!') + + // Fix 2: Cleanup setTimeout properly + const timeoutId = setTimeout(() => { + setCopied(false) + }, 2000) + + return () => clearTimeout(timeoutId) + } catch (err) { + toast.error('Failed to copy address') + console.error('Failed to copy address:', err) + } + } + + // Fix 2 continued: Cleanup on unmount + useEffect(() => { + return () => { + setCopied(false) + } + }, []) + return (
{withSidebar && (
A
- )}{' '} - {/* Placeholder for an avatar */} - {shortAddress} + )} + {/* Fix 3: Use button for accessibility */} +
) }