import { cn } from "@app/lib/cn"; import { Check, Copy, Loader2 } from "lucide-react"; import Link from "next/link"; import { useState } from "react"; import { useTranslations } from "next-intl"; type CopyToClipboardProps = { text: string; displayText?: string; getCopyText?: () => Promise; isLink?: boolean; className?: string; }; const CopyToClipboard = ({ text, displayText, getCopyText, isLink, className }: CopyToClipboardProps) => { const [copied, setCopied] = useState(false); const [copying, setCopying] = useState(false); const handleCopy = async () => { if (copying) { return; } setCopying(true); try { const value = getCopyText ? await getCopyText() : text; await navigator.clipboard.writeText(value); setCopied(true); setTimeout(() => { setCopied(false); }, 2000); } catch { // Fetch errors are toasted by the caller; clipboard failures stay silent. } finally { setCopying(false); } }; const displayValue = displayText ?? text; const t = useTranslations(); return (
{isLink ? ( {displayValue} ) : ( {displayValue} )}
); }; export default CopyToClipboard;