The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix(ui): fall back to writeText when clipboard.write fails (#50777)
Dongha2c5b0a5ca0af
diff --git a/apps/studio/lib/helpers.test.ts b/apps/studio/lib/helpers.test.tsindex defae2e2b43..cd867453c81 100644--- a/apps/studio/lib/helpers.test.ts+++ b/apps/studio/lib/helpers.test.ts@@ -224,16 +224,36 @@ describe('copyToClipboard', () => {expect(writeTextMock).toHaveBeenCalledWith('hello')})- it('resolves and reports when clipboard.write is denied', async () => {+ it('falls back to writeText when clipboard.write is denied', async () => {writeMock.mockRejectedValue(new DOMException("Failed to execute 'write' on 'Clipboard': Write permission denied."))const callback = vi.fn()- const promise = copyToClipboard('hello', callback)- vi.runAllTimers()+ await expect(copyToClipboard('hello', callback)).resolves.toBeUndefined()+ expect(writeTextMock).toHaveBeenCalledWith('hello')+ expect(callback).toHaveBeenCalled()+ expect(toast.error).not.toHaveBeenCalled()+ })++ it('does not retry after a successful rich write when the callback throws', async () => {+ const callback = vi.fn(() => {+ throw new Error('Callback failed')+ })++ await expect(copyToClipboard('hello', callback)).resolves.toBeUndefined()+ expect(writeMock).toHaveBeenCalledOnce()+ expect(writeTextMock).not.toHaveBeenCalled()+ expect(callback).toHaveBeenCalledOnce()+ expect(toast.error).toHaveBeenCalledWith('Unable to copy to clipboard')+ })- await expect(promise).resolves.toBeUndefined()+ it('reports when both clipboard methods are denied', async () => {+ writeMock.mockRejectedValue(new DOMException('Write permission denied.'))+ writeTextMock.mockRejectedValue(new DOMException('Write permission denied.'))+ const callback = vi.fn()++ await expect(copyToClipboard('hello', callback)).resolves.toBeUndefined()expect(callback).not.toHaveBeenCalled()expect(toast.error).toHaveBeenCalledWith('Unable to copy to clipboard')})diff --git a/packages/ui/src/lib/utils/clipboard.ts b/packages/ui/src/lib/utils/clipboard.tsindex 91b615a6a00..29debc31656 100644--- a/packages/ui/src/lib/utils/clipboard.ts+++ b/packages/ui/src/lib/utils/clipboard.ts@@ -21,18 +21,31 @@ export const copyToClipboard = async (str: ClipboardText, callback = noop) => {try {if (typeof ClipboardItem !== 'undefined' && navigator.clipboard?.write) {- // NOTE: Safari locks down the clipboard API to only work when triggered- // by a direct user interaction. You can't use it async in a promise.- // But! You can wrap the promise in a ClipboardItem, and give that to- // the clipboard API.- // Found this on https://developer.apple.com/forums/thread/691873- const text = new ClipboardItem({- 'text/plain': Promise.resolve(str).then((text) => new Blob([text], { type: 'text/plain' })),- })-- await navigator.clipboard.write([text])- callback()- return+ let richWriteSucceeded = false++ try {+ // NOTE: Safari locks down the clipboard API to only work when triggered+ // by a direct user interaction. You can't use it async in a promise.+ // But! You can wrap the promise in a ClipboardItem, and give that to+ // the clipboard API.+ // Found this on https://developer.apple.com/forums/thread/691873+ const text = new ClipboardItem({+ 'text/plain': Promise.resolve(str).then(+ (text) => new Blob([text], { type: 'text/plain' })+ ),+ })++ await navigator.clipboard.write([text])+ richWriteSucceeded = true+ } catch {+ // Safari can expose clipboard.write() and still reject it. Fall back to+ // writeText() before reporting that copying failed.+ }++ if (richWriteSucceeded) {+ callback()+ return+ }}if (!navigator.clipboard) throw new Error('Clipboard API unavailable')