import { ChangeEvent, FC, useCallback, useEffect, useState } from "react"; import { Controller, useForm } from "react-hook-form"; // hooks import useReloadConfirmations from "hooks/use-reload-confirmation"; import debounce from "lodash/debounce"; // components import { TextArea } from "@plane/ui"; import { RichTextEditor } from "@plane/rich-text-editor"; // types import { IIssue } from "types"; // services import { FileService } from "services/file.service"; import useEditorSuggestions from "hooks/use-editor-suggestions"; export interface IssueDescriptionFormValues { name: string; description_html: string; } export interface IssueDetailsProps { issue: { name: string; description_html: string; project_id?: string; }; workspaceSlug: string; handleFormSubmit: (value: IssueDescriptionFormValues) => Promise; isAllowed: boolean; } const fileService = new FileService(); export const IssueDescriptionForm: FC = (props) => { const { issue, handleFormSubmit, workspaceSlug, isAllowed } = props; // states const [isSubmitting, setIsSubmitting] = useState<"submitting" | "submitted" | "saved">("saved"); const [characterLimit, setCharacterLimit] = useState(false); const { setShowAlert } = useReloadConfirmations(); const editorSuggestion = useEditorSuggestions(); const { handleSubmit, watch, reset, control, formState: { errors }, } = useForm({ defaultValues: { name: "", description_html: "", }, }); const [localTitleValue, setLocalTitleValue] = useState(""); const issueTitleCurrentValue = watch("name"); useEffect(() => { if (localTitleValue === "" && issueTitleCurrentValue !== "") { setLocalTitleValue(issueTitleCurrentValue); } }, [issueTitleCurrentValue, localTitleValue]); const handleDescriptionFormSubmit = useCallback( async (formData: Partial) => { if (!formData?.name || formData?.name.length === 0 || formData?.name.length > 255) return; await handleFormSubmit({ name: formData.name ?? "", description_html: formData.description_html ?? "

", }); }, [handleFormSubmit] ); useEffect(() => { if (isSubmitting === "submitted") { setShowAlert(false); setTimeout(async () => { setIsSubmitting("saved"); }, 2000); } else if (isSubmitting === "submitting") { setShowAlert(true); } }, [isSubmitting, setShowAlert]); // reset form values useEffect(() => { if (!issue) return; reset({ ...issue, }); }, [issue, reset]); const debouncedFormSave = debounce(async () => { handleSubmit(handleDescriptionFormSubmit)().finally(() => setIsSubmitting("submitted")); }, 1500); return (
{isAllowed ? ( (