change copy function

This commit is contained in:
smanylov
2026-02-09 18:37:56 +07:00
parent 54529fdbb5
commit 63a241f3b5
@@ -32,10 +32,51 @@ export default function ReferralLinkSection() {
} }
}, [referralUserStats]); }, [referralUserStats]);
const copyHandler = () => { const copyHandler = async () => {
if (refLink.current?.textContent) { if (!refLink.current?.textContent) return;
navigator.clipboard.writeText(refLink.current?.textContent);
toast.success(`${t('copied')}: ${refLink.current?.textContent}`) const textToCopy = refLink.current.textContent;
if (typeof window === 'undefined' || !navigator.clipboard) {
console.warn('Clipboard API не доступен');
fallbackCopy(textToCopy);
return;
}
try {
await navigator.clipboard.writeText(textToCopy);
toast.success(`${t('copied')}: ${textToCopy}`);
} catch (err) {
console.error('Ошибка копирования:', err);
const success = fallbackCopy(textToCopy);
if (success) {
toast.success(`${t('copied')}: ${textToCopy}`);
} else {
toast.error('Не удалось скопировать');
}
}
};
const fallbackCopy = (text: string) => {
try {
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
textArea.style.top = '-999999px';
document.body.appendChild(textArea);
textArea.select();
textArea.setSelectionRange(0, 99999);
const successful = document.execCommand('copy');
document.body.removeChild(textArea);
return successful;
} catch (err) {
console.error('Fallback copy failed:', err);
return false;
} }
}; };