mirror of
https://github.com/supabase/supabase.git
synced 2026-06-09 03:22:27 +08:00
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { customDomainKeys } from './keys'
|
|
import { handleError, post } from '@/data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type CustomDomainActivateVariables = {
|
|
projectRef: string
|
|
}
|
|
|
|
export async function activateCustomDomain({ projectRef }: CustomDomainActivateVariables) {
|
|
const { data, error } = await post(`/v1/projects/{ref}/custom-hostname/activate`, {
|
|
params: { path: { ref: projectRef } },
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type CustomDomainActivateData = Awaited<ReturnType<typeof activateCustomDomain>>
|
|
|
|
export const useCustomDomainActivateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<CustomDomainActivateData, ResponseError, CustomDomainActivateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<CustomDomainActivateData, ResponseError, CustomDomainActivateVariables>({
|
|
mutationFn: (vars) => activateCustomDomain(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: customDomainKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to activate custom domain: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|