import { useQuery } from '@tanstack/react-query' import { invoicesKeys } from './keys' import { head } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' export type InvoicesCountVariables = { slug?: string } export async function getInvoicesCount({ slug }: InvoicesCountVariables, signal?: AbortSignal) { if (!slug) throw new Error('Slug is required') const res = await head('/platform/organizations/{slug}/billing/invoices', { params: { path: { slug } }, signal, parseAs: 'text', }) if (res.error) throw (res as any).error return Number(res.response.headers.get('X-Total-Count')) } export type InvoicesCountData = Awaited> export type InvoicesCountError = StudioError export const useInvoicesCountQuery = ( { slug }: InvoicesCountVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: invoicesKeys.count(slug), queryFn: ({ signal }) => getInvoicesCount({ slug }, signal), enabled: enabled && typeof slug !== 'undefined', ...options, })