import { useMutation, useQueryClient } from '@tanstack/react-query' import { toast } from 'sonner' import { storageKeys } from './keys' import { del, handleError } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomMutationOptions } from '@/types' type DeleteIcebergNamespaceTableVariables = { warehouse: string namespace: string table: string projectRef?: string } async function deleteIcebergNamespaceTable({ projectRef, warehouse, namespace, table, }: DeleteIcebergNamespaceTableVariables) { if (!projectRef) throw new Error('projectRef is required') const { error } = await del( '/platform/storage/{ref}/analytics-buckets/{id}/namespaces/{namespace}/tables/{table}', { params: { path: { ref: projectRef, id: warehouse, namespace, table }, query: { purge: 'true' }, }, } ) if (error) handleError(error) return true } type IcebergNamespaceTableDeleteData = Awaited> export const useIcebergNamespaceTableDeleteMutation = ({ onSuccess, onError, ...options }: Omit< UseCustomMutationOptions< IcebergNamespaceTableDeleteData, StudioError, DeleteIcebergNamespaceTableVariables >, 'mutationFn' > = {}) => { const queryClient = useQueryClient() return useMutation< IcebergNamespaceTableDeleteData, StudioError, DeleteIcebergNamespaceTableVariables >({ mutationFn: (vars) => deleteIcebergNamespaceTable({ ...vars }), async onSuccess(data, variables, context) { await queryClient.invalidateQueries({ queryKey: storageKeys.icebergNamespace({ projectRef: variables.projectRef, warehouse: variables.warehouse, namespace: variables.namespace, }), }) await onSuccess?.(data, variables, context) }, async onError(data, variables, context) { if (onError === undefined) { toast.error(`Failed to delete Iceberg namespace table: ${data.message}`) } else { onError(data, variables, context) } }, ...options, }) }