2024-04-11 15:58:59 +00:00
|
|
|
import { FC, useCallback, useEffect, useState } from "react";
|
|
|
|
import debounce from "lodash/debounce";
|
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
// types
|
|
|
|
import { TIssue } from "@plane/types";
|
|
|
|
// ui
|
2024-03-06 13:09:14 +00:00
|
|
|
import { Loader } from "@plane/ui";
|
2024-04-11 15:58:59 +00:00
|
|
|
// components
|
|
|
|
import { RichTextEditor, RichTextReadOnlyEditor } from "@/components/editor";
|
|
|
|
import { TIssueOperations } from "@/components/issues/issue-detail";
|
2024-04-30 11:51:52 +00:00
|
|
|
// helpers
|
|
|
|
import { getDescriptionPlaceholder } from "@/helpers/issue.helper";
|
2024-03-06 13:09:14 +00:00
|
|
|
// hooks
|
2024-04-11 15:58:59 +00:00
|
|
|
import { useWorkspace } from "@/hooks/store";
|
2024-02-16 14:37:04 +00:00
|
|
|
|
|
|
|
export type IssueDescriptionInputProps = {
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
2024-02-19 10:13:57 +00:00
|
|
|
initialValue: string | undefined;
|
|
|
|
disabled?: boolean;
|
|
|
|
issueOperations: TIssueOperations;
|
2024-04-30 11:51:52 +00:00
|
|
|
placeholder?: string | ((isFocused: boolean, value: string) => string);
|
2024-04-11 15:58:59 +00:00
|
|
|
setIsSubmitting: (initialValue: "submitting" | "submitted" | "saved") => void;
|
|
|
|
swrIssueDescription: string | null | undefined;
|
2024-02-16 14:37:04 +00:00
|
|
|
};
|
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
export const IssueDescriptionInput: FC<IssueDescriptionInputProps> = observer((props) => {
|
|
|
|
const {
|
|
|
|
workspaceSlug,
|
|
|
|
projectId,
|
|
|
|
issueId,
|
|
|
|
disabled,
|
|
|
|
swrIssueDescription,
|
|
|
|
initialValue,
|
|
|
|
issueOperations,
|
|
|
|
setIsSubmitting,
|
2024-04-15 14:30:02 +00:00
|
|
|
placeholder,
|
2024-04-11 15:58:59 +00:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { handleSubmit, reset, control } = useForm<TIssue>({
|
|
|
|
defaultValues: {
|
|
|
|
description_html: initialValue,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const [localIssueDescription, setLocalIssueDescription] = useState({
|
|
|
|
id: issueId,
|
|
|
|
description_html: initialValue,
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleDescriptionFormSubmit = useCallback(
|
|
|
|
async (formData: Partial<TIssue>) => {
|
|
|
|
await issueOperations.update(workspaceSlug, projectId, issueId, {
|
|
|
|
description_html: formData.description_html ?? "<p></p>",
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[workspaceSlug, projectId, issueId, issueOperations]
|
|
|
|
);
|
|
|
|
|
2024-02-18 09:58:37 +00:00
|
|
|
const { getWorkspaceBySlug } = useWorkspace();
|
2024-02-16 14:37:04 +00:00
|
|
|
// computed values
|
2024-02-18 09:58:37 +00:00
|
|
|
const workspaceId = getWorkspaceBySlug(workspaceSlug)?.id as string;
|
2024-02-16 14:37:04 +00:00
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
// reset form values
|
2024-02-16 14:37:04 +00:00
|
|
|
useEffect(() => {
|
2024-04-11 15:58:59 +00:00
|
|
|
if (!issueId) return;
|
|
|
|
reset({
|
|
|
|
id: issueId,
|
|
|
|
description_html: initialValue === "" ? "<p></p>" : initialValue,
|
|
|
|
});
|
|
|
|
setLocalIssueDescription({
|
|
|
|
id: issueId,
|
|
|
|
description_html: initialValue === "" ? "<p></p>" : initialValue,
|
|
|
|
});
|
|
|
|
}, [initialValue, issueId, reset]);
|
2024-02-16 14:37:04 +00:00
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
// ADDING handleDescriptionFormSubmit TO DEPENDENCY ARRAY PRODUCES ADVERSE EFFECTS
|
|
|
|
// TODO: Verify the exhaustive-deps warning
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
const debouncedFormSave = useCallback(
|
|
|
|
debounce(async () => {
|
|
|
|
handleSubmit(handleDescriptionFormSubmit)().finally(() => setIsSubmitting("submitted"));
|
|
|
|
}, 1500),
|
|
|
|
[handleSubmit, issueId]
|
|
|
|
);
|
2024-02-16 14:37:04 +00:00
|
|
|
|
|
|
|
return (
|
2024-04-11 15:58:59 +00:00
|
|
|
<>
|
|
|
|
{localIssueDescription.description_html ? (
|
|
|
|
<Controller
|
|
|
|
name="description_html"
|
|
|
|
control={control}
|
|
|
|
render={({ field: { onChange } }) =>
|
|
|
|
!disabled ? (
|
|
|
|
<RichTextEditor
|
|
|
|
id={issueId}
|
|
|
|
initialValue={localIssueDescription.description_html ?? "<p></p>"}
|
|
|
|
value={swrIssueDescription ?? null}
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
workspaceId={workspaceId}
|
|
|
|
projectId={projectId}
|
|
|
|
dragDropEnabled
|
|
|
|
onChange={(_description: object, description_html: string) => {
|
|
|
|
setIsSubmitting("submitting");
|
|
|
|
onChange(description_html);
|
|
|
|
debouncedFormSave();
|
|
|
|
}}
|
2024-04-15 14:30:02 +00:00
|
|
|
placeholder={
|
2024-04-30 11:51:52 +00:00
|
|
|
placeholder ? placeholder : (isFocused, value) => getDescriptionPlaceholder(isFocused, value)
|
2024-04-15 14:30:02 +00:00
|
|
|
}
|
2024-04-11 15:58:59 +00:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<RichTextReadOnlyEditor
|
|
|
|
initialValue={localIssueDescription.description_html ?? ""}
|
2024-04-15 14:15:47 +00:00
|
|
|
containerClassName="!p-0 !pt-2 text-custom-text-200 min-h-[150px]"
|
2024-04-11 15:58:59 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Loader>
|
|
|
|
<Loader.Item height="150px" />
|
|
|
|
</Loader>
|
|
|
|
)}
|
|
|
|
</>
|
2024-02-16 14:37:04 +00:00
|
|
|
);
|
2024-04-11 15:58:59 +00:00
|
|
|
});
|