Skip to content

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

Save snippet for cmdk working properly

Joshen Lim468302e1afe9
diff --git a/studio/components/interfaces/App/CommandMenuWrapper.tsx b/studio/components/interfaces/App/CommandMenuWrapper.tsx
index e6e82b2b3c1..c060e33bdbe 100644
--- a/studio/components/interfaces/App/CommandMenuWrapper.tsx
+++ b/studio/components/interfaces/App/CommandMenuWrapper.tsx
@@ -1,25 +1,30 @@
import remarkGfm from 'remark-gfm'
import ReactMarkdown from 'react-markdown'
import { PropsWithChildren } from 'react'
+import { useParams } from 'common'
import { CommandMenuProvider } from 'ui'
-import { checkPermissions, useOptimisticSqlSnippetCreate, useStore } from 'hooks'
-import { useParams } from 'common/hooks'
import { PermissionAction } from '@supabase/shared-types/out/constants'
-import { useProfileQuery } from 'data/profile/profile-query'
-import { uuidv4 } from 'lib/helpers'
+
+import { checkPermissions, useStore } from 'hooks'
import { createSqlSnippetSkeleton } from '../SQLEditor/SQLEditor.utils'
+import { useSqlEditorStateSnapshot } from 'state/sql-editor'
+import { useProfileQuery } from 'data/profile/profile-query'
+import { SqlSnippet } from 'data/content/sql-snippets-query'
import { useProjectApiQuery } from 'data/config/project-api-query'
+import { useContentCreateMutation } from 'data/content/content-create-mutation'
const CommandMenuWrapper = ({ children }: PropsWithChildren<{}>) => {
const { ref } = useParams()
- const { content } = useStore()
+ const { ui } = useStore()
+ const snap = useSqlEditorStateSnapshot()
const { data: profile } = useProfileQuery()
const { data: settings } = useProjectApiQuery({ projectRef: ref })
- // const canCreateSQLSnippet = checkPermissions(PermissionAction.CREATE, 'user_content', {
- // resource: { type: 'sql', owner_id: profile?.id },
- // subject: { id: profile?.id },
- // })
+ const { mutateAsync: createContent } = useContentCreateMutation()
+ const canCreateSQLSnippet = checkPermissions(PermissionAction.CREATE, 'user_content', {
+ resource: { type: 'sql', owner_id: profile?.id },
+ subject: { id: profile?.id },
+ })
const apiKeys = {
anon: settings?.autoApiService?.defaultApiKey ?? undefined,
@@ -27,10 +32,18 @@ const CommandMenuWrapper = ({ children }: PropsWithChildren<{}>) => {
}
const onSaveGeneratedSQL = async (answer: string, resolve: any) => {
- // remove backticks from returned answer
- answer = answer.replace(/`/g, '')
+ if (!ref) return console.error('Project ref is required')
+ if (!canCreateSQLSnippet) {
+ return ui.setNotification({
+ category: 'info',
+ message: 'Your queries will not be saved as you do not have sufficient permissions',
+ })
+ }
+
+ // Remove markdown syntax from returned answer
+ answer = answer.replace(/`/g, '').replace(/sql\n/g, '')
- const formattedOutput = `
+ const formattedSql = `
-- Note: This query was generated via Supabase AI, please do verify the correctness of the
-- SQL snippet before running it against your database as we are not able to guarantee the
-- correctness of the snippet that was generated.
@@ -38,15 +51,32 @@ const CommandMenuWrapper = ({ children }: PropsWithChildren<{}>) => {
${answer}
`.trim()
- console.log('onSaveGeneratedSQL', { formattedOutput })
- const snippet = createSqlSnippetSkeleton({
- owner_id: profile?.id,
- name: 'Generated query',
- sql: formattedOutput,
- })
- const payload = { id: uuidv4(), ...snippet }
- await content.create(payload)
- resolve()
+ try {
+ const payload = createSqlSnippetSkeleton({
+ name: 'Generated query',
+ owner_id: profile?.id,
+ sql: formattedSql,
+ })
+ await createContent(
+ { projectRef: ref, payload },
+ {
+ onSuccess(data) {
+ snap.addSnippet(data.content[0] as SqlSnippet, ref)
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully saved snippet!`,
+ })
+ },
+ }
+ )
+ } catch (error: any) {
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to create new query: ${error.message}`,
+ })
+ } finally {
+ resolve()
+ }
}
return (
diff --git a/studio/pages/project/[ref]/sql/[id].tsx b/studio/pages/project/[ref]/sql/[id].tsx
index 866d9e39d98..fbc8c045d97 100644
--- a/studio/pages/project/[ref]/sql/[id].tsx
+++ b/studio/pages/project/[ref]/sql/[id].tsx
@@ -53,7 +53,6 @@ const SqlEditor: NextPageWithLayout = () => {
projectRef: project?.ref,
connectionString: project?.connectionString,
})
- console.log({ schemas, tableColumns, keywords, functions })
const pgInfoRef = useRef<any>(null)
const formatPgsqlRef = useRef(formatPgsql)