import { PermissionAction } from '@supabase/shared-types/out/constants' import { useParams } from 'common' import { partition } from 'lodash' import { useRouter } from 'next/router' import { toast } from 'sonner' import { cn, SQL_ICON } from 'ui' import { createSqlSnippetSkeletonV2 } from '../SQLEditor.utils' import { SQL_TEMPLATES } from '@/components/interfaces/SQLEditor/SQLEditor.queries' import { ActionCard } from '@/components/layouts/Tabs/ActionCard' import { useSendEventMutation } from '@/data/telemetry/send-event-mutation' import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { useProfile } from '@/lib/profile' import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor-v2' const SQLQuickstarts = () => { const router = useRouter() const { ref } = useParams() const { profile } = useProfile() const { data: project } = useSelectedProjectQuery() const { data: org } = useSelectedOrganizationQuery() const [, quickStart] = partition(SQL_TEMPLATES, { type: 'template' }) const snapV2 = useSqlEditorV2StateSnapshot() const { can: canCreateSQLSnippet } = useAsyncCheckPermissions( PermissionAction.CREATE, 'user_content', { resource: { type: 'sql', owner_id: profile?.id }, subject: { id: profile?.id }, } ) const { mutate: sendEvent } = useSendEventMutation() const handleNewQuery = async (sql: string, name: string) => { if (!ref) return console.error('Project ref is required') if (!project) return console.error('Project is required') if (!profile) return console.error('Profile is required') if (!canCreateSQLSnippet) { return toast('Your queries will not be saved as you do not have sufficient permissions') } try { const snippet = createSqlSnippetSkeletonV2({ name, sql, owner_id: profile?.id, project_id: project?.id, }) snapV2.addSnippet({ projectRef: ref, snippet }) snapV2.addNeedsSaving(snippet.id) router.push(`/project/${ref}/sql/${snippet.id}`) } catch (error: any) { toast.error(`Failed to create new query: ${error.message}`) } } return (

Quickstarts

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

{quickStart.map((x, i) => ( } // sql={x.sql} onClick={() => { handleNewQuery(x.sql, x.title) sendEvent({ action: 'sql_editor_quickstart_clicked', properties: { quickstartName: x.title }, groups: { project: ref ?? 'Unknown', organization: org?.slug ?? 'Unknown' }, }) }} /> ))}
) } export default SQLQuickstarts