Skip to content

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

Fix unnecessary GET content request when writing a new SQL query (#35709)

Joshen Lim3487831a4844
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
index 01ebe12e902..efc327aba41 100644
--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
@@ -68,6 +68,7 @@ export const createSqlSnippetSkeletonV2 = ({
content_id: id ?? '',
sql: sql ?? '',
} as any,
+ isNotSavedInDatabaseYet: true,
}
}
diff --git a/apps/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx
index 011a66553fa..1ba28f268e9 100644
--- a/apps/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx
@@ -6,7 +6,6 @@ import { useCallback } from 'react'
import { useParams } from 'common'
import { SupabaseGrid } from 'components/grid/SupabaseGrid'
import { useLoadTableEditorStateFromLocalStorageIntoUrl } from 'components/grid/SupabaseGrid.utils'
-import { useEditorType } from 'components/layouts/editors/EditorsLayout.hooks'
import {
Entity,
isMaterializedView,
@@ -19,6 +18,7 @@ import { useQuerySchemaState } from 'hooks/misc/useSchemaQueryState'
import { useSelectedProject } from 'hooks/misc/useSelectedProject'
import { useUrlState } from 'hooks/ui/useUrlState'
import { PROTECTED_SCHEMAS } from 'lib/constants/schemas'
+import Link from 'next/link'
import { useAppStateSnapshot } from 'state/app-state'
import { TableEditorTableStateContextProvider } from 'state/table-editor-table'
import { createTabId, useTabsStateSnapshot } from 'state/tabs'
@@ -39,7 +39,6 @@ export const TableGridEditor = ({
selectedTable,
}: TableGridEditorProps) => {
const router = useRouter()
- const editor = useEditorType()
const project = useSelectedProject()
const appSnap = useAppStateSnapshot()
const { ref: projectRef, id } = useParams()
@@ -66,9 +65,7 @@ export const TableGridEditor = ({
})
const onClearDashboardHistory = () => {
- if (projectRef && editor) {
- appSnap.setDashboardHistory(projectRef, editor === 'table' ? 'editor' : editor, undefined)
- }
+ if (projectRef) appSnap.setDashboardHistory(projectRef, 'editor', undefined)
}
const onTableCreated = useCallback(
@@ -83,7 +80,7 @@ export const TableGridEditor = ({
if (isTableEditorTabsEnabled && selectedTable) {
// Close tab
const tabId = createTabId(selectedTable.entity_type, { id: selectedTable.id })
- tabs.handleTabClose({ id: tabId, router, editor, onClearDashboardHistory })
+ tabs.handleTabClose({ id: tabId, router, editor: 'table', onClearDashboardHistory })
} else {
const tables = await getTables(selectedSchema)
if (tables.length > 0) {
@@ -117,22 +114,44 @@ export const TableGridEditor = ({
description="This table doesn't exist in your database"
>
{isTableEditorTabsEnabled && (
- <Button
- type="default"
- className="mt-2"
- onClick={() => {
- if (tabId) {
- tabs.handleTabClose({
- id: tabId,
- router,
- editor,
- onClearDashboardHistory,
- })
- }
- }}
- >
- Close tab
- </Button>
+ <>
+ {!!tabId ? (
+ <Button
+ type="default"
+ className="mt-2"
+ onClick={() => {
+ tabs.handleTabClose({
+ id: tabId,
+ router,
+ editor: 'table',
+ onClearDashboardHistory,
+ })
+ }}
+ >
+ Close tab
+ </Button>
+ ) : tabs.openTabs.length > 0 ? (
+ <Button
+ asChild
+ type="default"
+ className="mt-2"
+ onClick={() => appSnap.setDashboardHistory(projectRef, 'editor', undefined)}
+ >
+ <Link href={`/project/${projectRef}/editor/${tabs.openTabs[0].split('-')[1]}`}>
+ Close tab
+ </Link>
+ </Button>
+ ) : (
+ <Button
+ asChild
+ type="default"
+ className="mt-2"
+ onClick={() => appSnap.setDashboardHistory(projectRef, 'editor', undefined)}
+ >
+ <Link href={`/project/${projectRef}/editor`}>Head back</Link>
+ </Button>
+ )}
+ </>
)}
</Admonition>
</div>
diff --git a/apps/studio/pages/project/[ref]/sql/[id].tsx b/apps/studio/pages/project/[ref]/sql/[id].tsx
index 97b26a634a1..40d8b604b6a 100644
--- a/apps/studio/pages/project/[ref]/sql/[id].tsx
+++ b/apps/studio/pages/project/[ref]/sql/[id].tsx
@@ -6,35 +6,49 @@ import { useIsSQLEditorTabsEnabled } from 'components/interfaces/App/FeaturePrev
import { SQLEditor } from 'components/interfaces/SQLEditor/SQLEditor'
import DefaultLayout from 'components/layouts/DefaultLayout'
import { EditorBaseLayout } from 'components/layouts/editors/EditorBaseLayout'
+import { useEditorType } from 'components/layouts/editors/EditorsLayout.hooks'
import SQLEditorLayout from 'components/layouts/SQLEditorLayout/SQLEditorLayout'
import { SQLEditorMenu } from 'components/layouts/SQLEditorLayout/SQLEditorMenu'
import { useContentIdQuery } from 'data/content/content-id-query'
+import Link from 'next/link'
import { useAppStateSnapshot } from 'state/app-state'
import { SnippetWithContent, useSnippets, useSqlEditorV2StateSnapshot } from 'state/sql-editor-v2'
import { createTabId, useTabsStateSnapshot } from 'state/tabs'
import type { NextPageWithLayout } from 'types'
+import { Button } from 'ui'
+import { Admonition } from 'ui-patterns'
const SqlEditor: NextPageWithLayout = () => {
const router = useRouter()
const { id, ref, content, skip } = useParams()
+ const editor = useEditorType()
+ const tabs = useTabsStateSnapshot()
const appSnap = useAppStateSnapshot()
const snapV2 = useSqlEditorV2StateSnapshot()
- const tabs = useTabsStateSnapshot()
+ const isSqlEditorTabsEnabled = useIsSQLEditorTabsEnabled()
const allSnippets = useSnippets(ref!)
+ const snippet = allSnippets.find((x) => x.id === id)
+
+ const tabId = !!id ? tabs.openTabs.find((x) => x.endsWith(id)) : undefined
// [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
- const { data } = useContentIdQuery(
+ 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'),
+ enabled: Boolean(
+ id !== 'new' && typeof snapV2.addSnippet === 'function' && !snippet?.isNotSavedInDatabaseYet
+ ),
}
)
+ const snippetMissing =
+ isError && error.code === 404 && error.message.includes('Content not found')
+ const invalidId = isError && error.code === 400 && error.message.includes('Invalid uuid')
useEffect(() => {
if (ref && data) {
@@ -56,8 +70,6 @@ const SqlEditor: NextPageWithLayout = () => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [id, allSnippets, content])
- const isSqlEditorTabsEnabled = useIsSQLEditorTabsEnabled()
-
// Watch for route changes
useEffect(() => {
if (isSqlEditorTabsEnabled) {
@@ -78,6 +90,47 @@ const SqlEditor: NextPageWithLayout = () => {
}
}, [router.isReady, id, isSqlEditorTabsEnabled])
+ if (snippetMissing || invalidId) {
+ return (
+ <div className="flex items-center justify-center h-full">
+ <div className="w-[400px]">
+ <Admonition
+ type="default"
+ title={`Unable to find snippet with ID ${id}`}
+ description="This snippet doesn't exist in your project"
+ >
+ {isSqlEditorTabsEnabled && (
+ <>
+ {!!tabId ? (
+ <Button
+ type="default"
+ className="mt-2"
+ onClick={() => {
+ tabs.handleTabClose({
+ id: tabId,
+ router,
+ editor,
+ onClearDashboardHistory: () => {
+ if (ref) appSnap.setDashboardHistory(ref, 'sql', undefined)
+ },
+ })
+ }}
+ >
+ Close tab
+ </Button>
+ ) : (
+ <Button asChild type="default" className="mt-2">
+ <Link href={`/project/${ref}/sql`}>Head back</Link>
+ </Button>
+ )}
+ </>
+ )}
+ </Admonition>
+ </div>
+ </div>
+ )
+ }
+
return <SQLEditor />
}
diff --git a/apps/studio/state/sql-editor-v2.ts b/apps/studio/state/sql-editor-v2.ts
index 94046904784..a5d2e75897b 100644
--- a/apps/studio/state/sql-editor-v2.ts
+++ b/apps/studio/state/sql-editor-v2.ts
@@ -22,6 +22,7 @@ export type StateSnippetFolder = {
// [Joshen] API codegen is somehow missing the content property
export interface SnippetWithContent extends Snippet {
content?: SqlSnippets.Content
+ isNotSavedInDatabaseYet?: boolean
}
export type StateSnippet = {
@@ -304,6 +305,8 @@ async function upsertSnippet(
])
}
+ let snippet = sqlEditorState.snippets[id]?.snippet
+ if (snippet?.content) snippet.isNotSavedInDatabaseYet = false
sqlEditorState.savingStates[id] = 'IDLE'
} catch (error) {
sqlEditorState.savingStates[id] = 'UPDATING_FAILED'