import { useQuery } 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, UseCustomQueryOptions } 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> export type BackupsError = ResponseError export const useBackupsQuery = ( { projectRef }: BackupsVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => { // [Joshen] Check for specifically false to account for project not loaded yet const isOrioleDbInAws = useIsOrioleDbInAws() return useQuery({ queryKey: databaseKeys.backups(projectRef), queryFn: ({ signal }) => getBackups({ projectRef }, signal), enabled: enabled && !isOrioleDbInAws && typeof projectRef !== 'undefined', ...options, }) }