import React, { useEffect } from "react"; import { observer } from "mobx-react-lite"; import { useRouter } from "next/router"; import { TwitterPicker } from "react-color"; import { Controller, useForm } from "react-hook-form"; import { ChevronDown } from "lucide-react"; import { Dialog, Popover, Transition } from "@headlessui/react"; import type { IIssueLabel, IState } from "@plane/types"; // hooks import { Button, Input, TOAST_TYPE, setToast } from "@plane/ui"; import { LABEL_COLOR_OPTIONS, getRandomLabelColor } from "@/constants/label"; import { useLabel } from "@/hooks/store"; // ui // types // constants // types type Props = { isOpen: boolean; projectId: string; handleClose: () => void; onSuccess?: (response: IIssueLabel) => void; }; const defaultValues: Partial = { name: "", color: "rgb(var(--color-text-200))", }; export const CreateLabelModal: React.FC = observer((props) => { const { isOpen, projectId, handleClose, onSuccess } = props; // router const router = useRouter(); const { workspaceSlug } = router.query; // store hooks const { createLabel } = useLabel(); // form info const { formState: { errors, isSubmitting }, handleSubmit, watch, control, reset, setValue, setFocus, } = useForm({ defaultValues, }); /** * For setting focus on name input */ useEffect(() => { setFocus("name"); }, [setFocus, isOpen]); useEffect(() => { if (isOpen) setValue("color", getRandomLabelColor()); }, [setValue, isOpen]); const onClose = () => { handleClose(); reset(defaultValues); }; const onSubmit = async (formData: IIssueLabel) => { if (!workspaceSlug) return; await createLabel(workspaceSlug.toString(), projectId.toString(), formData) .then((res) => { onClose(); if (onSuccess) onSuccess(res); }) .catch((error) => { setToast({ title: "Oops!", type: TOAST_TYPE.ERROR, message: error?.error ?? "Error while adding the label", }); reset(formData); }); }; return (
Create Label
{({ open, close }) => ( <> {watch("color") && watch("color") !== "" && ( )} ( { onChange(value.hex); close(); }} /> )} /> )}
( )} />
); });