import { useQuery } from '@tanstack/react-query' import type { components } from 'api-types' import { platformAppKeys } from './keys' import { get, handleError, unguarded } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' export type PlatformAppsVariables = { slug?: string } export type PlatformApp = components['schemas']['ListPlatformAppsResponse_Output']['apps'][number] export async function getPlatformApps({ slug }: PlatformAppsVariables, signal?: AbortSignal) { if (!slug) throw new Error('slug is required') const { data, error } = await get('/platform/organizations/{slug}/apps', { params: { path: { slug } }, signal, grant: unguarded.migrating, }) if (error) handleError(error) return data } export type PlatformAppsData = Awaited> export type PlatformAppsError = StudioError export const usePlatformAppsQuery = ( { slug }: PlatformAppsVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: platformAppKeys.list(slug), queryFn: ({ signal }) => getPlatformApps({ slug }, signal), enabled: enabled && typeof slug !== 'undefined', ...options, })