import { acceptUntrustedSql, untrustedSql } from '@supabase/pg-meta' import { LOCAL_STORAGE_KEYS } from 'common' import { Loader2Icon } 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 ( { 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 ( ) } /** 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 ? (
) : ( setActiveUtilityTab(tab as UtilityTab)} /> ) } /** The SQL editor shell: warning modal, toolbar, and the editor/results split. */ export const SQLEditorLayout = () => { return ( <>
) }