import { FC, useCallback, useEffect, useState } from "react"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // hooks import useReloadConfirmations from "hooks/use-reload-confirmation"; import { useDebouncedCallback } from "use-debounce"; // components import { TextArea } from "components/ui"; import { TipTapEditor } from "components/tiptap"; // types import { IIssue } from "types"; export interface IssueDescriptionFormValues { name: string; description_html: string; } export interface IssueDetailsProps { issue: { name: string; description_html: string; }; workspaceSlug: string; handleFormSubmit: (value: IssueDescriptionFormValues) => Promise; isAllowed: boolean; } export const IssueDescriptionForm: FC = ({ issue, handleFormSubmit, workspaceSlug, isAllowed, }) => { const [isSubmitting, setIsSubmitting] = useState<"submitting" | "submitted" | "saved">("saved"); const [characterLimit, setCharacterLimit] = useState(false); const { setShowAlert } = useReloadConfirmations(); const { handleSubmit, watch, reset, register, control, formState: { errors }, } = useForm({ defaultValues: { name: "", description_html: "", }, }); 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 debouncedTitleSave = useDebouncedCallback(async () => { setTimeout(async () => { handleSubmit(handleDescriptionFormSubmit)().finally(() => setIsSubmitting("submitted")); }, 500); }, 1000); return (
{isAllowed ? (