import { useState, useEffect, Fragment, FC, ChangeEvent } from "react"; import { observer } from "mobx-react-lite"; import { useForm, Controller } from "react-hook-form"; import { Dialog, Transition } from "@headlessui/react"; import { X } from "lucide-react"; // ui import { Button, CustomEmojiIconPicker, CustomSelect, EmojiIconPickerTypes, Input, setToast, TextArea, TOAST_TYPE, } from "@plane/ui"; // components import { ImagePickerPopover } from "components/core"; import { MemberDropdown } from "components/dropdowns"; // constants import { PROJECT_CREATED } from "constants/event-tracker"; import { NETWORK_CHOICES, PROJECT_UNSPLASH_COVERS } from "constants/project"; import { EUserWorkspaceRoles } from "constants/workspace"; // helpers import { convertHexEmojiToDecimal, getRandomEmoji } from "helpers/emoji.helper"; // hooks import { useEventTracker, useProject, useUser } from "hooks/store"; import { projectIdentifierSanitizer } from "helpers/project.helper"; import { ProjectLogo } from "./project-logo"; import { IProject } from "@plane/types"; type Props = { isOpen: boolean; onClose: () => void; setToFavorite?: boolean; workspaceSlug: string; }; interface IIsGuestCondition { onClose: () => void; } const defaultValues: Partial = { cover_image: PROJECT_UNSPLASH_COVERS[Math.floor(Math.random() * PROJECT_UNSPLASH_COVERS.length)], description: "", logo_props: { in_use: "emoji", emoji: { value: getRandomEmoji(), }, }, identifier: "", name: "", network: 2, project_lead: null, }; const IsGuestCondition: FC = ({ onClose }) => { useEffect(() => { onClose(); setToast({ title: "Error", type: TOAST_TYPE.ERROR, message: "You don't have permission to create project.", }); }, [onClose]); return null; }; export const CreateProjectModal: FC = observer((props) => { const { isOpen, onClose, setToFavorite = false, workspaceSlug } = props; // store const { captureProjectEvent } = useEventTracker(); const { membership: { currentWorkspaceRole }, } = useUser(); const { addProjectToFavorites, createProject } = useProject(); // states const [isChangeInIdentifierRequired, setIsChangeInIdentifierRequired] = useState(true); // form info const { formState: { errors, isSubmitting }, handleSubmit, reset, control, watch, setValue, } = useForm({ defaultValues, reValidateMode: "onChange", }); if (currentWorkspaceRole && isOpen) if (currentWorkspaceRole < EUserWorkspaceRoles.MEMBER) return ; const handleClose = () => { onClose(); setIsChangeInIdentifierRequired(true); setTimeout(() => { reset(); }, 300); }; const handleAddToFavorites = (projectId: string) => { if (!workspaceSlug) return; addProjectToFavorites(workspaceSlug.toString(), projectId).catch(() => { setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Couldn't remove the project from favorites. Please try again.", }); }); }; const onSubmit = async (formData: Partial) => { // Upper case identifier formData.identifier = formData.identifier?.toUpperCase(); return createProject(workspaceSlug.toString(), formData) .then((res) => { const newPayload = { ...res, state: "SUCCESS", }; captureProjectEvent({ eventName: PROJECT_CREATED, payload: newPayload, }); setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: "Project created successfully.", }); if (setToFavorite) { handleAddToFavorites(res.id); } handleClose(); }) .catch((err) => { Object.keys(err.data).map((key) => { setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: err.data[key], }); captureProjectEvent({ eventName: PROJECT_CREATED, payload: { ...formData, state: "FAILED", }, }); }); }); }; const handleNameChange = (onChange: any) => (e: ChangeEvent) => { if (!isChangeInIdentifierRequired) { onChange(e); return; } if (e.target.value === "") setValue("identifier", ""); else setValue("identifier", projectIdentifierSanitizer(e.target.value).substring(0, 5)); onChange(e); }; const handleIdentifierChange = (onChange: any) => (e: ChangeEvent) => { const { value } = e.target; const alphanumericValue = projectIdentifierSanitizer(value); setIsChangeInIdentifierRequired(false); onChange(alphanumericValue); }; return (
{watch("cover_image") && ( Cover image )}
( )} />
( } onChange={(val: any) => { let logoValue = {}; if (val.type === "emoji") logoValue = { value: convertHexEmojiToDecimal(val.value.unified), url: val.value.imageUrl, }; else if (val.type === "icon") logoValue = val.value; onChange({ in_use: val.type, [val.type]: logoValue, }); }} defaultIconColor={value.in_use === "icon" ? value.icon?.color : undefined} defaultOpen={ value.in_use === "emoji" ? EmojiIconPickerTypes.EMOJI : EmojiIconPickerTypes.ICON } /> )} />
( )} /> <>{errors?.name?.message}
/^[ÇŞĞIİÖÜA-Z0-9]+$/.test(value.toUpperCase()) || "Only Alphanumeric & Non-latin characters are allowed.", minLength: { value: 1, message: "Identifier must at least be of 1 character", }, maxLength: { value: 12, message: "Identifier must at most be of 12 characters", }, }} render={({ field: { value, onChange } }) => ( )} /> <>{errors?.identifier?.message}
(