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.
88 lines
2.4 KiB
TypeScript
88 lines
2.4 KiB
TypeScript
import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
|
|
|
|
import { Content } from './content-query'
|
|
import { remapSqlContentFields } from './content-remap'
|
|
import { contentKeys } from './keys'
|
|
import { SNIPPET_PAGE_LIMIT, withSavedStatus, type Snippet } from './sql-folders-query'
|
|
import { get } from '@/data/fetchers'
|
|
import type { SqlSnippets, UseCustomInfiniteQueryOptions } from '@/types'
|
|
|
|
export type SqlSnippet = Extract<Content, { type: 'sql' }>
|
|
|
|
interface GetSqlSnippetsVariables {
|
|
projectRef?: string
|
|
cursor?: string
|
|
visibility?: SqlSnippet['visibility']
|
|
favorite?: boolean
|
|
name?: string
|
|
sort?: 'name' | 'inserted_at'
|
|
}
|
|
|
|
export async function getSqlSnippets(
|
|
{ projectRef, cursor, visibility, favorite, name, sort }: GetSqlSnippetsVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (typeof projectRef === 'undefined') {
|
|
throw new Error('projectRef is required for getSqlSnippets')
|
|
}
|
|
|
|
const sortOrder = sort === 'name' ? 'asc' : 'desc'
|
|
|
|
const { data, error } = await get('/platform/projects/{ref}/content', {
|
|
params: {
|
|
path: { ref: projectRef },
|
|
query: {
|
|
type: 'sql',
|
|
cursor,
|
|
visibility,
|
|
favorite,
|
|
name,
|
|
limit: SNIPPET_PAGE_LIMIT.toString(),
|
|
sort_by: sort,
|
|
sort_order: sortOrder,
|
|
},
|
|
},
|
|
signal,
|
|
})
|
|
|
|
if (error) {
|
|
throw error
|
|
}
|
|
|
|
const snippets = remapSqlContentFields(
|
|
data.data as unknown as Array<Snippet & { content?: SqlSnippets.Content }>
|
|
)
|
|
return {
|
|
cursor: data.cursor,
|
|
contents: snippets.map(withSavedStatus),
|
|
}
|
|
}
|
|
|
|
export type SqlSnippetsData = Awaited<ReturnType<typeof getSqlSnippets>>
|
|
export type SqlSnippetsError = unknown
|
|
|
|
export const useSqlSnippetsQuery = <TData = SqlSnippetsData>(
|
|
{ projectRef, sort, name, visibility, favorite }: Omit<GetSqlSnippetsVariables, 'cursor'>,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomInfiniteQueryOptions<
|
|
SqlSnippetsData,
|
|
SqlSnippetsError,
|
|
InfiniteData<TData>,
|
|
readonly unknown[],
|
|
string | undefined
|
|
> = {}
|
|
) =>
|
|
useInfiniteQuery({
|
|
queryKey: contentKeys.sqlSnippets(projectRef, { sort, name, visibility, favorite }),
|
|
queryFn: ({ signal, pageParam: cursor }) =>
|
|
getSqlSnippets({ projectRef, cursor, sort, name, visibility, favorite }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
initialPageParam: undefined,
|
|
getNextPageParam(lastPage) {
|
|
return lastPage.cursor
|
|
},
|
|
...options,
|
|
})
|