mirror of
https://github.com/supabase/supabase.git
synced 2026-10-09 11:25:06 +03:00
PR 3 of a stacked refactor. Replaces the two overlapping pieces of snippet
lifecycle state — the savingStates map ('IDLE'|'UPDATING'|'UPDATING_FAILED')
and the isNotSavedInDatabaseYet boolean — with a single SnippetStatus enum.
Save progress and persistence are orthogonal, so the never-persisted 'new'
phase keeps its own saving/failed variants (new, new_saving, new_save_failed)
alongside the persisted ones (saved, unsaved, saving, save_failed). The
wasNeverPersisted() predicate recovers the persistence axis and stays true
across the entire first-save lifecycle, so content-fetching, list
invalidation, and the replication-lag 404 swallow all map faithfully — and a
new snippet's first save keeps its saving/failed indicator.
Status is attached at the data layer so a snippet is never without one:
- SnippetStatus + SnippetWithContent now live in data/content; the snippet
queries attach status 'saved' via withSavedStatus(), and upsertContent
returns SnippetWithContent so move/rename responses carry status too.
- A SQL-typed getSqlSnippetById/useSqlSnippetByIdQuery returns
SnippetWithContent (the generic useContentIdQuery stays for Reports), so
[id].tsx loads content with no casting.
- 'new' is attached on local creation (createSqlSnippetSkeletonV2).
Dependents import SnippetWithContent/SnippetStatus directly from the data
layer (no state-layer re-exports). 'unsaved' is reserved for manual-save
dirty tracking in a later PR. Pure unit tests for every predicate/transition.
74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
|
|
|
|
import { contentKeys } from './keys'
|
|
import { SNIPPET_PAGE_LIMIT, withSavedStatus } from './sql-folders-query'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import type { ResponseError, UseCustomInfiniteQueryOptions } from '@/types'
|
|
|
|
export type SQLSnippetFolderContentsVariables = {
|
|
projectRef?: string
|
|
folderId?: string
|
|
cursor?: string
|
|
name?: string
|
|
sort?: 'name' | 'inserted_at'
|
|
}
|
|
|
|
export async function getSQLSnippetFolderContents(
|
|
{ projectRef, folderId, cursor, sort, name }: SQLSnippetFolderContentsVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (typeof projectRef === 'undefined') throw new Error('projectRef is required')
|
|
if (typeof folderId === 'undefined') throw new Error('folderId is required')
|
|
|
|
const sortOrder = sort === 'name' ? 'asc' : 'desc'
|
|
|
|
const { data, error } = await get('/platform/projects/{ref}/content/folders/{id}', {
|
|
params: {
|
|
path: { ref: projectRef, id: folderId },
|
|
query: {
|
|
cursor,
|
|
limit: SNIPPET_PAGE_LIMIT.toString(),
|
|
sort_by: sort,
|
|
sort_order: sortOrder,
|
|
name,
|
|
},
|
|
},
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return {
|
|
...data.data,
|
|
contents: (data.data.contents ?? []).map(withSavedStatus),
|
|
cursor: data.cursor,
|
|
}
|
|
}
|
|
|
|
export type SQLSnippetFolderContentsData = Awaited<ReturnType<typeof getSQLSnippetFolderContents>>
|
|
export type SQLSnippetFolderContentsError = ResponseError
|
|
|
|
export const useSQLSnippetFolderContentsQuery = <TData = SQLSnippetFolderContentsData>(
|
|
{ projectRef, folderId, name, sort }: Omit<SQLSnippetFolderContentsVariables, 'cursor'>,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomInfiniteQueryOptions<
|
|
SQLSnippetFolderContentsData,
|
|
SQLSnippetFolderContentsError,
|
|
InfiniteData<TData>,
|
|
readonly unknown[],
|
|
string | undefined
|
|
> = {}
|
|
) =>
|
|
useInfiniteQuery({
|
|
queryKey: contentKeys.folderContents(projectRef, folderId, { name, sort }),
|
|
queryFn: ({ signal, pageParam }) =>
|
|
getSQLSnippetFolderContents({ projectRef, folderId, cursor: pageParam, name, sort }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined' && typeof folderId !== 'undefined',
|
|
initialPageParam: undefined,
|
|
getNextPageParam(lastPage) {
|
|
return lastPage.cursor
|
|
},
|
|
...options,
|
|
})
|