2024-02-16 14:37:04 +00:00
|
|
|
import { FC, useState, useEffect, useCallback } from "react";
|
|
|
|
import { observer } from "mobx-react";
|
|
|
|
// components
|
|
|
|
import { TextArea } from "@plane/ui";
|
|
|
|
// types
|
2024-05-01 12:31:30 +00:00
|
|
|
import { cn } from "@/helpers/common.helper";
|
2024-03-19 14:38:35 +00:00
|
|
|
import useDebounce from "@/hooks/use-debounce";
|
2024-02-16 14:37:04 +00:00
|
|
|
import { TIssueOperations } from "./issue-detail";
|
|
|
|
// hooks
|
|
|
|
|
|
|
|
export type IssueTitleInputProps = {
|
|
|
|
disabled?: boolean;
|
|
|
|
value: string | undefined | null;
|
|
|
|
workspaceSlug: string;
|
2024-02-18 09:58:37 +00:00
|
|
|
isSubmitting: "submitting" | "submitted" | "saved";
|
2024-02-16 14:37:04 +00:00
|
|
|
setIsSubmitting: (value: "submitting" | "submitted" | "saved") => void;
|
|
|
|
issueOperations: TIssueOperations;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
2024-05-01 12:31:30 +00:00
|
|
|
className?: string;
|
|
|
|
containerClassName?: string;
|
2024-02-16 14:37:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const IssueTitleInput: FC<IssueTitleInputProps> = observer((props) => {
|
2024-05-01 12:31:30 +00:00
|
|
|
const {
|
|
|
|
disabled,
|
|
|
|
value,
|
|
|
|
workspaceSlug,
|
|
|
|
isSubmitting,
|
|
|
|
setIsSubmitting,
|
|
|
|
issueId,
|
|
|
|
issueOperations,
|
|
|
|
projectId,
|
|
|
|
className,
|
|
|
|
containerClassName,
|
|
|
|
} = props;
|
2024-02-16 14:37:04 +00:00
|
|
|
// states
|
|
|
|
const [title, setTitle] = useState("");
|
2024-05-01 12:31:30 +00:00
|
|
|
const [isLengthVisible, setIsLengthVisible] = useState(false);
|
2024-02-16 14:37:04 +00:00
|
|
|
// hooks
|
|
|
|
const debouncedValue = useDebounce(title, 1500);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (value) setTitle(value);
|
|
|
|
}, [value]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-02-25 17:07:25 +00:00
|
|
|
const textarea = document.querySelector("#title-input");
|
2024-02-18 18:47:31 +00:00
|
|
|
if (debouncedValue && debouncedValue !== value) {
|
2024-05-21 10:59:30 +00:00
|
|
|
if (debouncedValue.trim().length > 0) {
|
|
|
|
issueOperations.update(workspaceSlug, projectId, issueId, { name: debouncedValue }).finally(() => {
|
|
|
|
setIsSubmitting("saved");
|
|
|
|
if (textarea && !textarea.matches(":focus")) {
|
|
|
|
const trimmedTitle = debouncedValue.trim();
|
|
|
|
if (trimmedTitle !== title) setTitle(trimmedTitle);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setTitle(value || "");
|
2024-02-16 14:37:04 +00:00
|
|
|
setIsSubmitting("saved");
|
2024-05-21 10:59:30 +00:00
|
|
|
}
|
2024-02-16 14:37:04 +00:00
|
|
|
}
|
|
|
|
// DO NOT Add more dependencies here. It will cause multiple requests to be sent.
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [debouncedValue]);
|
|
|
|
|
2024-02-25 17:07:25 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const handleBlur = () => {
|
|
|
|
const trimmedTitle = title.trim();
|
|
|
|
if (trimmedTitle !== title && isSubmitting !== "submitting") {
|
2024-05-21 10:59:30 +00:00
|
|
|
if (trimmedTitle.length > 0) {
|
|
|
|
setTitle(trimmedTitle);
|
|
|
|
setIsSubmitting("submitting");
|
|
|
|
} else {
|
|
|
|
setTitle(value || "");
|
|
|
|
setIsSubmitting("saved");
|
|
|
|
}
|
2024-02-25 17:07:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const textarea = document.querySelector("#title-input"); // You might need to change this selector according to your TextArea component
|
|
|
|
if (textarea) {
|
|
|
|
textarea.addEventListener("blur", handleBlur);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (textarea) {
|
|
|
|
textarea.removeEventListener("blur", handleBlur);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [title, isSubmitting, setIsSubmitting]);
|
|
|
|
|
2024-02-16 14:37:04 +00:00
|
|
|
const handleTitleChange = useCallback(
|
|
|
|
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
setIsSubmitting("submitting");
|
|
|
|
setTitle(e.target.value);
|
|
|
|
},
|
2024-02-18 09:58:37 +00:00
|
|
|
[setIsSubmitting]
|
2024-02-16 14:37:04 +00:00
|
|
|
);
|
|
|
|
|
2024-02-21 11:12:04 +00:00
|
|
|
if (disabled) return <div className="text-2xl font-medium">{title}</div>;
|
|
|
|
|
2024-02-16 14:37:04 +00:00
|
|
|
return (
|
2024-05-21 10:59:30 +00:00
|
|
|
<div className="flex flex-col gap-1.5">
|
|
|
|
<div className={cn("relative", containerClassName)}>
|
|
|
|
<TextArea
|
|
|
|
id="title-input"
|
|
|
|
className={cn(
|
|
|
|
"block w-full resize-none overflow-hidden rounded border-none bg-transparent px-3 py-0 text-2xl font-medium outline-none ring-0",
|
|
|
|
{
|
2024-06-07 10:39:56 +00:00
|
|
|
"ring-1 ring-red-400 mx-3": title.length && title.length === 0,
|
2024-05-21 10:59:30 +00:00
|
|
|
},
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
disabled={disabled}
|
|
|
|
value={title}
|
|
|
|
onChange={handleTitleChange}
|
|
|
|
maxLength={255}
|
|
|
|
placeholder="Issue title"
|
|
|
|
onFocus={() => setIsLengthVisible(true)}
|
|
|
|
onBlur={() => setIsLengthVisible(false)}
|
|
|
|
/>
|
|
|
|
<div
|
|
|
|
className={cn(
|
|
|
|
"pointer-events-none absolute bottom-1 right-1 z-[2] rounded bg-custom-background-100 p-0.5 text-xs text-custom-text-200 opacity-0 transition-opacity",
|
|
|
|
{
|
|
|
|
"opacity-100": isLengthVisible,
|
|
|
|
}
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<span className={`${title.length === 0 || title.length > 255 ? "text-red-500" : ""}`}>{title.length}</span>
|
|
|
|
/255
|
|
|
|
</div>
|
2024-02-16 14:37:04 +00:00
|
|
|
</div>
|
2024-05-21 10:59:30 +00:00
|
|
|
{title.length === 0 && <span className="text-sm text-red-500">Title is required</span>}
|
2024-02-16 14:37:04 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|