import { useQuery } from '@tanstack/react-query' import { storageKeys } from './keys' import { get, handleError } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' type GetNamespaceTablesVariables = { warehouse?: string namespace?: string projectRef?: string } async function getNamespaceTables( { projectRef, warehouse, namespace }: GetNamespaceTablesVariables, signal?: AbortSignal ) { if (!projectRef) throw new Error('projectRef is required') if (!namespace) throw new Error('namespace is required') if (!warehouse) throw new Error('warehouse is required') const { data, error } = await get( '/platform/storage/{ref}/analytics-buckets/{id}/namespaces/{namespace}/tables', { params: { path: { ref: projectRef, id: warehouse, namespace } }, signal, } ) if (error) handleError(error) return data.data.map((x) => x.name) } type IcebergNamespaceTablesData = Awaited> export type IcebergNamespaceTablesError = StudioError export const useIcebergNamespaceTablesQuery = ( params: GetNamespaceTablesVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => { const { projectRef, warehouse, namespace } = params return useQuery({ queryKey: storageKeys.icebergNamespaceTables({ projectRef, warehouse, namespace, }), queryFn: () => getNamespaceTables({ ...params }), enabled: enabled && typeof projectRef !== 'undefined' && typeof warehouse !== 'undefined' && typeof namespace !== 'undefined', ...options, }) }