import { useQuery, useQueryClient } from '@tanstack/react-query' import { components } from 'api-types' import { organizationKeys } from './keys' import { get, handleError } from '@/data/fetchers' import { UseCustomQueryOptions } from '@/types' export type MemberWithFreeProjectLimit = components['schemas']['MemberWithFreeProjectLimit_Output'] export type FreeProjectLimitCheckVariables = { slug?: string } export async function getFreeProjectLimitCheck( { slug }: FreeProjectLimitCheckVariables, signal?: AbortSignal ) { if (!slug) throw new Error('slug is required') const { data, error } = await get( '/platform/organizations/{slug}/members/reached-free-project-limit', { params: { path: { slug } }, signal, } ) if (error) handleError(error) return data } export type FreeProjectLimitCheckData = Awaited> export type FreeProjectLimitCheckError = unknown export const useFreeProjectLimitCheckQuery = ( { slug }: FreeProjectLimitCheckVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: organizationKeys.freeProjectLimitCheck(slug), queryFn: ({ signal }) => getFreeProjectLimitCheck({ slug }, signal), enabled: enabled && typeof slug !== 'undefined', ...options, }) /** * Free project limits are shared across all of a user's orgs, so this invalidates the cached * check for every org, not just one. */ export const useInvalidateFreeProjectLimitChecks = () => { const queryClient = useQueryClient() const invalidateFreeProjectLimitChecks = () => { return queryClient.invalidateQueries({ predicate: ({ queryKey }) => queryKey[0] === 'organizations' && queryKey[2] === 'free-project-limit-check', }) } return { invalidateFreeProjectLimitChecks } }