2024-06-10 09:43:10 +00:00
|
|
|
"use client";
|
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
import React, { useRef } from "react";
|
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { useForm, Controller } from "react-hook-form";
|
2024-05-22 10:09:28 +00:00
|
|
|
// editor
|
2024-04-11 15:58:59 +00:00
|
|
|
import { EditorRefApi } from "@plane/lite-text-editor";
|
2024-05-22 10:09:28 +00:00
|
|
|
// ui
|
|
|
|
import { TOAST_TYPE, setToast } from "@plane/ui";
|
|
|
|
// editor components
|
2024-04-11 15:58:59 +00:00
|
|
|
import { LiteTextEditor } from "@/components/editor/lite-text-editor";
|
|
|
|
// hooks
|
2024-06-10 06:46:23 +00:00
|
|
|
import { useIssueDetails, usePublish, useUser } from "@/hooks/store";
|
2023-09-01 11:12:30 +00:00
|
|
|
// types
|
2024-04-11 15:58:59 +00:00
|
|
|
import { Comment } from "@/types/issue";
|
2023-09-01 11:12:30 +00:00
|
|
|
|
|
|
|
const defaultValues: Partial<Comment> = {
|
|
|
|
comment_html: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
2024-06-10 06:46:23 +00:00
|
|
|
anchor: string;
|
2023-09-01 11:12:30 +00:00
|
|
|
disabled?: boolean;
|
|
|
|
};
|
|
|
|
|
2024-05-14 08:56:54 +00:00
|
|
|
export const AddComment: React.FC<Props> = observer((props) => {
|
2024-06-10 06:46:23 +00:00
|
|
|
const { anchor } = props;
|
2024-04-11 15:58:59 +00:00
|
|
|
// refs
|
|
|
|
const editorRef = useRef<EditorRefApi>(null);
|
|
|
|
// store hooks
|
2024-05-14 08:56:54 +00:00
|
|
|
const { peekId: issueId, addIssueComment } = useIssueDetails();
|
2024-05-08 17:31:20 +00:00
|
|
|
const { data: currentUser } = useUser();
|
2024-06-10 06:46:23 +00:00
|
|
|
const { workspaceSlug, workspace: workspaceID } = usePublish(anchor);
|
2024-04-11 15:58:59 +00:00
|
|
|
// form info
|
2023-09-01 11:12:30 +00:00
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
|
|
|
watch,
|
|
|
|
formState: { isSubmitting },
|
|
|
|
reset,
|
|
|
|
} = useForm<Comment>({ defaultValues });
|
|
|
|
|
|
|
|
const onSubmit = async (formData: Comment) => {
|
2024-06-10 06:46:23 +00:00
|
|
|
if (!anchor || !issueId || isSubmitting || !formData.comment_html) return;
|
2023-09-01 11:12:30 +00:00
|
|
|
|
2024-06-10 06:46:23 +00:00
|
|
|
await addIssueComment(anchor, issueId, formData)
|
2023-09-01 11:12:30 +00:00
|
|
|
.then(() => {
|
|
|
|
reset(defaultValues);
|
|
|
|
editorRef.current?.clearEditor();
|
|
|
|
})
|
2024-04-11 15:58:59 +00:00
|
|
|
.catch(() =>
|
2024-05-22 10:09:28 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2023-09-01 11:12:30 +00:00
|
|
|
title: "Error!",
|
|
|
|
message: "Comment could not be posted. Please try again.",
|
2024-04-11 15:58:59 +00:00
|
|
|
})
|
|
|
|
);
|
2023-09-01 11:12:30 +00:00
|
|
|
};
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
// TODO: on click if he user is not logged in redirect to login page
|
2023-09-01 11:12:30 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="issue-comments-section">
|
|
|
|
<Controller
|
|
|
|
name="comment_html"
|
|
|
|
control={control}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
2024-04-11 15:58:59 +00:00
|
|
|
<LiteTextEditor
|
2024-05-08 17:31:20 +00:00
|
|
|
onEnterKeyPress={(e) => {
|
|
|
|
if (currentUser) handleSubmit(onSubmit)(e);
|
|
|
|
}}
|
2024-06-10 06:46:23 +00:00
|
|
|
workspaceId={workspaceID?.toString() ?? ""}
|
|
|
|
workspaceSlug={workspaceSlug?.toString() ?? ""}
|
2023-09-01 11:12:30 +00:00
|
|
|
ref={editorRef}
|
2024-04-11 15:58:59 +00:00
|
|
|
initialValue={
|
2023-09-01 11:12:30 +00:00
|
|
|
!value || value === "" || (typeof value === "object" && Object.keys(value).length === 0)
|
|
|
|
? watch("comment_html")
|
|
|
|
: value
|
|
|
|
}
|
2024-04-11 15:58:59 +00:00
|
|
|
onChange={(comment_json, comment_html) => onChange(comment_html)}
|
|
|
|
isSubmitting={isSubmitting}
|
2023-09-01 11:12:30 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|