import { FC, useEffect, useState } from "react"; import { Controller, useForm } from "react-hook-form"; // icons import { Lock } from "lucide-react"; // ui import { Button, CustomSelect, Input, TextArea, TOAST_TYPE, setToast } from "@plane/ui"; // components import { ImagePickerPopover } from "components/core"; import EmojiIconPicker from "components/emoji-icon-picker"; // constants import { PROJECT_UPDATED } from "constants/event-tracker"; import { NETWORK_CHOICES } from "constants/project"; // helpers import { renderFormattedDate } from "helpers/date-time.helper"; import { renderEmoji } from "helpers/emoji.helper"; // hooks import { useEventTracker, useProject } from "hooks/store"; // services import { ProjectService } from "services/project"; // types import { IProject, IWorkspace } from "@plane/types"; export interface IProjectDetailsForm { project: IProject; workspaceSlug: string; projectId: string; isAdmin: boolean; } const projectService = new ProjectService(); export const ProjectDetailsForm: FC = (props) => { const { project, workspaceSlug, projectId, isAdmin } = props; // states const [isLoading, setIsLoading] = useState(false); // store hooks const { captureProjectEvent } = useEventTracker(); const { updateProject } = useProject(); // form info const { handleSubmit, watch, control, setValue, setError, reset, formState: { errors, dirtyFields }, getValues, } = useForm({ defaultValues: { ...project, emoji_and_icon: project.emoji ?? project.icon_prop, workspace: (project.workspace as IWorkspace).id, }, }); useEffect(() => { if (project && projectId !== getValues("id")) { reset({ ...project, emoji_and_icon: project.emoji ?? project.icon_prop, workspace: (project.workspace as IWorkspace).id, }); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [project, projectId]); const handleIdentifierChange = (event: React.ChangeEvent) => { const { value } = event.target; const alphanumericValue = value.replace(/[^a-zA-Z0-9]/g, ""); const formattedValue = alphanumericValue.toUpperCase(); setValue("identifier", formattedValue); }; const handleUpdateChange = async (payload: Partial) => { if (!workspaceSlug || !project) return; return updateProject(workspaceSlug.toString(), project.id, payload) .then((res) => { const changed_properties = Object.keys(dirtyFields); captureProjectEvent({ eventName: PROJECT_UPDATED, payload: { ...res, changed_properties: changed_properties, state: "SUCCESS", element: "Project general settings", }, }); setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: "Project updated successfully", }); }) .catch((error) => { captureProjectEvent({ eventName: PROJECT_UPDATED, payload: { ...payload, state: "FAILED", element: "Project general settings" }, }); setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: error?.error ?? "Project could not be updated. Please try again.", }); }); }; const onSubmit = async (formData: IProject) => { if (!workspaceSlug) return; setIsLoading(true); const payload: Partial = { name: formData.name, network: formData.network, identifier: formData.identifier, description: formData.description, cover_image: formData.cover_image, }; if (typeof formData.emoji_and_icon === "object") { payload.emoji = null; payload.icon_prop = formData.emoji_and_icon; } else { payload.emoji = formData.emoji_and_icon; payload.icon_prop = null; } if (project.identifier !== formData.identifier) await projectService .checkProjectIdentifierAvailability(workspaceSlug as string, payload.identifier ?? "") .then(async (res) => { if (res.exists) setError("identifier", { message: "Identifier already exists" }); else await handleUpdateChange(payload); }); else await handleUpdateChange(payload); setTimeout(() => { setIsLoading(false); }, 300); }; const currentNetwork = NETWORK_CHOICES.find((n) => n.key === project?.network); return (
{watch("cover_image")!}
( )} />
{watch("name")} {watch("identifier")} . {project.network === 0 && } {currentNetwork?.label}
( )} />

Project Name

( )} />

Description

(