plane/web/components/issues/issue-detail/issue-activity/comments/comment-create-update.tsx

81 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-01-22 06:47:55 +00:00
import { FC, useRef } from "react";
import { useForm, Controller } from "react-hook-form";
// components
import { LiteTextEditorWithRef } from "@plane/lite-text-editor";
import { Button } from "@plane/ui";
2024-01-22 06:47:55 +00:00
// services
import { FileService } from "services/file.service";
// types
import { TActivityOperations } from "../root";
2024-01-22 06:47:55 +00:00
import { TIssueComment } from "@plane/types";
const fileService = new FileService();
type TIssueCommentCreateUpdate = {
2024-01-22 06:47:55 +00:00
workspaceSlug: string;
activityOperations: TActivityOperations;
2024-01-22 06:47:55 +00:00
disabled: boolean;
};
export const IssueCommentCreateUpdate: FC<TIssueCommentCreateUpdate> = (props) => {
2024-01-22 06:47:55 +00:00
const { workspaceSlug, activityOperations, disabled } = props;
// refs
const editorRef = useRef<any>(null);
// react hook form
const {
handleSubmit,
control,
watch,
formState: { isSubmitting },
reset,
2024-01-22 06:47:55 +00:00
} = useForm<Partial<TIssueComment>>({ defaultValues: { comment_html: "<p></p>" } });
const onSubmit = async (formData: Partial<TIssueComment>) => {
await activityOperations.createComment(formData).finally(() => {
reset({ comment_html: "" });
editorRef.current?.clearEditor();
});
};
return (
<div>
2024-01-22 06:47:55 +00:00
<Controller
name="comment_html"
control={control}
render={({ field: { value, onChange } }) => (
<LiteTextEditorWithRef
onEnterKeyPress={(e) => {
2024-01-22 06:47:55 +00:00
handleSubmit(onSubmit)(e);
}}
cancelUploadImage={fileService.cancelUpload}
2024-01-22 06:47:55 +00:00
uploadFile={fileService.getUploadFileFunction(workspaceSlug as string)}
deleteFile={fileService.deleteImage}
restoreFile={fileService.restoreImage}
ref={editorRef}
2024-01-22 06:47:55 +00:00
value={!value ? "<p></p>" : value}
customClassName="p-2"
editorContentCustomClassNames="min-h-[35px]"
debouncedUpdatesEnabled={false}
onChange={(comment_json: Object, comment_html: string) => {
onChange(comment_html);
}}
submitButton={
<Button
disabled={isSubmitting || disabled}
variant="primary"
type="submit"
className="!px-2.5 !py-1.5 !text-xs"
onClick={(e) => {
2024-01-22 06:47:55 +00:00
handleSubmit(onSubmit)(e);
}}
>
{isSubmitting ? "Adding..." : "Comment"}
</Button>
}
/>
)}
2024-01-22 06:47:55 +00:00
/>
</div>
);
};