import { useState, useRef, useEffect } from 'react'; import { Copy, CheckCircle } from 'lucide-react'; import { Button } from '@/components/radix/Button'; import { cn } from '@/lib/utils/utils'; interface CopyButtonProps { text: string; onCopy?: (text: string) => void; className?: string; variant?: 'primary' | 'secondary'; showText?: boolean; copiedText?: string; copyText?: string; disabled?: boolean; } export function CopyButton({ text, onCopy, className, variant = 'primary', showText = true, copiedText = 'Copied', copyText = 'Copy', disabled = false, }: CopyButtonProps) { const [copied, setCopied] = useState(false); const timerRef = useRef(null); // Cleanup timer on unmount useEffect(() => { return () => { if (timerRef.current) { clearTimeout(timerRef.current); } }; }, []); const handleCopy = async (e: React.MouseEvent) => { e.stopPropagation(); if (disabled) { return; } try { await navigator.clipboard.writeText(text); // Clear existing timer if any if (timerRef.current) { clearTimeout(timerRef.current); } setCopied(true); // Set new timer timerRef.current = setTimeout(() => { setCopied(false); timerRef.current = null; }, 3000); if (onCopy) { onCopy(text); } } catch (error) { // Failed to copy text console.error(error); } }; const isPrimary = variant === 'primary'; return ( ); }