mirror of
https://github.com/supabase/supabase.git
synced 2026-06-08 02:25:04 +08:00
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { BannedIPKeys } from './keys'
|
|
import { del, handleError } from '@/data/fetchers'
|
|
import type { ResponseError, 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 },
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type IPDeleteData = Awaited<ReturnType<typeof deleteBannedIPs>>
|
|
|
|
export const useBannedIPsDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<IPDeleteData, ResponseError, IPDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
return useMutation<IPDeleteData, ResponseError, IPDeleteVariables>({
|
|
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,
|
|
})
|
|
}
|