Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Proper error handling for document is not focused issue with clipboard https://sentry.io/organizations/supabase/issues/2354867267/

Joshen Lim7a3949fe9f24
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.tsx b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
index e3c39ed5eaa..5c14341b70b 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
@@ -5,7 +5,7 @@ import { CSVLink } from 'react-csv'
import { useMonaco } from '@monaco-editor/react'
import { IconLoader, Typography } from '@supabase/ui'
-import { uuidv4 } from 'lib/helpers'
+import { copyToClipboard, uuidv4 } from 'lib/helpers'
import { useStore } from 'hooks'
import { SQL_TEMPLATES } from './SQLEditor.constants'
import { getResultsMarkdown } from './SQLEditor.utils'
@@ -109,8 +109,9 @@ const SQLEditor: FC<Props> = ({ queryId, templateId }) => {
return ui.setNotification({ category: 'info', message: 'There are no results to be copied' })
}
const markdown = getResultsMarkdown(results)
- navigator?.clipboard.writeText(markdown)
- ui.setNotification({ category: 'success', message: 'Results copied to clipboard' })
+ copyToClipboard(markdown, () => {
+ ui.setNotification({ category: 'success', message: 'Results copied to clipboard' })
+ })
}
const onSelectDownload = () => {
diff --git a/studio/components/to-be-cleaned/SimpleCodeBlock.tsx b/studio/components/to-be-cleaned/SimpleCodeBlock.tsx
index efeec0fcd86..45082b9dba0 100644
--- a/studio/components/to-be-cleaned/SimpleCodeBlock.tsx
+++ b/studio/components/to-be-cleaned/SimpleCodeBlock.tsx
@@ -11,6 +11,7 @@ import defaultTheme from 'prism-react-renderer/themes/palenight'
import Clipboard from 'clipboard'
import rangeParser from 'parse-numeric-range'
import { Button } from '@supabase/ui'
+import { copyToClipboard } from 'lib/helpers'
const highlightLinesRangeRegex = /{([\d,-]+)}/
const prism = {
@@ -61,7 +62,7 @@ const SimpleCodeBlock: FC<any> = ({ children, className: languageClassName, meta
}
const handleCopyCode = (code: any) => {
- navigator?.clipboard?.writeText(code).then(() => setShowCopied(true))
+ copyToClipboard(code, () => setShowCopied(true))
}
return (
diff --git a/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js b/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js
index bb81d7dc7fc..c95f0344200 100644
--- a/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js
+++ b/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js
@@ -14,7 +14,7 @@ import {
} from '@supabase/ui'
import Editor from '@monaco-editor/react'
-import { timeout } from 'lib/helpers'
+import { copyToClipboard, timeout } from 'lib/helpers'
import { IS_PLATFORM } from 'lib/constants'
import { useSqlStore, UTILITY_TAB_TYPES } from 'localStores/sqlEditor/SqlEditorStore'
import { useProjectContentStore } from 'stores/projectContentStore'
@@ -240,10 +240,11 @@ const ResultsDropdown = observer(() => {
function onCopyAsMarkdown() {
if (navigator) {
- navigator.clipboard.writeText(sqlEditorStore.activeTab.markdownData)
+ copyToClipboard(sqlEditorStore.activeTab.markdownData, () => {
+ ui.setNotification({ category: 'success', message: 'Copied results to clipboard' })
+ Telemetry.sendEvent('sql_editor', 'sql_copy_as_markdown', '')
+ })
}
- ui.setNotification({ category: 'success', message: 'Copied results to clipboard' })
- Telemetry.sendEvent('sql_editor', 'sql_copy_as_markdown', '')
}
return (
@@ -571,7 +572,7 @@ const Results = ({ results }) => {
const colKey = columns[idx].key
const cellValue = results[rowIdx]?.[colKey] ?? ''
const value = formatClipboardValue(cellValue)
- navigator.clipboard.writeText(value)
+ copyToClipboard(value)
}
}
diff --git a/studio/lib/helpers.ts b/studio/lib/helpers.ts
index 2396b5066d1..d657c8e2477 100644
--- a/studio/lib/helpers.ts
+++ b/studio/lib/helpers.ts
@@ -152,3 +152,12 @@ export const snakeToCamel = (str: string) =>
str.replace(/([-_][a-z])/g, (group: string) =>
group.toUpperCase().replace('-', '').replace('_', '')
)
+
+export const copyToClipboard = (str: string, callback = () => {}) => {
+ const focused = window.document.hasFocus()
+ if (focused) {
+ window.navigator?.clipboard?.writeText(str).then(callback)
+ } else {
+ console.warn('Unable to copy to clipboard')
+ }
+}
diff --git a/studio/localStores/storageExplorer/StorageExplorerStore.js b/studio/localStores/storageExplorer/StorageExplorerStore.js
index 5efb69d3512..654c26adfeb 100644
--- a/studio/localStores/storageExplorer/StorageExplorerStore.js
+++ b/studio/localStores/storageExplorer/StorageExplorerStore.js
@@ -23,7 +23,7 @@ import {
STORAGE_ROW_STATUS,
STORAGE_SORT_BY,
} from 'components/to-be-cleaned/Storage/Storage.constants.ts'
-import { timeout } from 'lib/helpers'
+import { timeout, copyToClipboard } from 'lib/helpers'
/**
* This is a preferred method rather than React Context and useStorageExplorerStore().
@@ -369,12 +369,15 @@ class StorageExplorerStore {
const filePreview = find(this.filePreviewCache, { id: file.id })
if (filePreview) {
// Already generated signed URL
- window.navigator?.clipboard?.writeText(filePreview.url)
+ copyToClipboard(filePreview.url, () => {
+ toast(`Copied URL for ${file.name} to clipboard.`)
+ })
} else {
// Need to generate signed URL, and might as well save it to cache as well
const signedUrl = await this.fetchFilePreview(file.name)
- window.navigator?.clipboard?.writeText(signedUrl)
-
+ copyToClipboard(signedUrl, () => {
+ toast(`Copied URL for ${file.name} to clipboard.`)
+ })
const fileCache = {
id: file.id,
url: signedUrl,
@@ -383,7 +386,6 @@ class StorageExplorerStore {
}
this.addFileToPreviewCache(fileCache)
}
- toast(`Copied URL for ${file.name} to clipboard.`)
}
/* Methods that involve the storage client library */