Skip to content

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

Mitigate SQL Editor "Unable to find snippet" issue (#39364)

Joshen Limf142a77a1c73
diff --git a/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx b/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
index 97f4fdb668e..9d7c268d827 100644
--- a/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
@@ -1,8 +1,8 @@
import Editor, { Monaco, OnMount } from '@monaco-editor/react'
-import { debounce } from 'lodash'
import { useRouter } from 'next/router'
-import { MutableRefObject, useEffect, useRef } from 'react'
+import { MutableRefObject, useEffect, useRef, useState } from 'react'
+import { useDebounce } from '@uidotdev/usehooks'
import { LOCAL_STORAGE_KEYS, useParams } from 'common'
import { useLocalStorageQuery } from 'hooks/misc/useLocalStorage'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
@@ -51,9 +51,9 @@ const MonacoEditor = ({
const { profile } = useProfile()
const { ref, content } = useParams()
const { data: project } = useSelectedProjectQuery()
+
const snapV2 = useSqlEditorV2StateSnapshot()
const tabsSnap = useTabsStateSnapshot()
-
const aiSnap = useAiAssistantStateSnapshot()
const [intellisenseEnabled] = useLocalStorageQuery(
@@ -61,6 +61,10 @@ const MonacoEditor = ({
true
)
+ // [Joshen] Lodash debounce doesn't seem to be working here, so opting to use useDebounce
+ const [value, setValue] = useState('')
+ const debouncedValue = useDebounce(value, 1000)
+
const snippet = snapV2.snippets[id]
const disableEdit =
snippet?.snippet.visibility === 'project' && snippet?.snippet.owner_id !== profile?.id
@@ -162,34 +166,32 @@ const MonacoEditor = ({
onMount?.(editor)
}
- const debouncedSetSql = debounce((id, value) => snapV2.setSql(id, value), 1000)
-
function handleEditorChange(value: string | undefined) {
- // make active tab permanent
tabsSnap.makeActiveTabPermanent()
-
- const snippetCheck = snapV2.snippets[id]
-
if (id && value) {
- if (snippetCheck) {
- debouncedSetSql(id, value)
- } else {
- if (ref && profile !== undefined && project !== undefined) {
- const snippet = createSqlSnippetSkeletonV2({
- id,
- name: untitledSnippetTitle,
- sql: value,
- owner_id: profile?.id,
- project_id: project?.id,
- })
- snapV2.addSnippet({ projectRef: ref, snippet })
- snapV2.addNeedsSaving(snippet.id)
- router.push(`/project/${ref}/sql/${snippet.id}`, undefined, { shallow: true })
- }
+ if (!!snippet) {
+ setValue(value)
+ } else if (ref && !!profile && !!project) {
+ const snippet = createSqlSnippetSkeletonV2({
+ id,
+ name: untitledSnippetTitle,
+ sql: value,
+ owner_id: profile?.id,
+ project_id: project?.id,
+ })
+ snapV2.addSnippet({ projectRef: ref, snippet })
+ router.push(`/project/${ref}/sql/${snippet.id}`, undefined, { shallow: true })
}
}
}
+ useEffect(() => {
+ if (debouncedValue.length > 0 && snippet) {
+ snapV2.setSql(id, value)
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [debouncedValue])
+
// if an SQL query is passed by the content parameter, set the editor value to its content. This
// is usually used for sending the user to SQL editor from other pages with SQL.
useEffect(() => {
diff --git a/apps/studio/pages/project/[ref]/sql/[id].tsx b/apps/studio/pages/project/[ref]/sql/[id].tsx
index 958d6884175..b7c6c7fca39 100644
--- a/apps/studio/pages/project/[ref]/sql/[id].tsx
+++ b/apps/studio/pages/project/[ref]/sql/[id].tsx
@@ -36,17 +36,19 @@ const SqlEditor: NextPageWithLayout = () => {
// [Refactor] There's an unnecessary request getting triggered when we start typing while on /new
// the URL ID gets updated and we attempt to fetch content for a snippet that's not been created yet
+ // [Joshen] May need to investigate separately, but occasionally addSnippet doesnt exist in
+ // the snapV2 valtio store for some reason hence why the added typeof check here
+ const canFetchContentBasedOnId = Boolean(
+ id !== 'new' && typeof snapV2.addSnippet === 'function' && !snippet?.isNotSavedInDatabaseYet
+ )
const { data, error, isError } = useContentIdQuery(
{ projectRef: ref, id },
{
- // [Joshen] May need to investigate separately, but occasionally addSnippet doesnt exist in
- // the snapV2 valtio store for some reason hence why the added typeof check here
retry: false,
- enabled: Boolean(
- id !== 'new' && typeof snapV2.addSnippet === 'function' && !snippet?.isNotSavedInDatabaseYet
- ),
+ enabled: canFetchContentBasedOnId,
}
)
+
const snippetMissing =
isError && error.code === 404 && error.message.includes('Content not found')
const invalidId = isError && error.code === 400 && error.message.includes('Invalid uuid')