Skip to content

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

Bunch of changes to the editor to handle the non-existant snippet.

Ivan Vasilov715f6cff6ffe
diff --git a/studio/components/interfaces/SQLEditor/MonacoEditor.tsx b/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
index 660499d712e..04c13ff6dc6 100644
--- a/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
@@ -1,10 +1,17 @@
import Editor, { Monaco, OnMount } from '@monaco-editor/react'
-import { timeout } from 'lib/helpers'
+import { useParams } from 'common'
import { editor } from 'monaco-editor'
+import { useRouter } from 'next/router'
import { MutableRefObject, useRef, useState } from 'react'
+import { cn } from 'ui'
+
+import { SqlSnippet } from 'data/content/sql-snippets-query'
+import { useSelectedProject } from 'hooks'
+import { useProfile } from 'lib/profile'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
+import { untitledSnippetTitle } from './SQLEditor.constants'
import { IStandaloneCodeEditor } from './SQLEditor.types'
-import { cn } from 'ui'
+import { createSqlSnippetSkeleton } from './SQLEditor.utils'
export type MonacoEditorProps = {
id: string
@@ -23,6 +30,11 @@ const MonacoEditor = ({
className,
executeQuery,
}: MonacoEditorProps) => {
+ const { ref } = useParams()
+ const router = useRouter()
+ const { profile } = useProfile()
+ const project = useSelectedProject()
+
const snap = useSqlEditorStateSnapshot({ sync: true })
const snippet = snap.snippets[id]
@@ -64,14 +76,28 @@ const MonacoEditor = ({
})
if (autoFocus) {
- await timeout(500)
+ if (editor.getValue().length === 1) editor.setPosition({ lineNumber: 1, column: 2 })
editor.focus()
}
}
function handleEditorChange(value: string | undefined) {
- if (id && value !== undefined) {
- snap.setSql(id, value)
+ if (id && value) {
+ if (snap.snippets[id]) {
+ snap.setSql(id, value)
+ } else {
+ const snippet = createSqlSnippetSkeleton({
+ id,
+ name: untitledSnippetTitle,
+ sql: value,
+ owner_id: profile?.id,
+ project_id: project?.id,
+ })
+ if (ref) {
+ snap.addSnippet(snippet as SqlSnippet, ref)
+ router.push(`/project/${ref}/sql/${snippet.id}`, undefined, { shallow: true })
+ }
+ }
}
}
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.tsx b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
index 281cf0791f4..4eca5076b69 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
@@ -189,19 +189,20 @@ const SQLEditor = () => {
const executeQuery = useCallback(
async (force: boolean = false) => {
- if (isDiffOpen) {
- return
- }
+ if (isDiffOpen) return
// use the latest state
const state = getSqlEditorStateSnapshot()
- const snippet = idRef.current && state.snippets[idRef.current]
+ const snippet = idRef.current ? state.snippets[idRef.current] : null
- if (project && snippet && !isExecuting && editorRef.current !== null) {
+ if (editorRef.current !== null && !isExecuting && project !== undefined) {
const editor = editorRef.current
const selection = editor.getSelection()
const selectedValue = selection ? editor.getModel()?.getValueInRange(selection) : undefined
- const sql = (selectedValue || editorRef.current?.getValue()) ?? snippet.snippet.content.sql
+
+ const sql = snippet
+ ? (selectedValue || editorRef.current?.getValue()) ?? snippet.snippet.content.sql
+ : selectedValue || editorRef.current?.getValue()
const containsDestructiveOperations = destructiveSqlRegex.some((regex) => regex.test(sql))
@@ -210,7 +211,7 @@ const SQLEditor = () => {
return
}
- if (idRef.current && snippet.snippet.name === untitledSnippetTitle) {
+ if (idRef.current && snippet?.snippet.name === untitledSnippetTitle) {
// Intentionally don't await title gen (lazy)
setAiTitle(idRef.current, sql)
}
@@ -297,9 +298,6 @@ const SQLEditor = () => {
setSelectedDiffType(DiffType.Modification)
setDebugSolution(undefined)
setSqlDiff(undefined)
- setTimeout(() => {
- inputRef.current?.focus()
- }, 0)
} finally {
setIsAcceptDiffLoading(false)
}
@@ -318,9 +316,6 @@ const SQLEditor = () => {
setDebugSolution(undefined)
setSqlDiff(undefined)
- setTimeout(() => {
- inputRef.current?.focus()
- }, 0)
}, [debugSolution, telemetryProps, router])
useEffect(() => {
@@ -483,7 +478,6 @@ const SQLEditor = () => {
}}
>
<Input_Shadcn_
- autoFocus
value={aiInput}
onChange={(e) => setAiInput(e.currentTarget.value)}
disabled={isDiffOpen}
@@ -838,7 +832,7 @@ const SQLEditor = () => {
id={id}
editorRef={editorRef}
isExecuting={isExecuting}
- autoFocus={false}
+ autoFocus
executeQuery={executeQuery}
/>
</motion.div>