import { QueryClient, useQuery } from '@tanstack/react-query' import { platformComponents as components } from 'api-types' import { organizationKeys } from './keys' import { getManagedByFromOrganizationPartner } from './managed-by-utils' import { get, handleError } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import { useProfile } from '@/lib/profile' import type { Organization, UseCustomQueryOptions } from '@/types' export type OrganizationBase = components['schemas']['OrganizationResponse_Output'] export function castOrganizationResponseToOrganization(org: OrganizationBase): Organization { return { ...org, billing_email: org.billing_email ?? 'Unknown', managed_by: getManagedByFromOrganizationPartner(org.billing_partner, org.integration_source), partner_id: org.slug.startsWith('vercel_') ? org.slug.replace('vercel_', '') : undefined, } } export async function getOrganizations({ signal, headers, }: { signal?: AbortSignal headers?: Record }): Promise { const { data, error } = await get('/platform/organizations', { signal, headers }) if (error) handleError(error) if (!Array.isArray(data)) return [] return data .map(castOrganizationResponseToOrganization) .sort((a, b) => a.name.localeCompare(b.name)) } export type OrganizationsData = Awaited> export type OrganizationsError = StudioError export const useOrganizationsQuery = ({ enabled = true, ...options }: UseCustomQueryOptions = {}) => { const { profile } = useProfile() return useQuery({ queryKey: organizationKeys.list(), queryFn: ({ signal }) => getOrganizations({ signal }), enabled: enabled && profile !== undefined, ...options, staleTime: 30 * 60 * 1000, }) } export function invalidateOrganizationsQuery(client: QueryClient) { return client.invalidateQueries({ queryKey: organizationKeys.list() }) }