import { useMutation } from '@tanstack/react-query' import { toast } from 'sonner' import type { User } from './users-infinite-query' import { handleError, post } from '@/data/fetchers' import type { ResponseError, UseCustomMutationOptions } from '@/types' export type UserSendMagicLinkVariables = { projectRef: string user: User } export async function sendMagicLink({ projectRef, user }: UserSendMagicLinkVariables) { const { data, error } = await post('/platform/auth/{ref}/magiclink', { params: { path: { ref: projectRef } }, body: { email: user.email }, }) if (error) handleError(error) return data } type UserSendMagicLinkData = Awaited> export const useUserSendMagicLinkMutation = ({ onSuccess, onError, ...options }: Omit< UseCustomMutationOptions, 'mutationFn' > = {}) => { return useMutation({ mutationFn: (vars) => sendMagicLink(vars), async onSuccess(data, variables, context) { // [Joshen] If we need to invalidate any queries await onSuccess?.(data, variables, context) }, async onError(data, variables, context) { if (onError === undefined) { toast.error(`Failed to send magic link: ${data.message}`) } else { onError(data, variables, context) } }, ...options, }) }