2024-01-23 07:58:58 +00:00
|
|
|
import { FC, useRef } from "react";
|
|
|
|
import { useForm, Controller } from "react-hook-form";
|
2024-04-11 15:58:59 +00:00
|
|
|
// types
|
2024-03-19 14:38:35 +00:00
|
|
|
import { TIssueComment } from "@plane/types";
|
|
|
|
// components
|
2024-04-11 15:58:59 +00:00
|
|
|
import { LiteTextEditor } from "@/components/editor/lite-text-editor/lite-text-editor";
|
|
|
|
// constants
|
|
|
|
import { EIssueCommentAccessSpecifier } from "@/constants/issue";
|
|
|
|
// helpers
|
2024-03-19 14:38:35 +00:00
|
|
|
import { isEmptyHtmlString } from "@/helpers/string.helper";
|
2024-04-11 15:58:59 +00:00
|
|
|
// hooks
|
|
|
|
import { useWorkspace } from "@/hooks/store";
|
|
|
|
// editor
|
2024-03-06 13:09:14 +00:00
|
|
|
import { TActivityOperations } from "../root";
|
2024-01-23 07:58:58 +00:00
|
|
|
|
|
|
|
type TIssueCommentCreate = {
|
2024-04-11 15:58:59 +00:00
|
|
|
projectId: string;
|
2024-01-23 07:58:58 +00:00
|
|
|
workspaceSlug: string;
|
|
|
|
activityOperations: TActivityOperations;
|
|
|
|
showAccessSpecifier?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const IssueCommentCreate: FC<TIssueCommentCreate> = (props) => {
|
2024-04-11 15:58:59 +00:00
|
|
|
const { workspaceSlug, projectId, activityOperations, showAccessSpecifier = false } = props;
|
2024-01-23 07:58:58 +00:00
|
|
|
// refs
|
|
|
|
const editorRef = useRef<any>(null);
|
2024-04-11 15:58:59 +00:00
|
|
|
// store hooks
|
|
|
|
const workspaceStore = useWorkspace();
|
|
|
|
// derived values
|
|
|
|
const workspaceId = workspaceStore.getWorkspaceBySlug(workspaceSlug as string)?.id as string;
|
|
|
|
// form info
|
2024-01-23 07:58:58 +00:00
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
2024-02-26 10:41:35 +00:00
|
|
|
watch,
|
2024-01-23 07:58:58 +00:00
|
|
|
formState: { isSubmitting },
|
|
|
|
reset,
|
2024-04-11 15:58:59 +00:00
|
|
|
} = useForm<Partial<TIssueComment>>({
|
|
|
|
defaultValues: {
|
|
|
|
comment_html: "<p></p>",
|
|
|
|
},
|
|
|
|
});
|
2024-01-23 07:58:58 +00:00
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
const onSubmit = async (formData: Partial<TIssueComment>) =>
|
2024-01-23 07:58:58 +00:00
|
|
|
await activityOperations.createComment(formData).finally(() => {
|
2024-04-11 15:58:59 +00:00
|
|
|
reset({
|
|
|
|
comment_html: "<p></p>",
|
|
|
|
});
|
2024-01-23 07:58:58 +00:00
|
|
|
editorRef.current?.clearEditor();
|
|
|
|
});
|
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
const commentHTML = watch("comment_html");
|
2024-04-16 13:20:26 +00:00
|
|
|
|
|
|
|
const isEmpty =
|
|
|
|
commentHTML?.trim() === "" ||
|
|
|
|
commentHTML === "<p></p>" ||
|
|
|
|
(isEmptyHtmlString(commentHTML ?? "") && !commentHTML?.includes("mention-component"));
|
2024-02-26 10:41:35 +00:00
|
|
|
|
2024-01-23 07:58:58 +00:00
|
|
|
return (
|
2024-01-31 12:36:12 +00:00
|
|
|
<div
|
2024-02-26 10:41:35 +00:00
|
|
|
onKeyDown={(e) => {
|
2024-04-11 15:58:59 +00:00
|
|
|
if (e.key === "Enter" && !e.shiftKey && !isEmpty && !isSubmitting) handleSubmit(onSubmit)(e);
|
2024-02-26 10:41:35 +00:00
|
|
|
}}
|
2024-01-31 12:36:12 +00:00
|
|
|
>
|
2024-01-23 07:58:58 +00:00
|
|
|
<Controller
|
|
|
|
name="access"
|
|
|
|
control={control}
|
|
|
|
render={({ field: { onChange: onAccessChange, value: accessValue } }) => (
|
|
|
|
<Controller
|
|
|
|
name="comment_html"
|
|
|
|
control={control}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
2024-04-11 15:58:59 +00:00
|
|
|
<LiteTextEditor
|
|
|
|
workspaceId={workspaceId}
|
|
|
|
projectId={projectId}
|
|
|
|
workspaceSlug={workspaceSlug}
|
2024-04-16 13:20:26 +00:00
|
|
|
onEnterKeyPress={(e) => {
|
|
|
|
if (!isEmpty && !isSubmitting) handleSubmit(onSubmit)(e);
|
|
|
|
}}
|
2024-01-23 07:58:58 +00:00
|
|
|
ref={editorRef}
|
2024-04-11 15:58:59 +00:00
|
|
|
initialValue={value ?? "<p></p>"}
|
|
|
|
containerClassName="min-h-[35px]"
|
|
|
|
onChange={(comment_json, comment_html) => onChange(comment_html)}
|
|
|
|
accessSpecifier={accessValue ?? EIssueCommentAccessSpecifier.INTERNAL}
|
|
|
|
handleAccessChange={onAccessChange}
|
|
|
|
showAccessSpecifier={showAccessSpecifier}
|
|
|
|
isSubmitting={isSubmitting}
|
2024-01-23 07:58:58 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|