Files
supabase/apps/studio/data/database/backups-query.ts
T
Alaister YoungandJoshen Lim 8855d05803 chore(studio): swap react-query to object syntax (#39842)
* chore(studio): swap react-query to object syntax

* Fix small issues found

* Fix realtime settings

* Nit

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-10-27 09:38:27 +01:00

44 lines
1.5 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import type { components } from 'data/api'
import { get, handleError } from 'data/fetchers'
import { useIsOrioleDbInAws } from 'hooks/misc/useSelectedProject'
import type { ResponseError } from 'types'
import { databaseKeys } from './keys'
export type BackupsVariables = {
projectRef?: string
}
export type DatabaseBackup = components['schemas']['BackupsResponse']['backups'][number]
export async function getBackups({ projectRef }: BackupsVariables, signal?: AbortSignal) {
if (!projectRef) throw new Error('Project ref is required')
const { data, error } = await get(`/platform/database/{ref}/backups`, {
params: { path: { ref: projectRef } },
signal,
})
if (error) handleError(error)
return data
}
export type BackupsData = Awaited<ReturnType<typeof getBackups>>
export type BackupsError = ResponseError
export const useBackupsQuery = <TData = BackupsData>(
{ projectRef }: BackupsVariables,
{ enabled = true, ...options }: UseQueryOptions<BackupsData, BackupsError, TData> = {}
) => {
// [Joshen] Check for specifically false to account for project not loaded yet
const isOrioleDbInAws = useIsOrioleDbInAws()
return useQuery<BackupsData, BackupsError, TData>({
queryKey: databaseKeys.backups(projectRef),
queryFn: ({ signal }) => getBackups({ projectRef }, signal),
enabled: enabled && !isOrioleDbInAws && typeof projectRef !== 'undefined',
...options,
})
}