import { zodResolver } from "@hookform/resolvers/zod" import { useMutation, useQueryClient } from "@tanstack/react-query" import { Pencil } from "lucide-react" import { useState } from "react" import { useForm } from "react-hook-form" import { z } from "zod" import { UsersService, type UserUpdateMe } from "@/client" import { UserAvatar } from "@/components/Common/UserAvatar" import ChangePassword from "@/components/UserSettings/ChangePassword" import DeleteConfirmation from "@/components/UserSettings/DeleteConfirmation" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { LoadingButton } from "@/components/ui/loading-button" import useAuth from "@/hooks/useAuth" import useCustomToast from "@/hooks/useCustomToast" import { cn } from "@/lib/utils" import { handleError } from "@/utils" const formSchema = z.object({ full_name: z.string().max(30).optional(), email: z.email({ message: "Invalid email address" }), }) type FormData = z.infer /** Compact value row; the edit affordance appears on hover, focus or touch. */ const ReadOnlyField = ({ label, value, onEdit, }: { label: string value: string | undefined onEdit: () => void }) => (
{label}

{value || "N/A"}

) const UserInformation = () => { const queryClient = useQueryClient() const { showSuccessToast, showErrorToast } = useCustomToast() const [editMode, setEditMode] = useState(false) const { user: currentUser } = useAuth() const form = useForm({ resolver: zodResolver(formSchema), mode: "onBlur", criteriaMode: "all", defaultValues: { full_name: currentUser?.full_name ?? undefined, email: currentUser?.email, }, }) const toggleEditMode = () => { setEditMode(!editMode) } const mutation = useMutation({ mutationFn: (data: UserUpdateMe) => UsersService.updateUserMe({ requestBody: data }), onSuccess: () => { showSuccessToast("User updated successfully") toggleEditMode() }, onError: handleError.bind(showErrorToast), onSettled: () => { queryClient.invalidateQueries() }, }) const onSubmit = (data: FormData) => { const updateData: UserUpdateMe = {} // only include fields that have changed if (data.full_name !== currentUser?.full_name) { updateData.full_name = data.full_name } if (data.email !== currentUser?.email) { updateData.email = data.email } mutation.mutate(updateData) } const onCancel = () => { form.reset() toggleEditMode() } return ( Account Your profile details and sign-in {currentUser?.is_superuser && Admin}
editMode ? ( Full name ) : ( ) } /> editMode ? ( Email ) : ( ) } /> {editMode && (
Save
)}
{/* A superuser deleting its own account would lock everyone out, so that is the one thing this card does not offer them. */} {!currentUser?.is_superuser && (
)}
) } export default UserInformation