import { partition } from 'lodash' import { observer } from 'mobx-react-lite' import { PermissionAction } from '@supabase/shared-types/out/constants' import Telemetry from 'lib/telemetry' import { checkPermissions, useStore } from 'hooks' import { useProfileQuery } from 'data/profile/profile-query' import { SQL_TEMPLATES } from 'components/interfaces/SQLEditor/SQLEditor.constants' import SQLCard from './SQLCard' import { createSqlSnippetSkeleton } from '../SQLEditor.utils' import { useTelemetryProps, useParams } from 'common' import { useRouter } from 'next/router' import { SqlSnippet } from 'data/content/sql-snippets-query' import { useSqlEditorStateSnapshot } from 'state/sql-editor' import { uuidv4 } from 'lib/helpers' const SQLTemplates = observer(() => { const { ui } = useStore() const { ref } = useParams() const router = useRouter() const { data: profile } = useProfileQuery() const [sql, quickStart] = partition(SQL_TEMPLATES, { type: 'template' }) const telemetryProps = useTelemetryProps() const snap = useSqlEditorStateSnapshot() const canCreateSQLSnippet = checkPermissions(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 (

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