import React, { useEffect, useState, ReactElement } from "react"; import { observer } from "mobx-react"; import { Controller, useForm } from "react-hook-form"; import { ChevronDown, User2 } from "lucide-react"; import { Disclosure, Transition } from "@headlessui/react"; // services // hooks // layouts // components import type { IUser } from "@plane/types"; import { Button, CustomSelect, CustomSearchSelect, Input, Spinner, TOAST_TYPE, setPromiseToast, setToast, } from "@plane/ui"; import { DeactivateAccountModal } from "@/components/account"; import { ImagePickerPopover, UserImageUploadModal, PageHead } from "@/components/core"; // ui // icons // components import { SidebarHamburgerToggle } from "@/components/core/sidebar"; // constants import { TIME_ZONES } from "@/constants/timezones"; import { USER_ROLES } from "@/constants/workspace"; // hooks import { useAppTheme, useUser } from "@/hooks/store"; import { ProfileSettingsLayout } from "@/layouts/settings-layout"; // layouts // lib types import type { NextPageWithLayout } from "@/lib/types"; import { FileService } from "@/services/file.service"; // services // types const defaultValues: Partial = { avatar: "", cover_image: "", first_name: "", last_name: "", display_name: "", email: "", role: "Product / Project Manager", user_timezone: "Asia/Kolkata", }; const fileService = new FileService(); const ProfileSettingsPage: NextPageWithLayout = observer(() => { // states const [isLoading, setIsLoading] = useState(false); const [isRemoving, setIsRemoving] = useState(false); const [isImageUploadModalOpen, setIsImageUploadModalOpen] = useState(false); const [deactivateAccountModal, setDeactivateAccountModal] = useState(false); // form info const { handleSubmit, reset, watch, control, formState: { errors }, } = useForm({ defaultValues }); // store hooks const { data: currentUser, updateCurrentUser } = useUser(); const { toggleSidebar } = useAppTheme(); useEffect(() => { reset({ ...defaultValues, ...currentUser }); }, [currentUser, reset]); const onSubmit = async (formData: IUser) => { setIsLoading(true); const payload: Partial = { first_name: formData.first_name, last_name: formData.last_name, avatar: formData.avatar, cover_image: formData.cover_image, role: formData.role, display_name: formData?.display_name, user_timezone: formData.user_timezone, }; const updateCurrentUserDetail = updateCurrentUser(payload).finally(() => setIsLoading(false)); setPromiseToast(updateCurrentUserDetail, { loading: "Updating...", success: { title: "Success!", message: () => `Profile updated successfully.`, }, error: { title: "Error!", message: () => `There was some error in updating your profile. Please try again.`, }, }); }; const handleDelete = (url: string | null | undefined, updateUser: boolean = false) => { if (!url) return; setIsRemoving(true); fileService.deleteUserFile(url).then(() => { if (updateUser) updateCurrentUser({ avatar: "" }) .then(() => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: "Profile picture deleted successfully.", }); setIsRemoving(false); }) .catch(() => { setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "There was some error in deleting your profile picture. Please try again.", }); }) .finally(() => setIsRemoving(false)); }); }; const timeZoneOptions = TIME_ZONES.map((timeZone) => ({ value: timeZone.value, query: timeZone.label + " " + timeZone.value, content: timeZone.label, })); if (!currentUser) return (
); return ( <>
toggleSidebar()} />
( setIsImageUploadModalOpen(false)} isRemoving={isRemoving} handleDelete={() => handleDelete(currentUser?.avatar, true)} onSuccess={(url) => { onChange(url); handleSubmit(onSubmit)(); setIsImageUploadModalOpen(false); }} value={value && value.trim() !== "" ? value : null} /> )} /> setDeactivateAccountModal(false)} />
{currentUser?.first_name
( onChange(imageUrl)} control={control} value={value ?? "https://images.unsplash.com/photo-1506383796573-caf02b4a79ab"} isProfileCover /> )} />
{`${watch("first_name")} ${watch("last_name")}`}
{watch("email")}
{/* Activity Overview */}

First name*

( )} /> {errors.first_name && Please enter first name}

Last name

( )} />

Email*

( )} />

Role*

( {USER_ROLES.map((item) => ( {item.label} ))} )} /> {errors.role && Please select a role}

Display name*

{ if (value.trim().length < 1) return "Display name can't be empty."; if (value.split(" ").length > 1) return "Display name can't have two consecutive spaces."; if (value.replace(/\s/g, "").length < 1) return "Display name must be at least 1 characters long."; if (value.replace(/\s/g, "").length > 20) return "Display name must be less than 20 characters long."; return true; }, }} render={({ field: { value, onChange, ref } }) => ( )} /> {errors?.display_name && Please enter display name}

Timezone*

( t.value === value)?.label ?? value : "Select a timezone" } options={timeZoneOptions} onChange={onChange} optionsClassName="w-full" buttonClassName={errors.user_timezone ? "border-red-500" : "border-none"} className="rounded-md border-[0.5px] !border-custom-border-200" input /> )} /> {errors.role && Please select a time zone}
{({ open }) => ( <> Deactivate account
The danger zone of the profile page is a critical area that requires careful consideration and attention. When deactivating an account, all of the data and resources within that account will be permanently removed and cannot be recovered.
)}
); }); ProfileSettingsPage.getLayout = function getLayout(page: ReactElement) { return {page}; }; export default ProfileSettingsPage;