import { useQuery } from '@tanstack/react-query' import { configKeys } from './keys' import { components } from '@/data/api' import { get, handleError } from '@/data/fetchers' import { useDeploymentMode } from '@/hooks/misc/useDeploymentMode' import { IS_PLATFORM } from '@/lib/constants' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' export type ProjectStorageConfigVariables = { projectRef?: string } export type ProjectStorageConfigResponse = components['schemas']['StorageConfigResponse_Output'] export async function getProjectStorageConfig( { projectRef }: ProjectStorageConfigVariables, signal?: AbortSignal ) { if (!projectRef) throw new Error('projectRef is required') const { data, error } = await get('/platform/projects/{ref}/config/storage', { params: { path: { ref: projectRef } }, signal, }) if (error) { // [Joshen] This is due to API not returning an error message on this endpoint if a 404 is returned // Should only be a temporary patch, needs to be addressed on the API end if ((error as any).code === 404) { handleError({ ...(error as any), message: 'Storage configuration not found.' }) } else { handleError(error) } } return data } export type ProjectStorageConfigData = Awaited> export type ProjectStorageConfigError = StudioError export const useProjectStorageConfigQuery = ( { projectRef }: ProjectStorageConfigVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: configKeys.storage(projectRef), queryFn: ({ signal }) => getProjectStorageConfig({ projectRef }, signal), enabled: enabled && IS_PLATFORM && typeof projectRef !== 'undefined' && projectRef !== '_', ...options, }) export const useIsAnalyticsBucketsEnabled = ({ projectRef }: { projectRef?: string }) => { const { data } = useProjectStorageConfigQuery({ projectRef }) const isIcebergCatalogEnabled = !!data?.features.icebergCatalog?.enabled return isIcebergCatalogEnabled } export const useIsVectorBucketsEnabled = ({ projectRef }: { projectRef?: string }) => { const { data: isVectorBucketsEnabledData } = useProjectStorageConfigQuery( { projectRef }, { select: (data) => data?.features.vectorBuckets?.enabled } ) const { isCli, isPlatform } = useDeploymentMode() const isVectorBucketsEnabled = isCli || (isPlatform && !!isVectorBucketsEnabledData) return isVectorBucketsEnabled }