Files
supabase/apps/studio/data/content/content-infinite-query.ts
T
Joshen Lim 4425e05f04 Update SQL editor search to return a flat list (#33064)
* Update SQL editor search to return a flat list

* Open folder when clicking on a snippet from searching and snippet belongs to a folder

* Update snipet page limit

* Set keep previous data true for count

* Add hover card for SQL search list

* Fix issues from feedback

* Make count loader less jarring
2025-01-27 15:40:51 +08:00

64 lines
1.7 KiB
TypeScript

import { useInfiniteQuery, UseInfiniteQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import { Content, ContentType } from './content-query'
import { contentKeys } from './keys'
interface GetContentVariables {
projectRef?: string
cursor?: string
type: ContentType
name?: string
limit?: number
sort?: 'name' | 'inserted_at'
}
export async function getContent(
{ projectRef, type, name, limit = 10, sort, cursor }: GetContentVariables,
signal?: AbortSignal
) {
if (typeof projectRef === 'undefined') {
throw new Error('projectRef is required for getContent')
}
const { data, error } = await get('/platform/projects/{ref}/content', {
params: {
path: { ref: projectRef },
query: {
type,
name,
sort_by: sort,
limit: limit.toString(),
cursor,
},
},
signal,
})
if (error) handleError(error)
return {
cursor: data.cursor,
content: data.data as unknown as Content[],
}
}
export type ContentData = Awaited<ReturnType<typeof getContent>>
export type ContentError = unknown
export const useContentInfiniteQuery = <TData = ContentData>(
{ projectRef, type, name, limit, sort }: GetContentVariables,
{ enabled = true, ...options }: UseInfiniteQueryOptions<ContentData, ContentError, TData> = {}
) => {
return useInfiniteQuery<ContentData, ContentError, TData>(
contentKeys.infiniteList(projectRef, { type, name, limit, sort }),
({ signal, pageParam }) =>
getContent({ projectRef, type, name, limit, sort, cursor: pageParam }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
getNextPageParam: (lastPage) => lastPage.cursor,
...options,
}
)
}