Files
supabase/apps/studio/data/content/content-count-query.ts
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

54 lines
1.6 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { operations } from 'api-types'
import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { contentKeys } from './keys'
type GetContentCountVariables =
operations['ContentController_getContentCountV2']['parameters']['query'] & {
projectRef?: string
cumulative?: boolean
}
export async function getContentCount(
{ projectRef, cumulative, type, name }: GetContentCountVariables,
signal?: AbortSignal
) {
if (typeof projectRef === 'undefined') throw new Error('projectRef is required')
const { data, error } = await get('/platform/projects/{ref}/content/count', {
params: {
path: { ref: projectRef },
query: {
...(type && { type }),
...(name && { name }),
},
},
...(cumulative ? {} : { headers: { Version: '2' } }),
signal,
})
if (error) throw handleError(error)
return data
}
export type ContentIdData = Awaited<ReturnType<typeof getContentCount>>
export type ContentIdError = ResponseError
export const useContentCountQuery = <TData = ContentIdData>(
{ projectRef, cumulative, type, name }: GetContentCountVariables,
{ enabled = true, ...options }: UseQueryOptions<ContentIdData, ContentIdError, TData> = {}
) =>
useQuery<ContentIdData, ContentIdError, TData>(
contentKeys.count(projectRef, type, {
cumulative,
name,
}),
({ signal }) => getContentCount({ projectRef, cumulative, type, name }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)