import { useQuery } from '@tanstack/react-query' import { components } from 'api-types' import { clientSecretKeys } from './keys' import { get, handleError } from '@/data/fetchers' import type { ResponseError, UseCustomQueryOptions } from '@/types' export type Secret = components['schemas']['ListOAuthAppClientSecretsResponse_Output']['client_secrets'][0] & { client_secret?: components['schemas']['CreateOAuthAppClientSecretResponse_Output']['client_secret'] } export type CreatedSecret = components['schemas']['CreateOAuthAppClientSecretResponse_Output'] export interface SecretRowProps { secret: Secret appId?: string isNew?: boolean } type ClientSecretsVariables = { slug?: string appId?: string } export async function getClientSecrets( { slug, appId }: ClientSecretsVariables, signal?: AbortSignal ) { if (!slug) throw new Error('slug is required') if (!appId) throw new Error('appId is required') const { data, error } = await get( '/platform/organizations/{slug}/oauth/apps/{app_id}/client-secrets', { params: { path: { slug, app_id: appId }, }, signal, } ) if (error) handleError(error) return data } export type ClientSecretsData = Awaited> export type ClientSecretsError = ResponseError export const useClientSecretsQuery = ( { slug, appId }: ClientSecretsVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: clientSecretKeys.list(slug, appId), queryFn: ({ signal }) => getClientSecrets({ slug, appId }, signal), enabled: enabled && typeof slug !== 'undefined' && typeof appId !== 'undefined', ...options, })