Skip to content

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

feat(studio): add Copy as CSV option to SQL editor results (#44911)

Ali Waseem02325f00e747
diff --git a/apps/studio/components/interfaces/Account/Preferences/HotkeySettings.tsx b/apps/studio/components/interfaces/Account/Preferences/HotkeySettings.tsx
index 6f80db6e721..3e2e69524cc 100644
--- a/apps/studio/components/interfaces/Account/Preferences/HotkeySettings.tsx
+++ b/apps/studio/components/interfaces/Account/Preferences/HotkeySettings.tsx
@@ -22,6 +22,7 @@ const HotkeySchema = z.object({
inlineEditorEnabled: z.boolean(),
copyMarkdownEnabled: z.boolean(),
copyJsonEnabled: z.boolean(),
+ copyCsvEnabled: z.boolean(),
downloadCsvEnabled: z.boolean(),
})
@@ -46,6 +47,10 @@ export const HotkeySettings = () => {
LOCAL_STORAGE_KEYS.HOTKEY_COPY_JSON,
true
)
+ const [copyCsvEnabled, setCopyCsvEnabled] = useLocalStorageQuery(
+ LOCAL_STORAGE_KEYS.HOTKEY_COPY_CSV,
+ true
+ )
const [downloadCsvEnabled, setDownloadCsvEnabled] = useLocalStorageQuery(
LOCAL_STORAGE_KEYS.HOTKEY_DOWNLOAD_CSV,
true
@@ -59,6 +64,7 @@ export const HotkeySettings = () => {
inlineEditorEnabled: inlineEditorEnabled ?? true,
copyMarkdownEnabled: copyMarkdownEnabled ?? true,
copyJsonEnabled: copyJsonEnabled ?? true,
+ copyCsvEnabled: copyCsvEnabled ?? true,
downloadCsvEnabled: downloadCsvEnabled ?? true,
},
})
@@ -111,6 +117,13 @@ export const HotkeySettings = () => {
label="Copy results as JSON"
onToggle={setCopyJsonEnabled}
/>
+ <HotkeyToggle
+ form={form}
+ name="copyCsvEnabled"
+ keys={['Shift', 'Meta', 'c']}
+ label="Copy results as CSV"
+ onToggle={setCopyCsvEnabled}
+ />
<HotkeyToggle
form={form}
name="downloadCsvEnabled"
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
index 6bb4817b01c..ce37bc21a78 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
@@ -172,6 +172,12 @@ const UtilityPanel = ({
groups: { project: ref ?? '', organization: org?.slug ?? '' },
})
}}
+ onCopyAsCSV={() => {
+ sendEvent({
+ action: 'sql_editor_result_copy_csv_clicked',
+ groups: { project: ref ?? '', organization: org?.slug ?? '' },
+ })
+ }}
/>
)}
</div>
diff --git a/apps/studio/components/ui/DownloadResultsButton.tsx b/apps/studio/components/ui/DownloadResultsButton.tsx
index 6d8eb22be4e..72ce02cdd49 100644
--- a/apps/studio/components/ui/DownloadResultsButton.tsx
+++ b/apps/studio/components/ui/DownloadResultsButton.tsx
@@ -33,6 +33,7 @@ interface DownloadResultsButtonProps {
onDownloadAsCSV?: () => void
onCopyAsMarkdown?: () => void
onCopyAsJSON?: () => void
+ onCopyAsCSV?: () => void
}
export const DownloadResultsButton = ({
@@ -45,12 +46,14 @@ export const DownloadResultsButton = ({
onDownloadAsCSV,
onCopyAsMarkdown,
onCopyAsJSON,
+ onCopyAsCSV,
}: DownloadResultsButtonProps) => {
const { ref } = useParams()
const pathname = usePathname()
const isLogs = pathname?.includes?.('/logs') ?? false
const [copyMarkdownEnabled] = useLocalStorageQuery(LOCAL_STORAGE_KEYS.HOTKEY_COPY_MARKDOWN, true)
const [copyJsonEnabled] = useLocalStorageQuery(LOCAL_STORAGE_KEYS.HOTKEY_COPY_JSON, true)
+ const [copyCsvEnabled] = useLocalStorageQuery(LOCAL_STORAGE_KEYS.HOTKEY_COPY_CSV, true)
const [downloadCsvEnabled] = useLocalStorageQuery(LOCAL_STORAGE_KEYS.HOTKEY_DOWNLOAD_CSV, true)
const isEmpty = useMemo(() => results.length === 0, [results])
@@ -92,6 +95,18 @@ export const DownloadResultsButton = ({
})
}
+ const copyAsCSV = () => {
+ const csv = convertResultsToCSV(results)
+ if (!csv) {
+ toast('Results are empty')
+ return
+ }
+ copyToClipboard(csv, () => {
+ toast.success('Copied CSV to clipboard')
+ onCopyAsCSV?.()
+ })
+ }
+
useHotKey(
(e) => {
e.preventDefault()
@@ -110,6 +125,15 @@ export const DownloadResultsButton = ({
{ enabled: copyJsonEnabled ?? isEmpty, shift: true }
)
+ useHotKey(
+ (e) => {
+ e.preventDefault()
+ copyAsCSV()
+ },
+ 'c',
+ { enabled: copyCsvEnabled ?? isEmpty, shift: true }
+ )
+
useHotKey(
(e) => {
e.preventDefault()
@@ -155,6 +179,13 @@ export const DownloadResultsButton = ({
<KeyboardShortcut keys={['Shift', 'Meta', 'j']} />
</span>
</DropdownMenuItem>
+ <DropdownMenuItem onClick={copyAsCSV} className="gap-x-2">
+ <Copy size={14} />
+ <p>Copy as CSV</p>
+ <span className="ml-auto">
+ <KeyboardShortcut keys={['Shift', 'Meta', 'c']} />
+ </span>
+ </DropdownMenuItem>
<DropdownMenuItem className="gap-x-2" onClick={() => downloadAsCSV()}>
<Download size={14} />
<p>Download CSV</p>
diff --git a/packages/common/constants/local-storage.ts b/packages/common/constants/local-storage.ts
index 9f98d577c10..f94fcef69d9 100644
--- a/packages/common/constants/local-storage.ts
+++ b/packages/common/constants/local-storage.ts
@@ -96,6 +96,7 @@ export const LOCAL_STORAGE_KEYS = {
HOTKEY_COPY_MARKDOWN: 'supabase-dashboard-hotkey-copy-markdown',
HOTKEY_COPY_JSON: 'supabase-dashboard-hotkey-copy-json',
+ HOTKEY_COPY_CSV: 'supabase-dashboard-hotkey-copy-csv',
HOTKEY_DOWNLOAD_CSV: 'supabase-dashboard-hotkey-download-csv',
// Index Advisor notice dismissed
diff --git a/packages/common/telemetry-constants.ts b/packages/common/telemetry-constants.ts
index a480cf2269f..44b90962571 100644
--- a/packages/common/telemetry-constants.ts
+++ b/packages/common/telemetry-constants.ts
@@ -659,6 +659,18 @@ export interface SqlEditorResultCopyJsonClickedEvent {
groups: TelemetryGroups
}
+/**
+ * User clicked the "Result copy CSV" button in the SQL editor
+ *
+ * @group Events
+ * @source studio
+ * @page /dashboard/project/{ref}/sql
+ */
+export interface SqlEditorResultCopyCsvClickedEvent {
+ action: 'sql_editor_result_copy_csv_clicked'
+ groups: TelemetryGroups
+}
+
/**
* User submitted a prompt to the assistant sidebar.
*
@@ -3186,6 +3198,7 @@ export type TelemetryEvent =
| SqlEditorResultDownloadCsvClickedEvent
| SqlEditorResultCopyMarkdownClickedEvent
| SqlEditorResultCopyJsonClickedEvent
+ | SqlEditorResultCopyCsvClickedEvent
| AssistantPromptSubmittedEvent
| AssistantDebugSubmittedEvent
| AssistantSuggestionRunQueryClickedEvent