2024-01-10 14:39:45 +00:00
|
|
|
import { FC, useState, Fragment, useEffect } from "react";
|
|
|
|
import { TwitterPicker } from "react-color";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2024-03-22 13:00:23 +00:00
|
|
|
import { usePopper } from "react-popper";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { Plus, X, Loader } from "lucide-react";
|
2024-03-22 13:00:23 +00:00
|
|
|
import { Popover } from "@headlessui/react";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { IIssueLabel } from "@plane/types";
|
2024-01-10 14:39:45 +00:00
|
|
|
// hooks
|
2024-03-06 13:09:14 +00:00
|
|
|
import { Input, TOAST_TYPE, setToast } from "@plane/ui";
|
2024-01-10 14:39:45 +00:00
|
|
|
// ui
|
|
|
|
// types
|
2024-03-06 13:09:14 +00:00
|
|
|
import { TLabelOperations } from "./root";
|
2024-01-10 14:39:45 +00:00
|
|
|
|
|
|
|
type ILabelCreate = {
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
2024-04-08 13:41:47 +00:00
|
|
|
values: string[];
|
2024-01-10 14:39:45 +00:00
|
|
|
labelOperations: TLabelOperations;
|
|
|
|
disabled?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: Partial<IIssueLabel> = {
|
|
|
|
name: "",
|
|
|
|
color: "#ff0000",
|
|
|
|
};
|
|
|
|
|
|
|
|
export const LabelCreate: FC<ILabelCreate> = (props) => {
|
2024-04-08 13:41:47 +00:00
|
|
|
const { workspaceSlug, projectId, issueId, values, labelOperations, disabled = false } = props;
|
2024-01-10 14:39:45 +00:00
|
|
|
// state
|
|
|
|
const [isCreateToggle, setIsCreateToggle] = useState(false);
|
|
|
|
const handleIsCreateToggle = () => setIsCreateToggle(!isCreateToggle);
|
2024-03-22 13:00:23 +00:00
|
|
|
const [referenceElement, setReferenceElement] = useState<HTMLButtonElement | null>(null);
|
|
|
|
const [popperElement, setPopperElement] = useState<HTMLDivElement | null>(null);
|
2024-01-10 14:39:45 +00:00
|
|
|
// react hook form
|
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
reset,
|
|
|
|
control,
|
|
|
|
setFocus,
|
|
|
|
} = useForm<Partial<IIssueLabel>>({
|
|
|
|
defaultValues,
|
|
|
|
});
|
|
|
|
|
2024-03-22 13:00:23 +00:00
|
|
|
const { styles, attributes } = usePopper(referenceElement, popperElement, {
|
|
|
|
placement: "bottom-start",
|
|
|
|
modifiers: [
|
|
|
|
{
|
|
|
|
name: "preventOverflow",
|
|
|
|
options: {
|
|
|
|
padding: 12,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2024-01-10 14:39:45 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!isCreateToggle) return;
|
|
|
|
|
|
|
|
setFocus("name");
|
|
|
|
reset();
|
|
|
|
}, [isCreateToggle, reset, setFocus]);
|
|
|
|
|
|
|
|
const handleLabel = async (formData: Partial<IIssueLabel>) => {
|
|
|
|
if (!workspaceSlug || !projectId || isSubmitting) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const labelResponse = await labelOperations.createLabel(workspaceSlug, projectId, formData);
|
2024-04-08 13:41:47 +00:00
|
|
|
const currentLabels = [...(values || []), labelResponse.id];
|
2024-01-10 14:39:45 +00:00
|
|
|
await labelOperations.updateIssue(workspaceSlug, projectId, issueId, { label_ids: currentLabels });
|
2024-04-10 08:22:57 +00:00
|
|
|
handleIsCreateToggle();
|
2024-01-10 14:39:45 +00:00
|
|
|
reset(defaultValues);
|
|
|
|
} catch (error) {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
2024-05-07 09:33:24 +00:00
|
|
|
title: "Error!",
|
2024-03-06 08:48:41 +00:00
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-01-10 14:39:45 +00:00
|
|
|
message: "Label creation failed. Please try again sometime later.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div
|
2024-03-06 13:09:14 +00:00
|
|
|
className="relative flex flex-shrink-0 cursor-pointer items-center gap-1 rounded-full border border-custom-border-100 p-0.5 px-2 text-xs text-custom-text-300 transition-all hover:bg-custom-background-90 hover:text-custom-text-200"
|
2024-01-10 14:39:45 +00:00
|
|
|
onClick={handleIsCreateToggle}
|
|
|
|
>
|
|
|
|
<div className="flex-shrink-0">
|
2024-01-11 12:56:58 +00:00
|
|
|
{isCreateToggle ? <X className="h-2.5 w-2.5" /> : <Plus className="h-2.5 w-2.5" />}
|
2024-01-10 14:39:45 +00:00
|
|
|
</div>
|
|
|
|
<div className="flex-shrink-0">{isCreateToggle ? "Cancel" : "New"}</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{isCreateToggle && (
|
2024-03-20 12:37:11 +00:00
|
|
|
<form className="relative flex items-center gap-x-2" onSubmit={handleSubmit(handleLabel)}>
|
2024-01-10 14:39:45 +00:00
|
|
|
<div>
|
|
|
|
<Controller
|
|
|
|
name="color"
|
|
|
|
control={control}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
2024-03-20 12:37:11 +00:00
|
|
|
<Popover>
|
2024-01-10 14:39:45 +00:00
|
|
|
<>
|
2024-03-22 13:00:23 +00:00
|
|
|
<Popover.Button as={Fragment}>
|
|
|
|
<button type="button" ref={setReferenceElement} className="grid place-items-center outline-none">
|
|
|
|
{value && value?.trim() !== "" && (
|
|
|
|
<span
|
|
|
|
className="h-6 w-6 rounded"
|
|
|
|
style={{
|
|
|
|
backgroundColor: value ?? "black",
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</button>
|
2024-01-10 14:39:45 +00:00
|
|
|
</Popover.Button>
|
2024-03-22 13:00:23 +00:00
|
|
|
<Popover.Panel className="fixed z-10">
|
|
|
|
<div
|
|
|
|
className="p-2 max-w-xs sm:px-0"
|
|
|
|
ref={setPopperElement}
|
|
|
|
style={styles.popper}
|
|
|
|
{...attributes.popper}
|
2024-03-20 12:37:11 +00:00
|
|
|
>
|
2024-03-22 13:00:23 +00:00
|
|
|
<TwitterPicker triangle={"hide"} color={value} onChange={(value) => onChange(value.hex)} />
|
|
|
|
</div>
|
|
|
|
</Popover.Panel>
|
2024-01-10 14:39:45 +00:00
|
|
|
</>
|
|
|
|
</Popover>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="name"
|
|
|
|
rules={{
|
|
|
|
required: "This is required",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange, ref } }) => (
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
type="text"
|
|
|
|
value={value ?? ""}
|
|
|
|
onChange={onChange}
|
|
|
|
ref={ref}
|
|
|
|
hasError={Boolean(errors.name)}
|
|
|
|
placeholder="Title"
|
|
|
|
className="w-full"
|
2024-01-25 08:57:35 +00:00
|
|
|
disabled={isSubmitting}
|
2024-01-10 14:39:45 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="grid place-items-center rounded bg-red-500 p-1.5"
|
|
|
|
onClick={() => setIsCreateToggle(false)}
|
|
|
|
disabled={disabled}
|
|
|
|
>
|
|
|
|
<X className="h-4 w-4 text-white" />
|
|
|
|
</button>
|
|
|
|
<button type="submit" className="grid place-items-center rounded bg-green-500 p-1.5" disabled={isSubmitting}>
|
2024-03-06 13:09:14 +00:00
|
|
|
{isSubmitting ? <Loader className="spin h-4 w-4 text-white" /> : <Plus className="h-4 w-4 text-white" />}
|
2024-01-10 14:39:45 +00:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|