Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/components/web3/AddressMini.tsx
Original file line number Diff line number Diff line change
@@ -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<AddressMiniProps> = ({ 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 () => {
Copy link

Copilot AI Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The navigator.clipboard API requires HTTPS or localhost context and appropriate permissions. Consider checking for clipboard API availability before attempting to use it, as it may not be available in all browser contexts or could be blocked by permissions policies.

Suggested change
const handleCopyAddress = async () => {
const handleCopyAddress = async () => {
if (!navigator.clipboard || typeof navigator.clipboard.writeText !== 'function') {
toast.error('Clipboard API not available in this browser or context.')
return
}

Copilot uses AI. Check for mistakes.
// 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 (
<div className={`flex items-center ${withSidebar ? 'pl-4' : ''}`}>
{withSidebar && (
<div className="w-8 h-8 bg-blue-400 rounded-full flex items-center justify-center mr-2">
A
</div>
)}{' '}
{/* Placeholder for an avatar */}
<span className="text-sm font-medium">{shortAddress}</span>
)}
{/* Fix 3: Use button for accessibility */}
<button
type="button"
className={`text-sm font-medium cursor-pointer hover:text-blue-500 transition-colors ${copied ? 'text-green-500' : ''}`}
onClick={handleCopyAddress}
title="Click to copy full address"
style={{ background: 'none', border: 'none', padding: 0, font: 'inherit' }}
>
{shortAddress}
</button>
</div>
)
}
Expand Down