import { useQuery } from '@tanstack/react-query' import { storageKeys } from './keys' import { get, handleError } from '@/data/fetchers' import type { ResponseError, UseCustomQueryOptions } from '@/types' type StorageArchiveVariables = { projectRef?: string } async function fetchStorageArchive({ projectRef }: StorageArchiveVariables, signal?: AbortSignal) { if (!projectRef) throw new Error('projectRef is required') const { data, error } = await get('/platform/storage/{ref}/archive', { params: { path: { ref: projectRef } }, signal, }) if (error) { if ((error as ResponseError)?.message?.includes('Storage archive not found')) { return { fileUrl: undefined } } else { handleError(error) } } return data } export type StorageArchiveData = Awaited> export const useStorageArchiveQuery = ( { projectRef }: StorageArchiveVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: storageKeys.archive(projectRef), queryFn: ({ signal }) => fetchStorageArchive({ projectRef }, signal), enabled: enabled && typeof projectRef !== 'undefined', ...options, })