Files
supabase/apps/studio/data/documents/document-query.ts
Ivan Vasilov 436bdb10ae chore: Move the studio app to apps/studio (#18915)
* Move all studio files from /studio to /apps/studio.

* Move studio specific prettier ignores.

* Fix the ui references from studio.

* Fix the css imports.

* Fix all package.json issues.

* Fix the prettier setup for the studio app.

* Add .turbo folder to prettierignore.

* Fix the github workflows.
2023-11-15 12:38:55 +01:00

58 lines
1.7 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get } from 'data/fetchers'
import { ResponseError } from 'types'
import { documentKeys } from './keys'
export type DocType = 'standard-security-questionnaire' | 'soc2-type-2-report'
export type DocumentVariables = {
orgSlug?: string
docType?: DocType
}
export async function getDocument({ orgSlug, docType }: DocumentVariables, signal?: AbortSignal) {
if (!orgSlug) throw new Error('orgSlug is required')
if (docType === 'standard-security-questionnaire') {
const { data, error } = await get(
`/platform/organizations/{slug}/documents/standard-security-questionnaire`,
{
params: { path: { slug: orgSlug } },
signal,
}
)
if (error) throw error
return data as { fileUrl: string }
}
if (docType === 'soc2-type-2-report') {
const { data, error } = await get(
`/platform/organizations/{slug}/documents/soc2-type-2-report`,
{
params: { path: { slug: orgSlug } },
signal,
}
)
if (error) throw error
return data as { fileUrl: string }
}
}
export type DocumentData = Awaited<ReturnType<typeof getDocument>>
export type DocumentError = ResponseError
export const useDocumentQuery = <TData = DocumentData>(
{ orgSlug, docType }: DocumentVariables,
{ enabled = true, ...options }: UseQueryOptions<DocumentData, DocumentError, TData> = {}
) =>
useQuery<DocumentData, DocumentError, TData>(
documentKeys.resource(orgSlug, docType),
({ signal }) => getDocument({ orgSlug, docType }, signal),
{
enabled: enabled && typeof orgSlug !== 'undefined' && typeof docType !== 'undefined',
...options,
}
)