mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 19:05:06 +03:00
Third of a stack. **Stacked on #49070** (which is stacked on #49069) — review those first. Base retargets automatically as each merges. Mechanical throughout; no behavior change. ## The problem Three types described where a query runs, and no two agreed: | | shape | |---|---| | `CellSource` (registry) | `{ id, type, parameters: { … } }` — `id` and `type` always held the same literal | | `QuerySource` (SQL editor) | `{ type: 'database' } \| { type: 'logs', dateRange }` | | notebook cells | flat per-backend fields, neither of the above | Anything crossing between them needed a translation that dropped fields on the way — which is how a notebook cell's replica selection had nowhere to go. ## What changed One `QuerySourceBinding`: a backend `_tag` with that backend's parameters spread flat beside it, borrowed from the wire schema (#49069) so the binding and the persisted cell agree by construction. - **`QuerySource` is deleted.** `useRunSource` returns the shared binding, so `runSource.type`/`dateRange` become `_tag`/`time_range` across the SQL editor — that is most of the file count here. - **`getQuerySourceBinding`** projects a notebook cell onto a binding; **`toQuerySourceBinding`** does the same for any backend-tagged carrier. Both overloaded so an already-narrowed caller gets the matching binding back rather than the union, which keeps the result spreadable without re-narrowing. - **`ExplorerQuerySourceMenu`** drops its inline copy of the custom-range and upgrade-prompt logic in favor of `useLogsCustomRange`, which the SQL editor menu already used. The registry keeps only what is genuinely runtime: endpoints, labels, icons, availability, defaults. What a query *is* stays in the wire schema. ## Verification Typecheck, Prettier, and the lint ratchet clean. 405 tests pass across the notebook schema, query sources, the logs components, the SQL editor, and the Explorer surfaces. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Improvements** * Updated query source handling across Explorer and SQL Editor for a more consistent selection experience. * Database and log sources now preserve identifiers and time ranges more reliably when switching or editing queries. * Source menus, labels, icons, validation, and query execution now reflect the selected source more accurately. * **Bug Fixes** * Invalid or outdated saved source settings now safely fall back to a database source. * Improved log-source detection and time-range handling throughout query editing and execution. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
160 lines
5.1 KiB
TypeScript
160 lines
5.1 KiB
TypeScript
import { acceptUntrustedSql, untrustedSql } from '@supabase/pg-meta'
|
|
import { LOCAL_STORAGE_KEYS } from 'common'
|
|
import { Loader2 } from 'lucide-react'
|
|
import { useCallback } from 'react'
|
|
import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from 'ui'
|
|
|
|
import { RunQueryWarningModal } from './RunQueryWarningModal'
|
|
import type { UtilityTab } from './SQLEditor.types'
|
|
import { appendEnableRLSStatements } from './SQLEditor.utils'
|
|
import { useSQLEditorContext } from './SQLEditorContext'
|
|
import {
|
|
useSqlEditorAssistant,
|
|
useSqlEditorRun,
|
|
useSqlEditorSnippet,
|
|
useSqlEditorUi,
|
|
} from './SQLEditorControllers'
|
|
import { SQLEditorEditorPanel } from './SQLEditorEditorPanel'
|
|
import { UtilityActions } from './UtilityPanel/UtilityActions'
|
|
import { UtilityPanel } from './UtilityPanel/UtilityPanel'
|
|
import { acceptUntrustedLogsSql } from '@/data/logs/safe-analytics-sql'
|
|
|
|
const SQLEditorRunWarningModal = () => {
|
|
const { refocusEditor, clearPendingRunRefocus, markRefocusAfterRun } = useSQLEditorContext()
|
|
const { potentialIssues, resetPotentialIssues, executeQuery, readEditorSql } = useSqlEditorRun()
|
|
|
|
return (
|
|
<RunQueryWarningModal
|
|
visible={!!potentialIssues}
|
|
potentialIssues={potentialIssues}
|
|
onCancel={() => {
|
|
clearPendingRunRefocus()
|
|
resetPotentialIssues()
|
|
refocusEditor()
|
|
}}
|
|
onConfirm={() => {
|
|
markRefocusAfterRun()
|
|
resetPotentialIssues()
|
|
const sql = readEditorSql()
|
|
if (sql === undefined) return clearPendingRunRefocus()
|
|
// The user has reviewed the warning and confirmed — promote here.
|
|
void executeQuery(acceptUntrustedSql(sql), true)
|
|
}}
|
|
onConfirmWithRLS={() => {
|
|
const tables = potentialIssues?.createTablesMissingRLS ?? []
|
|
if (tables.length === 0) return
|
|
const baseSql = readEditorSql() ?? untrustedSql('')
|
|
const rewrittenSql = appendEnableRLSStatements(baseSql, tables)
|
|
markRefocusAfterRun()
|
|
resetPotentialIssues()
|
|
// The user has reviewed the warning and confirmed — promote here.
|
|
void executeQuery(acceptUntrustedSql(untrustedSql(rewrittenSql)), true)
|
|
}}
|
|
/>
|
|
)
|
|
}
|
|
|
|
/** The top-bar controls (run / prettify / etc.). */
|
|
const SQLEditorToolbar = () => {
|
|
const { clearPendingRunRefocus, markRefocusAfterRun } = useSQLEditorContext()
|
|
const { id } = useSqlEditorSnippet()
|
|
const { diff } = useSqlEditorAssistant()
|
|
const {
|
|
executeQuery,
|
|
readEditorSql,
|
|
isExecuting,
|
|
prettifyQuery,
|
|
runSource,
|
|
executeLogsQuery,
|
|
readEditorLogsSql,
|
|
} = useSqlEditorRun()
|
|
const { hasSelection } = useSqlEditorUi()
|
|
|
|
// Run gesture from the toolbar button — promote here, at the user action.
|
|
const runQuery = useCallback(() => {
|
|
markRefocusAfterRun()
|
|
if (runSource._tag === 'logs') {
|
|
const sql = readEditorLogsSql()
|
|
if (sql === undefined) return clearPendingRunRefocus()
|
|
void executeLogsQuery(acceptUntrustedLogsSql(sql))
|
|
} else {
|
|
const sql = readEditorSql()
|
|
if (sql === undefined) return clearPendingRunRefocus()
|
|
void executeQuery(acceptUntrustedSql(sql))
|
|
}
|
|
}, [
|
|
clearPendingRunRefocus,
|
|
executeLogsQuery,
|
|
executeQuery,
|
|
markRefocusAfterRun,
|
|
readEditorLogsSql,
|
|
readEditorSql,
|
|
runSource._tag,
|
|
])
|
|
|
|
return (
|
|
<UtilityActions
|
|
id={id}
|
|
runSource={runSource}
|
|
isExecuting={isExecuting}
|
|
isDisabled={diff.isDiffOpen}
|
|
hasSelection={hasSelection}
|
|
prettifyQuery={prettifyQuery}
|
|
executeQuery={runQuery}
|
|
className="px-4 min-h-[42px] border-b shrink-0"
|
|
/>
|
|
)
|
|
}
|
|
|
|
/** The bottom (results) resizable panel: loading state + utility panel. */
|
|
const SQLEditorResultsPanel = () => {
|
|
const { id, isLoading } = useSqlEditorSnippet()
|
|
const { diff, ai } = useSqlEditorAssistant()
|
|
const { isExecuting } = useSqlEditorRun()
|
|
const { activeUtilityTab, setActiveUtilityTab } = useSqlEditorUi()
|
|
|
|
return isLoading ? (
|
|
<div className="flex h-full w-full items-center justify-center">
|
|
<Loader2 className="animate-spin text-brand" />
|
|
</div>
|
|
) : (
|
|
<UtilityPanel
|
|
id={id}
|
|
isExecuting={isExecuting}
|
|
isDisabled={diff.isDiffOpen}
|
|
onDebug={ai.onDebug}
|
|
buildDebugPrompt={ai.buildDebugPrompt}
|
|
activeTab={activeUtilityTab}
|
|
onActiveTabChange={(tab) => setActiveUtilityTab(tab as UtilityTab)}
|
|
/>
|
|
)
|
|
}
|
|
|
|
/** The SQL editor shell: warning modal, toolbar, and the editor/results split. */
|
|
export const SQLEditorLayout = () => {
|
|
return (
|
|
<>
|
|
<SQLEditorRunWarningModal />
|
|
|
|
<div className="flex flex-col h-full">
|
|
<SQLEditorToolbar />
|
|
<ResizablePanelGroup
|
|
className="relative flex-1 min-h-0"
|
|
orientation="vertical"
|
|
autoSaveId={LOCAL_STORAGE_KEYS.SQL_EDITOR_SPLIT_SIZE}
|
|
>
|
|
<ResizablePanel defaultSize="50" maxSize="70">
|
|
<SQLEditorEditorPanel />
|
|
</ResizablePanel>
|
|
|
|
<ResizableHandle withHandle />
|
|
|
|
<ResizablePanel defaultSize="50" maxSize="70">
|
|
<SQLEditorResultsPanel />
|
|
</ResizablePanel>
|
|
</ResizablePanelGroup>
|
|
</div>
|
|
</>
|
|
)
|
|
}
|