import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query' import { toast } from 'sonner' import { awsAccountKeys } from './keys' import { del, handleError } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' export type AWSAccountDeleteVariables = { projectRef: string awsAccountId: string databaseIdentifier?: string } export async function deleteAWSAccount({ projectRef, awsAccountId, databaseIdentifier, }: AWSAccountDeleteVariables) { const deleteRequest = del as unknown as ( path: string, init: { params: { path: Record } } ) => Promise<{ data: unknown; error: unknown }> const path = databaseIdentifier ? '/platform/projects/{ref}/privatelink/associations/aws-account/{aws_account_id}/database/{database_identifier}' : '/platform/projects/{ref}/privatelink/associations/aws-account/{aws_account_id}' const { data, error } = await deleteRequest(path, { params: { path: { ref: projectRef, aws_account_id: awsAccountId, ...(databaseIdentifier ? { database_identifier: databaseIdentifier } : {}), }, }, }) if (error) handleError(error) return data } type AWSAccountDeleteData = Awaited> export const useAWSAccountDeleteMutation = ({ onSuccess, onError, ...options }: Omit< UseMutationOptions, 'mutationFn' > = {}) => { const queryClient = useQueryClient() return useMutation({ mutationFn: (vars) => deleteAWSAccount(vars), async onSuccess(data, variables, context, mutationContext) { const { projectRef } = variables await queryClient.invalidateQueries({ queryKey: awsAccountKeys.list(projectRef) }) await onSuccess?.(data, variables, context, mutationContext) }, async onError(data, variables, context, mutationContext) { if (onError === undefined) { toast.error(`Failed to delete connection: ${data.message}`) } else { onError(data, variables, context, mutationContext) } }, ...options, }) }