import { PermissionAction } from '@supabase/shared-types/out/constants' import { partition } from 'lodash' import { observer } from 'mobx-react-lite' import { useTelemetryProps } from 'common' import { SQL_TEMPLATES } from 'components/interfaces/SQLEditor/SQLEditor.constants' import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext' import { SqlSnippet } from 'data/content/sql-snippets-query' 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 { useSqlEditorStateSnapshot } from 'state/sql-editor' import { createSqlSnippetSkeleton } from '../SQLEditor.utils' import SQLCard from './SQLCard' const SQLTemplates = observer(() => { const { ui } = useStore() const router = useRouter() const { profile } = useProfile() const { project } = useProjectContext() const [sql] = partition(SQL_TEMPLATES, { type: 'template' }) const telemetryProps = useTelemetryProps() const snap = useSqlEditorStateSnapshot() const canCreateSQLSnippet = useCheckPermissions(PermissionAction.CREATE, 'user_content', { resource: { type: 'sql', owner_id: profile?.id }, subject: { id: profile?.id }, }) const handleNewQuery = async (sql: string, name: string) => { if (!project) return console.error('Project is required') if (!profile) return console.error('Profile 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({ id: uuidv4(), name, sql, owner_id: profile.id, project_id: project.id, }) snap.addSnippet(snippet as SqlSnippet, project.ref) snap.addNeedsSaving(snippet.id!) router.push(`/project/${project.ref}/sql/${snippet.id}`) } catch (error: any) { ui.setNotification({ category: 'error', message: `Failed to create new query: ${error.message}`, }) } } return (
Quick scripts to run on your database.
Click on any script to fill the query box, modify the script, then click Run.