Skip to content

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

fix snippet ordering in sidebar

Alaister Young6effd3a821df
diff --git a/studio/components/interfaces/App/CommandMenuWrapper.tsx b/studio/components/interfaces/App/CommandMenuWrapper.tsx
index c060e33bdbe..81745e56e65 100644
--- a/studio/components/interfaces/App/CommandMenuWrapper.tsx
+++ b/studio/components/interfaces/App/CommandMenuWrapper.tsx
@@ -44,9 +44,9 @@ const CommandMenuWrapper = ({ children }: PropsWithChildren<{}>) => {
answer = answer.replace(/`/g, '').replace(/sql\n/g, '')
const formattedSql = `
--- Note: This query was generated via Supabase AI, please do verify the correctness of the
--- SQL snippet before running it against your database as we are not able to guarantee the
--- correctness of the snippet that was generated.
+-- Note: This query was generated via Supabase AI, please verify the correctness of the
+-- SQL snippet before running it against your database as we are not able to guarantee it
+-- will do exactly what you requested the AI.
${answer}
`.trim()
diff --git a/studio/components/layouts/ProjectLayout/ProjectContext.tsx b/studio/components/layouts/ProjectLayout/ProjectContext.tsx
index aa78d0ac3ad..048d714c2f6 100644
--- a/studio/components/layouts/ProjectLayout/ProjectContext.tsx
+++ b/studio/components/layouts/ProjectLayout/ProjectContext.tsx
@@ -1,5 +1,5 @@
-import { useStore } from 'hooks'
import { useParams } from 'common/hooks'
+import { useStore } from 'hooks'
import { observer } from 'mobx-react-lite'
import { createContext, PropsWithChildren, useContext, useMemo } from 'react'
import { Project } from 'types'
diff --git a/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx b/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
index 1f786864920..e5f891f6f2c 100644
--- a/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
+++ b/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
@@ -10,7 +10,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 { useSnippets, useSqlEditorStateSnapshot } from 'state/sql-editor'
import { SqlSnippet, useSqlSnippetsQuery } from 'data/content/sql-snippets-query'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import QueryItem from './QueryItem'
@@ -26,17 +26,18 @@ const SideBarContent = observer(() => {
const [filterString, setFilterString] = useState('')
const snap = useSqlEditorStateSnapshot()
- const { data, isLoading, isSuccess } = useSqlSnippetsQuery(ref, {
+ const { isLoading, isSuccess } = useSqlSnippetsQuery(ref, {
onSuccess(data) {
- if (ref) snap.setInitialSnippets(data.snippets, ref)
+ if (ref) snap.setRemoteSnippets(data.snippets, ref)
},
})
const { mutateAsync: createContent } = useContentCreateMutation()
+ const snippets = useSnippets(ref)
+
const [favorites, queries] = useMemo(
- () =>
- data?.snippets ? partition(data.snippets, (snippet) => snippet.content.favorite) : [[], []],
- [data?.snippets]
+ () => (snippets ? partition(snippets, (snippet) => snippet.content.favorite) : [[], []]),
+ [snippets]
)
const favouriteTabs =
diff --git a/studio/pages/project/[ref]/sql/[id].tsx b/studio/pages/project/[ref]/sql/[id].tsx
index fbc8c045d97..538f48a21ca 100644
--- a/studio/pages/project/[ref]/sql/[id].tsx
+++ b/studio/pages/project/[ref]/sql/[id].tsx
@@ -131,4 +131,5 @@ const SqlEditor: NextPageWithLayout = () => {
}
SqlEditor.getLayout = (page) => <SQLEditorLayout title="SQL">{page}</SQLEditorLayout>
+
export default observer(SqlEditor)
diff --git a/studio/pages/project/[ref]/sql/index.tsx b/studio/pages/project/[ref]/sql/index.tsx
index 2de4268bb44..bcbeb763a7e 100644
--- a/studio/pages/project/[ref]/sql/index.tsx
+++ b/studio/pages/project/[ref]/sql/index.tsx
@@ -8,4 +8,5 @@ const SqlEditorWelcome: NextPageWithLayout = () => {
}
SqlEditorWelcome.getLayout = (page) => <SQLEditorLayout title="SQL">{page}</SQLEditorLayout>
+
export default observer(SqlEditorWelcome)
diff --git a/studio/state/sql-editor.ts b/studio/state/sql-editor.ts
index 6a8f28fca77..b0f3157dc39 100644
--- a/studio/state/sql-editor.ts
+++ b/studio/state/sql-editor.ts
@@ -1,17 +1,20 @@
import { Content } from 'data/content/content-query'
-import { SqlSnippet } from 'data/content/sql-snippets-query'
import { updateContent } from 'data/content/content-update-mutation'
+import { SqlSnippet } from 'data/content/sql-snippets-query'
import { debounce, memoize } from 'lodash'
+import { useMemo } from 'react'
import { proxy, snapshot, subscribe, useSnapshot } from 'valtio'
import { devtools, proxySet } from 'valtio/utils'
+export type StateSnippet = {
+ snippet: SqlSnippet
+ splitSizes: number[]
+ projectRef: string
+}
+
export const sqlEditorState = proxy({
snippets: {} as {
- [key: string]: {
- snippet: SqlSnippet
- splitSizes: number[]
- projectRef: string
- }
+ [key: string]: StateSnippet
},
results: {} as {
[key: string]: {
@@ -19,15 +22,37 @@ export const sqlEditorState = proxy({
error?: any
}[]
},
+ orders: {} as {
+ [key: string]: string[]
+ },
loaded: {} as {
- [key: string]: true
+ [key: string]: boolean
},
+
needsSaving: proxySet<string>([]),
savingStates: {} as {
[key: string]: 'IDLE' | 'UPDATING' | 'UPDATING_FAILED'
},
- setInitialSnippets: (snippets: SqlSnippet[], projectRef: string) => {
+ setRemoteSnippets: (snippets: SqlSnippet[], projectRef: string) => {
+ if (!sqlEditorState.orders[projectRef]) {
+ const orderedSnippets = snippets
+ .filter((s) => Boolean(s.id))
+ .sort((a, b) => {
+ // sort by snippet.inserted_at, falling back to snippet.id
+ if (a.inserted_at && b.inserted_at) {
+ const aDate = new Date(a.inserted_at)
+ const bDate = new Date(b.inserted_at)
+ // sort by date
+ return aDate > bDate ? -1 : 1
+ }
+
+ return a.id! > b.id! ? -1 : 1
+ })
+
+ sqlEditorState.orders[projectRef] = orderedSnippets.map((s) => s.id!)
+ }
+
snippets.forEach((snippet) => {
sqlEditorState.addSnippet(snippet, projectRef)
})
@@ -41,6 +66,9 @@ export const sqlEditorState = proxy({
}
sqlEditorState.results[snippet.id] = []
sqlEditorState.savingStates[snippet.id] = 'IDLE'
+ if (!sqlEditorState.orders[projectRef].includes(snippet.id)) {
+ sqlEditorState.orders[projectRef].unshift(snippet.id)
+ }
}
sqlEditorState.loaded[projectRef] = true
},
@@ -50,6 +78,10 @@ export const sqlEditorState = proxy({
const { [id]: result, ...otherResults } = sqlEditorState.results
sqlEditorState.results = otherResults
+
+ sqlEditorState.orders[snippet.projectRef] = sqlEditorState.orders[snippet.projectRef].filter(
+ (s) => s !== id
+ )
},
setSplitSizes: (id: string, splitSizes: number[]) => {
if (sqlEditorState.snippets[id]) {
@@ -104,6 +136,16 @@ export const getSqlEditorStateSnapshot = () => snapshot(sqlEditorState)
export const useSqlEditorStateSnapshot = (options?: Parameters<typeof useSnapshot>[1]) =>
useSnapshot(sqlEditorState, options)
+export const useSnippets = (projectRef: string | undefined) => {
+ const snapshot = useSqlEditorStateSnapshot()
+
+ return useMemo(() => {
+ return projectRef
+ ? snapshot.orders[projectRef]?.map((id) => snapshot.snippets[id].snippet) ?? []
+ : []
+ }, [projectRef, snapshot.orders, snapshot.snippets])
+}
+
async function update(id: string, projectRef: string, content: Partial<Content>) {
try {
sqlEditorState.savingStates[id] = 'UPDATING'