import { useQuery } from '@tanstack/react-query' import { components } from 'api-types' import { organizationKeys } from './keys' import { get, handleError } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' export type OrganizationInviteTokenVariables = { slug?: string; token?: string } export type OrganizationInviteByToken = components['schemas']['InvitationByTokenResponse_Output'] export async function getOrganizationInviteByToken( { slug, token }: OrganizationInviteTokenVariables, signal?: AbortSignal ) { if (!slug) throw new Error('slug is required') if (!token) throw new Error('token is required') const { data, error } = await get('/platform/organizations/{slug}/members/invitations/{token}', { params: { path: { slug, token } }, signal, }) if (error) handleError(error) return data } export type OrganizationInviteTokenData = Awaited> export type OrganizationInviteTokenError = StudioError export const useOrganizationInvitationTokenQuery = ( { slug, token }: OrganizationInviteTokenVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => { return useQuery({ queryKey: organizationKeys.token(slug, token), queryFn: ({ signal }) => getOrganizationInviteByToken({ slug, token }, signal), enabled: enabled && typeof slug !== 'undefined' && typeof token !== 'undefined', ...options, }) }