2024-02-16 14:37:04 +00:00
|
|
|
import { FC, useState, useEffect } from "react";
|
|
|
|
// components
|
|
|
|
import { RichReadOnlyEditor, RichTextEditor } from "@plane/rich-text-editor";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { Loader } from "@plane/ui";
|
|
|
|
// hooks
|
2024-03-19 14:38:35 +00:00
|
|
|
import { useMention, useWorkspace } from "@/hooks/store";
|
|
|
|
import useDebounce from "@/hooks/use-debounce";
|
2024-02-16 14:37:04 +00:00
|
|
|
// services
|
2024-03-19 14:38:35 +00:00
|
|
|
import { FileService } from "@/services/file.service";
|
2024-02-16 14:37:04 +00:00
|
|
|
const fileService = new FileService();
|
|
|
|
// types
|
|
|
|
import { TIssueOperations } from "./issue-detail";
|
|
|
|
|
|
|
|
export type IssueDescriptionInputProps = {
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
2024-02-19 10:13:57 +00:00
|
|
|
value: string | undefined;
|
|
|
|
initialValue: string | undefined;
|
|
|
|
disabled?: boolean;
|
|
|
|
issueOperations: TIssueOperations;
|
|
|
|
setIsSubmitting: (value: "submitting" | "submitted" | "saved") => void;
|
2024-02-16 14:37:04 +00:00
|
|
|
};
|
|
|
|
|
2024-02-19 10:13:57 +00:00
|
|
|
export const IssueDescriptionInput: FC<IssueDescriptionInputProps> = (props) => {
|
|
|
|
const { workspaceSlug, projectId, issueId, value, initialValue, disabled, issueOperations, setIsSubmitting } = props;
|
2024-02-16 14:37:04 +00:00
|
|
|
// states
|
2024-04-10 09:28:46 +00:00
|
|
|
const [localIssueId, setLocalIssueId] = useState(issueId);
|
2024-02-16 14:37:04 +00:00
|
|
|
const [descriptionHTML, setDescriptionHTML] = useState(value);
|
|
|
|
// store hooks
|
|
|
|
const { mentionHighlights, mentionSuggestions } = useMention();
|
2024-02-18 09:58:37 +00:00
|
|
|
const { getWorkspaceBySlug } = useWorkspace();
|
2024-02-16 14:37:04 +00:00
|
|
|
// hooks
|
|
|
|
const debouncedValue = useDebounce(descriptionHTML, 1500);
|
|
|
|
// 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
|
|
|
|
|
|
|
useEffect(() => {
|
2024-04-10 09:28:46 +00:00
|
|
|
if (issueId !== localIssueId) {
|
|
|
|
setDescriptionHTML(undefined);
|
|
|
|
setLocalIssueId(issueId);
|
|
|
|
} else {
|
|
|
|
setDescriptionHTML(value);
|
|
|
|
}
|
|
|
|
return () => setDescriptionHTML(undefined);
|
|
|
|
}, [issueId, localIssueId, value]);
|
2024-02-16 14:37:04 +00:00
|
|
|
|
2024-02-18 09:58:37 +00:00
|
|
|
useEffect(() => {
|
2024-02-18 18:47:31 +00:00
|
|
|
if (debouncedValue && debouncedValue !== value) {
|
2024-03-06 08:48:41 +00:00
|
|
|
issueOperations.update(workspaceSlug, projectId, issueId, { description_html: debouncedValue }).finally(() => {
|
|
|
|
setIsSubmitting("submitted");
|
|
|
|
});
|
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-19 07:24:45 +00:00
|
|
|
if (!descriptionHTML) {
|
2024-02-16 14:37:04 +00:00
|
|
|
return (
|
|
|
|
<Loader>
|
|
|
|
<Loader.Item height="150px" />
|
|
|
|
</Loader>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disabled) {
|
|
|
|
return (
|
|
|
|
<RichReadOnlyEditor
|
|
|
|
value={descriptionHTML}
|
|
|
|
customClassName="!p-0 !pt-2 text-custom-text-200"
|
|
|
|
noBorder={disabled}
|
|
|
|
mentionHighlights={mentionHighlights}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RichTextEditor
|
|
|
|
cancelUploadImage={fileService.cancelUpload}
|
|
|
|
uploadFile={fileService.getUploadFileFunction(workspaceSlug)}
|
|
|
|
deleteFile={fileService.getDeleteImageFunction(workspaceId)}
|
|
|
|
restoreFile={fileService.getRestoreImageFunction(workspaceId)}
|
2024-02-19 10:13:57 +00:00
|
|
|
value={descriptionHTML}
|
2024-02-18 18:47:31 +00:00
|
|
|
initialValue={initialValue}
|
2024-02-16 14:37:04 +00:00
|
|
|
dragDropEnabled
|
|
|
|
customClassName="min-h-[150px] shadow-sm"
|
2024-03-06 13:09:14 +00:00
|
|
|
onChange={(description: any, description_html: string) => {
|
2024-02-16 14:37:04 +00:00
|
|
|
setIsSubmitting("submitting");
|
2024-02-19 07:24:45 +00:00
|
|
|
setDescriptionHTML(description_html === "" ? "<p></p>" : description_html);
|
2024-02-16 14:37:04 +00:00
|
|
|
}}
|
|
|
|
mentionSuggestions={mentionSuggestions}
|
|
|
|
mentionHighlights={mentionHighlights}
|
|
|
|
/>
|
|
|
|
);
|
2024-02-19 10:13:57 +00:00
|
|
|
};
|