Files
supabase/apps/studio/data/content/sql-folders-query.ts
Joshen Lim 17e0c5dbcf Improve snippet organization in SQL Editor (#27881)
* Init changes for sql fodlers

* Added upsert logic in sql-editor-v2 valtio, hooked up with templates and quickstarts

* Do up logic for creating new snippets by typing in /new or by clicking new query button

* Do up logic for updating and deleting snippets

* Do up logic for favourites and shared snippets

* Do up logic for favourites and shared snippets

* Fix

* Fix saving indicator, add empty states for favorites and shared snippets

* Implement sorting

* Some minor QOL improvements

* Minor fix on empty state for private snippets

* Add delete folder mutation

* Implement create and update folder

* Fix reinstate with AI renaming for new snippets under folder

* Support controlled multi select behaviour in private snippets

* Undo changes to tree-view-multi-select

* Support bulk deletes

* Support moving queries + rendering queries in folders

* Support deleting folders and creating a new folder when moving a query

* Fix bug where renaming query removes content

* Add initial loading state in sql editor nav + handle fallback if cannot retrieve content by id

* Fix some spelling

* Fix TS issue in sql folders mutation keys

* Fix toggling favorite

* Lint

* Revert fallback behaviour in ]id] for now

* Fix favorites and shared snippets not showing

* Fix moving currently opened snippet leads to loading

* Support bulk moving

* Improve multi select logic a little

* Nit lint

* Reinstate AI retitling for untitled snippets when running query

* Remove hardcode in useAFlag

* Support creating new snippet in a folder directly

* Fix sharing snippets that are within a folder

* Fix sharing snippets within a folder

* Fix favorite

* Add loading state when fetching folder contents

* Fix favoriting snippets in folders
2024-07-17 12:23:19 +08:00

53 lines
1.9 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { contentKeys } from './keys'
import { components } from 'api-types'
export type SnippetFolderResponse = components['schemas']['GetUserContentFolderResponse']['data']
export type SnippetFolder = components['schemas']['UserContentFolder']
export type Snippet = components['schemas']['UserContentObjectMeta']
export type SnippetDetail = components['schemas']['UserContentObjectV2']
export async function getSQLSnippetFolders(
{ projectRef, folderId }: { projectRef?: string; folderId?: string },
signal?: AbortSignal
) {
if (typeof projectRef === 'undefined') throw new Error('projectRef is required')
if (folderId) {
const { data, error } = await get('/platform/projects/{ref}/content/folders/{id}', {
params: { path: { ref: projectRef, id: folderId } },
signal,
})
if (error) throw handleError(error)
return data.data
} else {
const { data, error } = await get('/platform/projects/{ref}/content/folders', {
params: { path: { ref: projectRef }, query: { type: 'sql' } },
signal,
})
if (error) throw handleError(error)
return data.data
}
}
export type SQLSnippetFoldersData = Awaited<ReturnType<typeof getSQLSnippetFolders>>
export type SQLSnippetFoldersError = ResponseError
export const useSQLSnippetFoldersQuery = <TData = SQLSnippetFoldersData>(
{ projectRef, folderId }: { projectRef?: string; folderId?: string },
{
enabled = true,
...options
}: UseQueryOptions<SQLSnippetFoldersData, SQLSnippetFoldersError, TData> = {}
) =>
useQuery<SQLSnippetFoldersData, SQLSnippetFoldersError, TData>(
contentKeys.folders(projectRef, folderId),
({ signal }) => getSQLSnippetFolders({ projectRef, folderId }, signal),
{ enabled: enabled && typeof projectRef !== 'undefined', ...options }
)