import { useMutation, useQueryClient } from '@tanstack/react-query' import { toast } from 'sonner' import { BannedIPKeys } from './keys' import { del, handleError, unguarded } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomMutationOptions } from '@/types' export type IPDeleteVariables = { projectRef: string /** can only be one for now */ ips: string[] } export async function deleteBannedIPs({ projectRef, ips }: IPDeleteVariables) { const { data, error } = await del(`/v1/projects/{ref}/network-bans`, { params: { path: { ref: projectRef }, }, body: { ipv4_addresses: ips }, grant: unguarded.migrating, }) if (error) handleError(error) return data } type IPDeleteData = Awaited> export const useBannedIPsDeleteMutation = ({ onSuccess, onError, ...options }: Omit< UseCustomMutationOptions, 'mutationFn' > = {}) => { const queryClient = useQueryClient() return useMutation({ mutationFn: (vars) => deleteBannedIPs(vars), async onSuccess(data, variables, context) { const { projectRef } = variables await queryClient.invalidateQueries({ queryKey: BannedIPKeys.list(projectRef) }) await onSuccess?.(data, variables, context) }, async onError(data, variables, context) { if (onError === undefined) { toast.error(`Failed to unban ips: ${data.message}`) } else { onError(data, variables, context) } }, ...options, }) }