Files
supabase/apps/studio/data/analytics/project-daily-stats-query.ts
Ivan Vasilov 8b657165b5 chore: Migrate to use custom type for ReactQuery queries and mutations (#40073)
* Add custom types for queries, mutations and infinite queries.

* Migrate all queries to use the new type.

* Migrate all infinite queries to useCustomInfiniteQueryOptions.

* Migrate all mutations to use useCustomMutationOptions.

* Add type to all imports in `types` folder.
2025-11-03 13:18:13 +01:00

71 lines
2.1 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { operations } from 'api-types'
import { get, handleError } from 'data/fetchers'
import type { AnalyticsData } from './constants'
import { analyticsKeys } from './keys'
import { UseCustomQueryOptions } from 'types'
export type ProjectDailyStatsAttribute =
operations['DailyStatsController_getDailyStats']['parameters']['query']['attribute']
export type ProjectDailyStatsVariables = {
projectRef?: string
attribute: ProjectDailyStatsAttribute
startDate?: string
endDate?: string
}
export async function getProjectDailyStats(
{ projectRef, attribute, startDate, endDate }: ProjectDailyStatsVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('Project ref is required')
if (!attribute) throw new Error('Attribute is required')
if (!startDate) throw new Error('Start date is required')
if (!endDate) throw new Error('End date is required')
const { data, error } = await get('/platform/projects/{ref}/daily-stats', {
params: {
path: { ref: projectRef },
query: {
attribute,
startDate,
endDate,
},
},
signal,
})
if (error) handleError(error)
return data as unknown as AnalyticsData
}
export type ProjectDailyStatsData = Awaited<ReturnType<typeof getProjectDailyStats>>
export type ProjectDailyStatsError = unknown
export const useProjectDailyStatsQuery = <TData = ProjectDailyStatsData>(
{ projectRef, attribute, startDate, endDate }: ProjectDailyStatsVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<ProjectDailyStatsData, ProjectDailyStatsError, TData> = {}
) =>
useQuery<ProjectDailyStatsData, ProjectDailyStatsError, TData>({
queryKey: analyticsKeys.infraMonitoring(projectRef, {
attribute,
startDate,
endDate,
}),
queryFn: ({ signal }) =>
getProjectDailyStats({ projectRef, attribute, startDate, endDate }, signal),
enabled:
enabled &&
typeof projectRef !== 'undefined' &&
typeof attribute !== 'undefined' &&
typeof startDate !== 'undefined' &&
typeof endDate !== 'undefined',
staleTime: 1000 * 60 * 30,
...options,
})