Skip to content

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

joshenlim/fe 4285 explorer missing prettify in query toolbar actions (#49675)

Joshen Limc0409b38b4b8
diff --git a/apps/studio/components/interfaces/Explorer/ExplorerNotebookTab.tsx b/apps/studio/components/interfaces/Explorer/ExplorerNotebookTab.tsx
index 4ad4e4159ce..06007bd406a 100644
--- a/apps/studio/components/interfaces/Explorer/ExplorerNotebookTab.tsx
+++ b/apps/studio/components/interfaces/Explorer/ExplorerNotebookTab.tsx
@@ -458,6 +458,7 @@ export const ExplorerNotebookTab = () => {
key={cell._id}
cell={cell}
onEdit={persistNotebookTab}
+ onPrettifyQuery={() => queryCellRefs.current.get(cell._id)?.prettify()}
ref={(instance) => {
if (instance) queryCellRefs.current.set(cell._id, instance)
else queryCellRefs.current.delete(cell._id)
diff --git a/apps/studio/components/interfaces/Explorer/ExplorerQueryTab.tsx b/apps/studio/components/interfaces/Explorer/ExplorerQueryTab.tsx
index 5cc0ad4a3bd..ca6e043997e 100644
--- a/apps/studio/components/interfaces/Explorer/ExplorerQueryTab.tsx
+++ b/apps/studio/components/interfaces/Explorer/ExplorerQueryTab.tsx
@@ -1,8 +1,9 @@
+import { type Hotkey } from '@tanstack/react-hotkeys'
import { useDebounce } from '@uidotdev/usehooks'
import { LOCAL_STORAGE_KEYS, useParams } from 'common'
-import { Check, Keyboard, Loader2, MoreVertical, Save, SquareCode } from 'lucide-react'
+import { AlignLeft, Check, Keyboard, Loader2, MoreVertical, Save, SquareCode } from 'lucide-react'
import { useRouter } from 'next/router'
-import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
+import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'
import { toast } from 'sonner'
import {
Button,
@@ -14,16 +15,18 @@ import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
+ DropdownMenuSeparator,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuTrigger,
+ KeyboardShortcut,
} from 'ui'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
import { ExplorerToolbarAction } from './ExplorerToolbar'
import { useCreateNotebook } from './hooks'
-import { QueryEditor, type ExplorerQueryModel } from './QueryEditor'
+import { QueryEditor, type ExplorerQueryModel, type QueryEditorHandle } from './QueryEditor'
import { type QueryDisplay, type QueryResult } from './types'
import { createQueryCellSkeleton } from './utils'
import { getNotebook } from '@/data/content/notebooks/notebook-query'
@@ -33,6 +36,8 @@ import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
import { explorerQueryState, useExplorerQueryStateSnapshot } from '@/state/explorer-query'
import { useNotebooksStateSnapshot } from '@/state/notebooks/notebooks-state'
import { useControlledRoleImpersonationState } from '@/state/role-impersonation-state'
+import { hotkeyToKeys } from '@/state/shortcuts/formatShortcut'
+import { SHORTCUT_DEFINITIONS, SHORTCUT_IDS } from '@/state/shortcuts/registry'
import { createTabId, TabsStateContext } from '@/state/tabs'
/** Query-tab lifecycle adapter around the shared QueryEditor. */
@@ -50,6 +55,12 @@ export const ExplorerQueryTab = () => {
true
)
+ const queryEditorRef = useRef<QueryEditorHandle>(null)
+
+ const hotkeySequnece: Hotkey | undefined =
+ SHORTCUT_DEFINITIONS[SHORTCUT_IDS.SQL_EDITOR_FORMAT].sequence[0]
+ const formatKeys = hotkeySequnece ? hotkeyToKeys(hotkeySequnece) : undefined
+
const [restoredQueryKey, setRestoredQueryKey] = useState<string>()
const [showQuery, setShowQuery] = useState(true)
const [search, setSearch] = useState('')
@@ -166,6 +177,7 @@ export const ExplorerQueryTab = () => {
return (
<QueryEditor
+ ref={queryEditorRef}
id={id}
variant="viewport"
title={draft.name}
@@ -255,7 +267,7 @@ export const ExplorerQueryTab = () => {
<DropdownMenuTrigger asChild>
<ExplorerToolbarAction icon={<MoreVertical size={16} strokeWidth={2} />} />
</DropdownMenuTrigger>
- <DropdownMenuContent>
+ <DropdownMenuContent className="w-48" align="end">
<DropdownMenuItem
className="justify-between"
onClick={() => setIsIntellisenseEnabled(!isIntellisenseEnabled)}
@@ -266,6 +278,17 @@ export const ExplorerQueryTab = () => {
</div>
{isIntellisenseEnabled && <Check className="text-brand" size={16} />}
</DropdownMenuItem>
+ <DropdownMenuSeparator />
+ <DropdownMenuItem
+ className="justify-between"
+ onClick={() => queryEditorRef.current?.prettify()}
+ >
+ <span className="flex items-center gap-x-2">
+ <AlignLeft size={14} />
+ Prettify SQL
+ </span>
+ {formatKeys && <KeyboardShortcut keys={formatKeys} />}
+ </DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</>
diff --git a/apps/studio/components/interfaces/Explorer/ExplorerToolbar.tsx b/apps/studio/components/interfaces/Explorer/ExplorerToolbar.tsx
index ce1d9467803..ce464262573 100644
--- a/apps/studio/components/interfaces/Explorer/ExplorerToolbar.tsx
+++ b/apps/studio/components/interfaces/Explorer/ExplorerToolbar.tsx
@@ -138,7 +138,7 @@ export type ExplorerToolbarActionProps = Omit<
React.ComponentPropsWithRef<typeof Button>,
'size' | 'variant'
> & {
- tooltip?: string
+ tooltip?: React.ReactNode
}
/**
diff --git a/apps/studio/components/interfaces/Explorer/QueryCell/index.tsx b/apps/studio/components/interfaces/Explorer/QueryCell/index.tsx
index 8f2183c1a55..cdbff562c8f 100644
--- a/apps/studio/components/interfaces/Explorer/QueryCell/index.tsx
+++ b/apps/studio/components/interfaces/Explorer/QueryCell/index.tsx
@@ -1,7 +1,10 @@
+import { AlignLeft } from 'lucide-react'
import { forwardRef, useState } from 'react'
+import { KeyboardShortcut } from 'ui'
import { type Snapshot } from 'valtio'
import { AddCellDropdown } from '../AddCellDropdown'
+import { ExplorerToolbarAction } from '../ExplorerToolbar'
import { MoveCellDropdownContent } from '../MoveCellDropdownContent'
import { QueryEditor, type QueryEditorHandle } from '../QueryEditor'
import { type QueryDisplay, type QueryResult } from '../types'
@@ -22,15 +25,22 @@ import {
import { type QuerySourceBinding } from '@/data/query-sources/query-source-registry'
import { useCurrentNotebook, useNotebooksStateSnapshot } from '@/state/notebooks/notebooks-state'
import { useLocalRoleImpersonationState } from '@/state/role-impersonation-state'
+import { hotkeyToKeys } from '@/state/shortcuts/formatShortcut'
+import { SHORTCUT_DEFINITIONS, SHORTCUT_IDS } from '@/state/shortcuts/registry'
+
+const PRETTIFY_SHORTCUT_KEYS = hotkeyToKeys(
+ SHORTCUT_DEFINITIONS[SHORTCUT_IDS.SQL_EDITOR_FORMAT].sequence[0]
+)
interface QueryCellProps {
cell: Snapshot<QueryCellSchema>
onEdit?: () => void
+ onPrettifyQuery?: () => void
}
/** Notebook adapter around the shared QueryEditor. */
export const QueryCell = forwardRef<QueryEditorHandle, QueryCellProps>(function QueryCell(
- { cell, onEdit },
+ { cell, onEdit, onPrettifyQuery },
ref
) {
const snap = useNotebooksStateSnapshot()
@@ -124,6 +134,18 @@ export const QueryCell = forwardRef<QueryEditorHandle, QueryCellProps>(function
onResultChange={setResult}
onRowLimitChange={handleRowLimitChange}
onDisplayChange={handleDisplayChange}
+ toolbarActions={
+ <ExplorerToolbarAction
+ icon={<AlignLeft />}
+ tooltip={
+ <div className="flex items-center gap-2.5">
+ <span>Prettify SQL</span>
+ <KeyboardShortcut keys={PRETTIFY_SHORTCUT_KEYS} />
+ </div>
+ }
+ onClick={onPrettifyQuery}
+ />
+ }
/>
</SortableSection>
)
diff --git a/apps/studio/components/interfaces/Explorer/QueryEditor/index.tsx b/apps/studio/components/interfaces/Explorer/QueryEditor/index.tsx
index 96fd8e4a7c9..11cfe9b4f2c 100644
--- a/apps/studio/components/interfaces/Explorer/QueryEditor/index.tsx
+++ b/apps/studio/components/interfaces/Explorer/QueryEditor/index.tsx
@@ -12,7 +12,7 @@ import {
useState,
type ReactNode,
} from 'react'
-import { Button, cn } from 'ui'
+import { Button, cn, KeyboardShortcut } from 'ui'
import { resolveLogTimeRange } from '../../QuerySources/LogTimeRange.utils'
import {
@@ -113,6 +113,8 @@ export type QueryEditorHandle = {
run: (force?: boolean) => Promise<void>
/** The editor's live text buffer, ahead of any blur-triggered commit to the store. */
getSql: () => string
+ /** Formats the editor's SQL in place and commits the result, same as the SQL Editor's Prettify SQL action. */
+ prettify: () => Promise<void>
}
type QueryEditorProps = {
@@ -350,9 +352,19 @@ export const QueryEditor = forwardRef<QueryEditorHandle, QueryEditorProps>(funct
const Shell = variant === 'viewport' ? ExplorerQueryViewport : ExplorerQuery
+ const handlePrettify = async () => {
+ if (pendingProposalRef.current) return
+
+ const editor = editorInstanceRef.current
+ if (!editor) return
+ await editor.getAction('editor.action.formatDocument')?.run()
+ onSqlCommitRef.current?.(editor.getValue())
+ }
+
useImperativeHandle(ref, () => ({
run: (force = false) => handleRunQuery({ shouldForce: force }),
getSql: () => sqlRef.current,
+ prettify: handlePrettify,
}))
useEffect(() => {
@@ -409,7 +421,12 @@ export const QueryEditor = forwardRef<QueryEditorHandle, QueryEditorProps>(funct
<ExplorerToolbarAction
icon={<Play size={16} strokeWidth={2} />}
loading={isExecuting}
- tooltip={hasSelection ? 'Run selected query' : 'Run query'}
+ tooltip={
+ <div className="flex items-center gap-2.5">
+ <span>{hasSelection ? 'Run selected query' : 'Run query'}</span>
+ <KeyboardShortcut keys={['Meta', 'Enter']} />
+ </div>
+ }
disabled={
isBusy || pendingProposal !== null || isRunDisabled || sql.trim().length === 0
}
@@ -494,6 +511,16 @@ export const QueryEditor = forwardRef<QueryEditorHandle, QueryEditorProps>(funct
if (selectionParts) setPromptState({ isOpen: true, ...selectionParts })
},
})
+
+ editor.addAction({
+ id: 'prettify-query',
+ label: 'Prettify SQL',
+ keybindings: [monaco.KeyMod.Alt | monaco.KeyMod.Shift | monaco.KeyCode.KeyF],
+ contextMenuGroupId: 'operation',
+ run: () => {
+ handlePrettify()
+ },
+ })
}}
/>