import { PermissionAction } from '@supabase/shared-types/out/constants' import { partition } from 'lodash' import { observer } from 'mobx-react-lite' import { useParams, useTelemetryProps } from 'common' import { SQL_TEMPLATES, sqlAiDisclaimerComment, } from 'components/interfaces/SQLEditor/SQLEditor.constants' import { useSqlGenerateMutation } from 'data/ai/sql-generate-mutation' import { SqlSnippet } from 'data/content/sql-snippets-query' import { m } from 'framer-motion' import { useCheckPermissions, useStore } from 'hooks' import { uuidv4 } from 'lib/helpers' import { useProfile } from 'lib/profile' import Telemetry from 'lib/telemetry' import { useRouter } from 'next/router' import { format } from 'sql-formatter' import { useSqlEditorStateSnapshot } from 'state/sql-editor' import { AiIcon, IconCornerDownLeft, Input } from 'ui' import { createSqlSnippetSkeleton } from '../SQLEditor.utils' import SQLCard from './SQLCard' const SQLTemplates = observer(() => { const { ui } = useStore() const { ref } = useParams() const router = useRouter() const { profile } = useProfile() const [sql, quickStart] = partition(SQL_TEMPLATES, { type: 'template' }) const { mutateAsync: generateSql, isLoading: isSqlGenerateLoading } = useSqlGenerateMutation() const telemetryProps = useTelemetryProps() const snap = useSqlEditorStateSnapshot() const canCreateSQLSnippet = useCheckPermissions(PermissionAction.CREATE, 'user_content', { resource: { type: 'sql', owner_id: profile?.id }, subject: { id: profile?.id }, }) // [Joshen TODO] Removed optimistic query creation logic for now, need to figure out // how to do that after using ids as part of the URL const handleNewQuery = async (sql: string, name: string) => { 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', }) } try { const snippet = createSqlSnippetSkeleton({ name, sql, owner_id: profile?.id }) const data = { ...snippet, id: uuidv4() } snap.addSnippet(data as SqlSnippet, ref, true) router.push(`/project/${ref}/sql/${data.id}`) } catch (error: any) { ui.setNotification({ category: 'error', message: `Failed to create new query: ${error.message}`, }) } } return (
What do you want to build?
{!isSqlGenerateLoading ? ( { setTimeout(() => { inputElement?.focus() }, 200) }} icon={ } inputClassName="w-full border-none py-4 focus:!ring-0" iconContainerClassName="transition text-scale-800 text-brand-900" placeholder="Ask Supabase AI to build a query" className="w-full" onKeyPress={async (e) => { if (e.key === 'Enter') { try { const value = e.currentTarget.value if (!value) { return } const { title, sql } = await generateSql({ prompt: e.currentTarget.value }) const formattedSql = sqlAiDisclaimerComment + '\n\n' + format(sql, { language: 'postgresql', keywordCase: 'lower', }) await handleNewQuery(formattedSql, title) } catch (error: unknown) { if ( error && typeof error === 'object' && 'message' in error && typeof error.message === 'string' ) { ui.setNotification({ category: 'error', message: error.message, }) } } } }} actions={
} />
) : ( )}

Scripts

Quick scripts to run on your database.

Click on any script to fill the query box, modify the script, then click Run.

{sql.map((x) => ( { handleNewQuery(sql, title) Telemetry.sendEvent( { category: 'scripts', action: 'script_clicked', label: x.title, }, telemetryProps, router ) }} /> ))}

Quick start

While we're in beta, we want to offer a quick way to explore Supabase. While we build importers, check out these simple starters.

Click on any script to fill the query box, modify the script, then click Run.

{quickStart.map((x) => ( { handleNewQuery(sql, title) Telemetry.sendEvent( { category: 'quickstart', action: 'quickstart_clicked', label: x.title, }, telemetryProps, router ) }} /> ))}
) }) export default SQLTemplates