diff --git a/frontend/src/lib/clipboard.ts b/frontend/src/lib/clipboard.ts index 578452fc..bf2d30aa 100644 --- a/frontend/src/lib/clipboard.ts +++ b/frontend/src/lib/clipboard.ts @@ -1,22 +1,36 @@ import { toast } from "src/components/ui/use-toast"; export function copyToClipboard(content: string) { - if (navigator.clipboard && window.isSecureContext) { - navigator.clipboard.writeText(content); - } else { - // Fallback for older browsers - const textArea = document.createElement("textarea"); - textArea.value = content; - textArea.style.position = "absolute"; - textArea.style.opacity = "0"; - document.body.appendChild(textArea); - selectElement(textArea); - new Promise((res, rej) => { - document.execCommand("copy") ? res(content) : rej(); + const copyPromise = new Promise((resolve, reject) => { + if (navigator.clipboard && window.isSecureContext) { + navigator.clipboard.writeText(content).then(resolve).catch(reject); + } else { + // Fallback for older browsers + const textArea = document.createElement("textarea"); + textArea.value = content; + textArea.style.position = "absolute"; + textArea.style.opacity = "0"; + document.body.appendChild(textArea); + selectElement(textArea); + if (document.execCommand("copy")) { + resolve(content); + } else { + reject(); + } textArea.remove(); + } + }); + + copyPromise + .then(() => { + toast({ title: "Copied to clipboard." }); + }) + .catch(() => { + toast({ + title: "Failed to copy", + variant: "destructive", + }); }); - } - toast({ title: "Copied to clipboard." }); } function selectElement(element: Element) {