Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): clean up the clipboard fallback on error
Yudhi Armyndharis41d24d01ec0a
diff --git a/dashboard/src/utils/clipboard.test.ts b/dashboard/src/utils/clipboard.test.tsnew file mode 100644index 000000000..30fdc0518--- /dev/null+++ b/dashboard/src/utils/clipboard.test.ts@@ -0,0 +1,78 @@+import { test, type TestContext } from 'node:test';+import assert from 'node:assert/strict';+import { copyToClipboard } from './clipboard.ts';++function clipboardWorld(t: TestContext, copy: () => boolean, writeText?: (text: string) => Promise<void>) {+ const children: ElementDouble[] = [];+ const doc = {+ activeElement: null as ElementDouble | null,+ body: {+ appendChild(el: ElementDouble) {+ children.push(el);+ },+ removeChild(el: ElementDouble) {+ children.splice(children.indexOf(el), 1);+ },+ },+ createElement: () => new ElementDouble(),+ execCommand: copy,+ };+ class ElementDouble {+ value = '';+ style = { position: '', opacity: '' };+ setAttribute() {}+ select() {+ doc.activeElement = this;+ }+ focus() {+ doc.activeElement = this;+ }+ remove() {+ doc.body.removeChild(this);+ }+ }+ const opener = new ElementDouble();+ opener.focus();+ for (const [key, value] of Object.entries({+ document: doc,+ HTMLElement: ElementDouble,+ navigator: { clipboard: writeText ? { writeText } : undefined },+ })) {+ const previous = Object.getOwnPropertyDescriptor(globalThis, key);+ Object.defineProperty(globalThis, key, { configurable: true, value });+ t.after(() => {+ if (previous) Object.defineProperty(globalThis, key, previous);+ else Reflect.deleteProperty(globalThis, key);+ });+ }+ return { doc, opener, children };+}++test('a throwing clipboard fallback removes its textarea and restores focus', async t => {+ const { doc, opener, children } = clipboardWorld(t, () => {+ throw new Error('copy unavailable');+ });+ assert.equal(await copyToClipboard('dummy text'), false);+ assert.equal(children.length, 0);+ assert.equal(doc.activeElement, opener);+});++test('a refused clipboard write still cleans up the successful legacy fallback', async t => {+ const { doc, opener, children } = clipboardWorld(+ t,+ () => true,+ async () => {+ throw new Error('denied');+ },+ );+ assert.equal(await copyToClipboard('dummy text'), true);+ assert.equal(children.length, 0);+ assert.equal(doc.activeElement, opener);+});++test('a false legacy copy result also removes its textarea and restores focus', async t => {+ const { doc, opener, children } = clipboardWorld(t, () => false);+ assert.equal(await copyToClipboard('dummy text'), false);+ assert.equal(children.length, 0);+ assert.equal(doc.activeElement, opener);+});diff --git a/dashboard/src/utils/clipboard.ts b/dashboard/src/utils/clipboard.tsindex 969804a7b..140a4aece 100644--- a/dashboard/src/utils/clipboard.ts+++ b/dashboard/src/utils/clipboard.ts@@ -16,19 +16,20 @@ export async function copyToClipboard(text: string): Promise<boolean> {}// Selecting the textarea takes focus, and removing it leaves focus on the page: give it back to where it was.const focused = document.activeElement;+ let ta: HTMLTextAreaElement | undefined;try {- const ta = document.createElement('textarea');+ ta = document.createElement('textarea');ta.value = text;ta.setAttribute('readonly', '');ta.style.position = 'fixed';ta.style.opacity = '0';document.body.appendChild(ta);ta.select();- const ok = document.execCommand('copy');- document.body.removeChild(ta);- if (focused instanceof HTMLElement) focused.focus();- return ok;+ return document.execCommand('copy');} catch {return false;+ } finally {+ ta?.remove();+ if (focused instanceof HTMLElement) focused.focus();}}