'use client' import { invalidateTeamCache, isEmailConfigured, updateEmail } from '@admin/actions/profile' import { authClient } from '@admin/auth/client' import { useMutation } from '@tanstack/react-query' import { useRouter } from 'next/navigation' import { toast } from 'sonner' export function useUpdateProfileImage() { const router = useRouter() return useMutation({ mutationFn: async (image: string | null) => { const { error } = await authClient.updateUser({ image }) if (error) { throw new Error(error.message || 'Failed to update profile picture') } await invalidateTeamCache() }, onSuccess: () => { toast.success('Profile picture updated') router.refresh() }, onError: (error: Error) => { toast.error(error.message || 'Failed to update profile picture') } }) } export function useUpdateProfileName() { const router = useRouter() return useMutation({ mutationFn: async (name: string) => { const { error } = await authClient.updateUser({ name }) if (error) { throw new Error(error.message || 'Failed to update name') } await invalidateTeamCache() }, onSuccess: () => { toast.success('Name updated') router.refresh() }, onError: (error: Error) => { toast.error(error.message || 'Failed to update name') } }) } export function useUpdateProfileEmail() { const router = useRouter() return useMutation({ mutationFn: async ({ email, password }: { email: string; password: string }) => { const result = await updateEmail(email, password) if (!result.success) { throw new Error(result.error || 'Failed to update email') } await authClient.getSession({ query: { disableCookieCache: true } }) }, onSuccess: () => { toast.success('Email updated') router.refresh() }, onError: (error: Error) => { toast.error(error.message || 'Failed to update email') } }) } export function useSendPasswordResetLink(email: string) { return useMutation({ mutationFn: async () => { const emailReady = await isEmailConfigured() if (!emailReady) { throw new Error('No email provider is configured. Add one to enable password reset emails.') } const { error } = await authClient.requestPasswordReset({ email, redirectTo: '/admin/reset-password' }) if (error) { throw new Error(error.message || 'Failed to send reset link') } }, onSuccess: () => { toast.success('Check your email for a password reset link') }, onError: (error: Error) => { toast.error(error.message || 'Failed to send the reset link. Try again.') } }) } export function useRevokeSession() { const router = useRouter() return useMutation({ mutationFn: async (token: string) => { const { error } = await authClient.revokeSession({ token }) if (error) { throw new Error(error.message || 'Failed to revoke session') } }, onSuccess: () => { toast.success('Session revoked') router.refresh() }, onError: (error: Error) => { toast.error(error.message || 'Failed to revoke session') } }) } export function useRevokeOtherSessions() { const router = useRouter() return useMutation({ mutationFn: async () => { const { error } = await authClient.revokeOtherSessions() if (error) { throw new Error(error.message || 'Failed to sign out other devices') } }, onSuccess: () => { toast.success('Signed out of other devices') router.refresh() }, onError: (error: Error) => { toast.error(error.message || 'Failed to sign out other devices') } }) }