"use client"; import { FC, useState, useEffect, useCallback } from "react"; import { observer } from "mobx-react"; // components import { TextArea } from "@plane/ui"; // types import { cn } from "@/helpers/common.helper"; import useDebounce from "@/hooks/use-debounce"; import { TIssueOperations } from "./issue-detail"; // hooks export type IssueTitleInputProps = { disabled?: boolean; value: string | undefined | null; workspaceSlug: string; isSubmitting: "submitting" | "submitted" | "saved"; setIsSubmitting: (value: "submitting" | "submitted" | "saved") => void; issueOperations: TIssueOperations; projectId: string; issueId: string; className?: string; containerClassName?: string; }; export const IssueTitleInput: FC = observer((props) => { const { disabled, value, workspaceSlug, isSubmitting, setIsSubmitting, issueId, issueOperations, projectId, className, containerClassName, } = props; // states const [title, setTitle] = useState(""); const [isLengthVisible, setIsLengthVisible] = useState(false); // hooks const debouncedValue = useDebounce(title, 1500); useEffect(() => { if (value) setTitle(value); }, [value]); useEffect(() => { const textarea = document.querySelector("#title-input"); if (debouncedValue && debouncedValue !== value) { 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 || ""); setIsSubmitting("saved"); } } // DO NOT Add more dependencies here. It will cause multiple requests to be sent. // eslint-disable-next-line react-hooks/exhaustive-deps }, [debouncedValue]); useEffect(() => { const handleBlur = () => { const trimmedTitle = title.trim(); if (trimmedTitle !== title && isSubmitting !== "submitting") { if (trimmedTitle.length > 0) { setTitle(trimmedTitle); setIsSubmitting("submitting"); } else { setTitle(value || ""); setIsSubmitting("saved"); } } }; 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]); const handleTitleChange = useCallback( (e: React.ChangeEvent) => { setIsSubmitting("submitting"); setTitle(e.target.value); }, [setIsSubmitting] ); if (disabled) return
{title}
; return (