mirror of
https://github.com/supabase/supabase.git
synced 2026-10-15 14:19:11 +03:00
70 lines
2.3 KiB
TypeScript
70 lines
2.3 KiB
TypeScript
import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
|
|
import { components } from 'api-types'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { useProfile } from 'lib/profile'
|
|
import type { Organization, ResponseError } from 'types'
|
|
import { organizationKeys } from './keys'
|
|
import { MANAGED_BY, ManagedBy } from 'lib/constants/infrastructure'
|
|
|
|
export type OrganizationBase = components['schemas']['OrganizationResponse']
|
|
|
|
export function castOrganizationResponseToOrganization(org: OrganizationBase): Organization {
|
|
return {
|
|
...org,
|
|
billing_email: org.billing_email ?? 'Unknown',
|
|
managed_by: getManagedBy(org),
|
|
partner_id: org.slug.startsWith('vercel_') ? org.slug.replace('vercel_', '') : undefined,
|
|
}
|
|
}
|
|
|
|
function getManagedBy(org: OrganizationBase): ManagedBy {
|
|
switch (org.billing_partner) {
|
|
case 'vercel_marketplace':
|
|
return MANAGED_BY.VERCEL_MARKETPLACE
|
|
// TODO(ignacio): Uncomment this when we've deployed the AWS Marketplace new slug
|
|
// case 'aws_marketplace':
|
|
// return MANAGED_BY.AWS_MARKETPLACE
|
|
case 'aws':
|
|
return MANAGED_BY.AWS_MARKETPLACE
|
|
default:
|
|
return MANAGED_BY.SUPABASE
|
|
}
|
|
}
|
|
|
|
export async function getOrganizations({
|
|
signal,
|
|
headers,
|
|
}: {
|
|
signal?: AbortSignal
|
|
headers?: Record<string, string>
|
|
}): Promise<Organization[]> {
|
|
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<ReturnType<typeof getOrganizations>>
|
|
export type OrganizationsError = ResponseError
|
|
|
|
export const useOrganizationsQuery = <TData = OrganizationsData>({
|
|
enabled = true,
|
|
...options
|
|
}: UseQueryOptions<OrganizationsData, OrganizationsError, TData> = {}) => {
|
|
const { profile } = useProfile()
|
|
return useQuery<OrganizationsData, OrganizationsError, TData>(
|
|
organizationKeys.list(),
|
|
({ signal }) => getOrganizations({ signal }),
|
|
{ enabled: enabled && profile !== undefined, ...options, staleTime: 30 * 60 * 1000 }
|
|
)
|
|
}
|
|
|
|
export function invalidateOrganizationsQuery(client: QueryClient) {
|
|
return client.invalidateQueries(organizationKeys.list())
|
|
}
|