Skip to content

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

fix: add back sql editor context copy item

Alaister Young59aea924f521
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx
index c0d3cb273fd..1c5bc078dc0 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx
@@ -1,11 +1,26 @@
-import DataGrid from '@supabase/react-data-grid'
+import DataGrid, { Column } from '@supabase/react-data-grid'
import { useKeyboardShortcuts } from 'hooks'
import { copyToClipboard } from 'lib/helpers'
-import { useState } from 'react'
+import { useEffect, useState } from 'react'
+import { Item, Menu, useContextMenu } from 'react-contexify'
+import { createPortal } from 'react-dom'
+import { IconClipboard } from 'ui'
+
+const Results = ({ id, rows }: { id: string; rows: readonly any[] }) => {
+ const SQL_CONTEXT_EDITOR_ID = 'sql-context-menu-' + id
-const Results = ({ rows }: { rows: readonly any[] }) => {
const [cellPosition, setCellPosition] = useState<any>(undefined)
+ function onCopyCell() {
+ if (columns && cellPosition) {
+ const { idx, rowIdx } = cellPosition
+ const colKey = columns[idx].key
+ const cellValue = rows[rowIdx]?.[colKey] ?? ''
+ const value = formatClipboardValue(cellValue)
+ copyToClipboard(value)
+ }
+ }
+
useKeyboardShortcuts(
{
'Command+c': (event: any) => {
@@ -20,6 +35,8 @@ const Results = ({ rows }: { rows: readonly any[] }) => {
['INPUT', 'TEXTAREA'] as any
)
+ const { show: showContextMenu } = useContextMenu()
+
if (rows.length <= 0) {
return (
<div className="bg-table-header-light dark:bg-table-header-dark">
@@ -29,12 +46,23 @@ const Results = ({ rows }: { rows: readonly any[] }) => {
}
const formatter = (column: any, row: any) => {
- return <span className="font-mono text-xs">{JSON.stringify(row[column])}</span>
+ return (
+ <span
+ className="font-mono text-xs w-full"
+ onContextMenu={(e) =>
+ showContextMenu(e, {
+ id: SQL_CONTEXT_EDITOR_ID,
+ })
+ }
+ >
+ {JSON.stringify(row[column])}
+ </span>
+ )
}
const columnRender = (name: string) => {
return <div className="flex h-full items-center justify-center font-mono">{name}</div>
}
- const columns = Object.keys(rows[0]).map((key) => ({
+ const columns: Column<any, unknown>[] = Object.keys(rows[0]).map((key) => ({
key,
name: key,
formatter: ({ row }: any) => formatter(key, row),
@@ -47,23 +75,34 @@ const Results = ({ rows }: { rows: readonly any[] }) => {
setCellPosition(position)
}
- function onCopyCell() {
- if (columns && cellPosition) {
- const { idx, rowIdx } = cellPosition
- const colKey = columns[idx].key
- const cellValue = rows[rowIdx]?.[colKey] ?? ''
- const value = formatClipboardValue(cellValue)
- copyToClipboard(value)
- }
- }
+ const [mounted, setMounted] = useState(false)
+ useEffect(() => {
+ if (!mounted) setMounted(true)
+ }, [])
return (
- <DataGrid
- columns={columns}
- rows={rows}
- style={{ height: '100%' }}
- onSelectedCellChange={onSelectedCellChange}
- />
+ <>
+ <DataGrid
+ columns={columns}
+ rows={rows}
+ style={{ height: '100%' }}
+ onSelectedCellChange={onSelectedCellChange}
+ />
+ {mounted &&
+ createPortal(
+ <Menu
+ id={SQL_CONTEXT_EDITOR_ID}
+ animation={false}
+ className="!bg-scale-300 border border-scale-500"
+ >
+ <Item onClick={onCopyCell}>
+ <IconClipboard size="tiny" />
+ <span className="ml-2 text-xs">Copy cell content</span>
+ </Item>
+ </Menu>,
+ document.body
+ )}
+ </>
)
}
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityTabResults.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityTabResults.tsx
index 16385900ce6..f5c65472d6e 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityTabResults.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityTabResults.tsx
@@ -38,7 +38,7 @@ const UtilityTabResults = ({ id, isExecuting }: UtilityTabResultsProps) => {
return (
<div className="h-full">
- <Results rows={result.rows} />
+ <Results id={id} rows={result.rows} />
</div>
)
}