Files
supabase/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
T

129 lines
4.5 KiB
TypeScript

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 useProfile from 'hooks/misc/useProfile'
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 } = useProfile()
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 (
<div className="block h-full space-y-8 overflow-y-auto p-6">
<div>
<div className="mb-4">
<h1 className="text-scale-1200 mb-3 text-xl">Scripts</h1>
<p className="text-scale-1100 text-sm">Quick scripts to run on your database.</p>
<p className="text-scale-1100 text-sm">
Click on any script to fill the query box, modify the script, then click
<span className="text-code">Run</span>.
</p>
</div>
<div className="grid gap-4 sm:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4">
{sql.map((x) => (
<SQLCard
key={x.title}
title={x.title}
description={x.description}
sql={x.sql}
onClick={(sql, title) => {
handleNewQuery(sql, title)
Telemetry.sendEvent(
{
category: 'scripts',
action: 'script_clicked',
label: x.title,
},
telemetryProps,
router
)
}}
/>
))}
</div>
</div>
<div className="mb-8">
<div className="mb-4">
<h1 className="text-scale-1200 mb-3 text-xl">Quick start</h1>
<p className="text-scale-1100 text-sm">
While we're in beta, we want to offer a quick way to explore Supabase. While we build
importers, check out these simple starters.
</p>
<p className="text-scale-1100 text-sm">
Click on any script to fill the query box, modify the script, then click
<span className="text-code">Run</span>.
</p>
</div>
<div className="grid gap-4 sm:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4">
{quickStart.map((x) => (
<SQLCard
key={x.title}
title={x.title}
description={x.description}
sql={x.sql}
onClick={(sql, title) => {
handleNewQuery(sql, title)
Telemetry.sendEvent(
{
category: 'quickstart',
action: 'quickstart_clicked',
label: x.title,
},
telemetryProps,
router
)
}}
/>
))}
</div>
</div>
</div>
)
})
export default SQLTemplates