import { FC, useState, Fragment, useEffect } from "react"; import { TwitterPicker } from "react-color"; import { Controller, useForm } from "react-hook-form"; import { usePopper } from "react-popper"; import { Plus, X, Loader } from "lucide-react"; import { Popover } from "@headlessui/react"; import { IIssueLabel } from "@plane/types"; // hooks import { Input, TOAST_TYPE, setToast } from "@plane/ui"; // ui // types import { TLabelOperations } from "./root"; type ILabelCreate = { workspaceSlug: string; projectId: string; issueId: string; values: string[]; labelOperations: TLabelOperations; disabled?: boolean; }; const defaultValues: Partial = { name: "", color: "#ff0000", }; export const LabelCreate: FC = (props) => { const { workspaceSlug, projectId, issueId, values, labelOperations, disabled = false } = props; // state const [isCreateToggle, setIsCreateToggle] = useState(false); const handleIsCreateToggle = () => setIsCreateToggle(!isCreateToggle); const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); // react hook form const { handleSubmit, formState: { errors, isSubmitting }, reset, control, setFocus, } = useForm>({ defaultValues, }); const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: "bottom-start", modifiers: [ { name: "preventOverflow", options: { padding: 12, }, }, ], }); useEffect(() => { if (!isCreateToggle) return; setFocus("name"); reset(); }, [isCreateToggle, reset, setFocus]); const handleLabel = async (formData: Partial) => { if (!workspaceSlug || !projectId || isSubmitting) return; try { const labelResponse = await labelOperations.createLabel(workspaceSlug, projectId, formData); const currentLabels = [...(values || []), labelResponse.id]; await labelOperations.updateIssue(workspaceSlug, projectId, issueId, { label_ids: currentLabels }); reset(defaultValues); } catch (error) { setToast({ title: "Label creation failed", type: TOAST_TYPE.ERROR, message: "Label creation failed. Please try again sometime later.", }); } }; return ( <>
{isCreateToggle ? : }
{isCreateToggle ? "Cancel" : "New"}
{isCreateToggle && (
( <>
onChange(value.hex)} />
)} />
( )} /> )} ); };