Files
supabase/apps/studio/data/content/content-count-query.ts
T
Joshen Lim a3ecf36c65 Show correct number of private snippets for SQL folders (#28411)
* Show correct number of private snippets for SQL folders

* Ensure that private snippet count is updated when sharing / unsharing queries

* Fix snippets caching issuie

* Fix move query modal showing more than just the project's folders
2024-08-07 17:38:17 +08:00

42 lines
1.3 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { contentKeys } from './keys'
interface getContentCountVariables {
projectRef?: string
type: 'sql' | 'report' | 'log_sql'
}
export async function getContentCount(
{ projectRef, type }: 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 } },
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, type }: getContentCountVariables,
{ enabled = true, ...options }: UseQueryOptions<ContentIdData, ContentIdError, TData> = {}
) =>
useQuery<ContentIdData, ContentIdError, TData>(
contentKeys.count(projectRef, type),
({ signal }) => getContentCount({ projectRef, type }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)