Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Update sql store to support add and delete snippets

Joshen Lim543534581583
diff --git a/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx b/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
index 3e52f968615..7aae99dc9f0 100644
--- a/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
@@ -11,6 +11,8 @@ import { useContentCreateMutation } from 'data/content/content-create-mutation'
import { createSqlSnippetSkeleton } from '../SQLEditor.utils'
import { useParams } from 'common'
import { useRouter } from 'next/router'
+import { SqlSnippet } from 'data/content/sql-snippets-query'
+import { useSqlEditorStateSnapshot } from 'state/sql-editor'
const SQLTemplates = observer(() => {
const { ui } = useStore()
@@ -19,6 +21,7 @@ const SQLTemplates = observer(() => {
const { data: profile } = useProfileQuery()
const [sql, quickStart] = partition(SQL_TEMPLATES, { type: 'template' })
+ const snap = useSqlEditorStateSnapshot()
const { mutateAsync: createContent } = useContentCreateMutation()
const canCreateSQLSnippet = checkPermissions(PermissionAction.CREATE, 'user_content', {
resource: { type: 'sql', owner_id: profile?.id },
@@ -38,7 +41,14 @@ const SQLTemplates = observer(() => {
try {
const payload = createSqlSnippetSkeleton({ sql, name, owner_id: profile?.id })
- const response = await createContent({ projectRef: ref, payload })
+ const response = await createContent(
+ { projectRef: ref, payload },
+ {
+ onSuccess(data) {
+ snap.addSnippet(data.content[0] as SqlSnippet, ref)
+ },
+ }
+ )
const snippetId = response.content[0].id
router.push(`/project/${ref}/sql/${snippetId}`)
} catch (error: any) {
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/FavoriteButton.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/FavoriteButton.tsx
index 6cb1432b542..667b659f253 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/FavoriteButton.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/FavoriteButton.tsx
@@ -7,10 +7,12 @@ import { useSqlEditorStateSnapshot } from 'state/sql-editor'
export type FavoriteButtonProps = { id: string }
const FavoriteButton = ({ id }: FavoriteButtonProps) => {
+ const client = useQueryClient()
const { project } = useProjectContext()
const snap = useSqlEditorStateSnapshot()
- const isFavorite = snap.snippets[id].snippet.content.favorite
- const client = useQueryClient()
+
+ const snippet = snap.snippets[id]
+ const isFavorite = snippet !== undefined ? snippet.snippet.content.favorite : false
async function addFavorite() {
snap.addFavorite(id)
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
index a8a2fb16c3a..ed7e77105b1 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
@@ -42,6 +42,7 @@ const SizeToggleButton = ({ id }: { id: string }) => {
snap.restoreUtilityPanel(id)
}
+ if (!snippet) return null
return snippet.utilityPanelCollapsed ? (
<Button
type="text"
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityTabResults.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityTabResults.tsx
index 0a4b24aaa22..8c345178893 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityTabResults.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityTabResults.tsx
@@ -9,10 +9,12 @@ export type UtilityTabResultsProps = {
const UtilityTabResults = ({ id, isExecuting }: UtilityTabResultsProps) => {
const snap = useSqlEditorStateSnapshot()
- const utilityPanelCollapsed = snap.snippets[id].utilityPanelCollapsed
- const result = snap.results[id][0]
+ const snippet = snap.snippets[id]
+ const utilityPanelCollapsed = snippet?.utilityPanelCollapsed ?? false
+ const result = snap.results[id]?.[0]
if (utilityPanelCollapsed) return null
+
if (isExecuting) {
return (
<div className="bg-table-header-light dark:bg-table-header-dark">
diff --git a/studio/components/layouts/SQLEditorLayout/QueryItem.tsx b/studio/components/layouts/SQLEditorLayout/QueryItem.tsx
index fc80ba9a50a..e8484e7608d 100644
--- a/studio/components/layouts/SQLEditorLayout/QueryItem.tsx
+++ b/studio/components/layouts/SQLEditorLayout/QueryItem.tsx
@@ -10,6 +10,8 @@ import { Dropdown, IconEdit2, IconTrash, Button, IconChevronDown, Modal } from '
import RenameQueryModal from 'components/interfaces/SQLEditor/RenameQueryModal'
import clsx from 'clsx'
import Link from 'next/link'
+import { useSqlEditorStateSnapshot } from 'state/sql-editor'
+import { useRouter } from 'next/router'
export interface QueryItemProps {
tabInfo: QueryTab
@@ -48,6 +50,8 @@ export default QueryItem
const QueryItemActions = observer(({ tabInfo }: { tabInfo: QueryTab }) => {
const { ui } = useStore()
const { ref } = useParams()
+ const router = useRouter()
+ const snap = useSqlEditorStateSnapshot()
const { mutateAsync: deleteContent } = useContentDeleteMutation()
const { id, name } = tabInfo || {}
@@ -73,7 +77,14 @@ const QueryItemActions = observer(({ tabInfo }: { tabInfo: QueryTab }) => {
try {
await deleteContent({ projectRef: ref, id })
- // [Joshen TODO] Needs to load another tab through router push
+ snap.removeSnippet(id)
+
+ const existingSnippetIds = Object.keys(snap.snippets)
+ if (existingSnippetIds.length === 0) {
+ router.push(`/project/${ref}/sql`)
+ } else {
+ router.push(`/project/${ref}/sql/${existingSnippetIds[0]}`)
+ }
} catch (error: any) {
ui.setNotification({
category: 'error',
@@ -95,7 +106,7 @@ const QueryItemActions = observer(({ tabInfo }: { tabInfo: QueryTab }) => {
</Dropdown.Item>
<Dropdown.Separator />
<Dropdown.Item onClick={onClickDelete} icon={<IconTrash size="tiny" />}>
- Remove query
+ Delete query
</Dropdown.Item>
</>
}
@@ -119,14 +130,15 @@ const QueryItemActions = observer(({ tabInfo }: { tabInfo: QueryTab }) => {
/>
<ConfirmationModal
- header="Confirm to remove"
- buttonLabel="Confirm"
+ header="Confirm to delete"
+ buttonLabel="Delete query"
+ buttonLoadingLabel="Deleting query"
visible={deleteModalOpen}
onSelectConfirm={onConfirmDelete}
onSelectCancel={() => setDeleteModalOpen(false)}
>
<Modal.Content>
- <p className="py-4 text-sm text-scale-1100">{`Are you sure you want to remove '${name}' ?`}</p>
+ <p className="py-4 text-sm text-scale-1100">{`Are you sure you want to delete '${name}' ?`}</p>
</Modal.Content>
</ConfirmationModal>
</div>
diff --git a/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx b/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
index 05bd2cdb057..3e636445bf2 100644
--- a/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
+++ b/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
@@ -11,7 +11,7 @@ import { useProfileQuery } from 'data/profile/profile-query'
import ProductMenuItem from 'components/ui/ProductMenu/ProductMenuItem'
import { useParams } from 'common'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
-import { useSqlSnippetsQuery } from 'data/content/sql-snippets-query'
+import { SqlSnippet, useSqlSnippetsQuery } from 'data/content/sql-snippets-query'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import QueryItem from './QueryItem'
import { createSqlSnippetSkeleton } from 'components/interfaces/SQLEditor/SQLEditor.utils'
@@ -67,7 +67,14 @@ const SideBarContent = observer(() => {
try {
const payload = createSqlSnippetSkeleton({ name: 'Untitled query', owner_id: profile?.id })
- const response = await createContent({ projectRef: ref, payload })
+ const response = await createContent(
+ { projectRef: ref, payload },
+ {
+ onSuccess(data) {
+ snap.addSnippet(data.content[0] as SqlSnippet, ref)
+ },
+ }
+ )
const snippetId = response.content[0].id
router.push(`/project/${ref}/sql/${snippetId}`)
} catch (error: any) {
diff --git a/studio/hooks/misc/useOptimisticSqlSnippetCreate.ts b/studio/hooks/misc/useOptimisticSqlSnippetCreate.ts
index e94d0b1890d..ed9897bc498 100644
--- a/studio/hooks/misc/useOptimisticSqlSnippetCreate.ts
+++ b/studio/hooks/misc/useOptimisticSqlSnippetCreate.ts
@@ -1,14 +1,13 @@
import { createSqlSnippetSkeleton } from 'components/interfaces/SQLEditor/SQLEditor.utils'
import { useProfileQuery } from 'data/profile/profile-query'
-import { useSqlStore } from 'localStores/sqlEditor/SqlEditorStore'
+// import { useSqlStore } from 'localStores/sqlEditor/SqlEditorStore'
import { useCallback } from 'react'
import { useStore } from './useStore'
import { useContentCreateMutation } from 'data/content/content-create-mutation'
import { useParams } from 'common'
import { uuidv4 } from 'lib/helpers'
-// [Joshen] Can we not call checkPermissions in here? rather than passing it in as an arg
-// [Joshen] Choosing to deprecate this tbh
+// [Joshen] Currently not using this after the SQL store refactor to valtio/RQ
export function useOptimisticSqlSnippetCreate(canCreateSQLSnippet: boolean) {
const { ref } = useParams()
diff --git a/studio/state/sql-editor.ts b/studio/state/sql-editor.ts
index a9f3308f24c..40b1ef1a62f 100644
--- a/studio/state/sql-editor.ts
+++ b/studio/state/sql-editor.ts
@@ -30,19 +30,28 @@ export const sqlEditorState = proxy({
setInitialSnippets: (snippets: SqlSnippet[], projectRef: string) => {
snippets.forEach((snippet) => {
- if (snippet.id && !sqlEditorState.snippets[snippet.id]) {
- sqlEditorState.snippets[snippet.id] = {
- snippet,
- splitSizes: [50, 50],
- utilityPanelCollapsed: false,
- projectRef,
- }
- sqlEditorState.results[snippet.id] = []
- sqlEditorState.savingStates[snippet.id] = 'IDLE'
+ sqlEditorState.addSnippet(snippet, projectRef)
+ })
+ },
+ addSnippet: (snippet: SqlSnippet, projectRef: string) => {
+ if (snippet.id && !sqlEditorState.snippets[snippet.id]) {
+ sqlEditorState.snippets[snippet.id] = {
+ snippet,
+ splitSizes: [50, 50],
+ utilityPanelCollapsed: false,
+ projectRef,
}
+ sqlEditorState.results[snippet.id] = []
+ sqlEditorState.savingStates[snippet.id] = 'IDLE'
+ }
+ sqlEditorState.loaded[projectRef] = true
+ },
+ removeSnippet: (id: string) => {
+ const { [id]: snippet, ...otherSnippets } = sqlEditorState.snippets
+ sqlEditorState.snippets = otherSnippets
- sqlEditorState.loaded[projectRef] = true
- })
+ const { [id]: result, ...otherResults } = sqlEditorState.results
+ sqlEditorState.results = otherResults
},
setSplitSizes: (id: string, splitSizes: number[]) => {
if (sqlEditorState.snippets[id]) {