2023-09-06 11:38:19 +00:00
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
// next
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
// react-hook-form
|
|
|
|
import { useForm, Controller } from "react-hook-form";
|
|
|
|
|
|
|
|
// hooks
|
|
|
|
import useProjectDetails from "hooks/use-project-details";
|
|
|
|
|
|
|
|
// components
|
2023-10-13 06:35:49 +00:00
|
|
|
import { LiteTextEditorWithRef } from "@plane/lite-text-editor";
|
2023-09-06 11:38:19 +00:00
|
|
|
// icons
|
|
|
|
import { Send } from "lucide-react";
|
|
|
|
|
|
|
|
// ui
|
2023-10-13 06:35:49 +00:00
|
|
|
import { PrimaryButton } from "components/ui";
|
2023-09-06 11:38:19 +00:00
|
|
|
|
|
|
|
// types
|
|
|
|
import type { IIssueComment } from "types";
|
2023-10-13 06:35:49 +00:00
|
|
|
import fileService from "services/file.service";
|
2023-09-06 11:38:19 +00:00
|
|
|
|
|
|
|
const defaultValues: Partial<IIssueComment> = {
|
|
|
|
access: "INTERNAL",
|
|
|
|
comment_html: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
disabled?: boolean;
|
|
|
|
onSubmit: (data: IIssueComment) => Promise<void>;
|
|
|
|
};
|
|
|
|
|
2023-10-13 06:35:49 +00:00
|
|
|
type commentAccessType = {
|
|
|
|
icon: string;
|
|
|
|
key: string;
|
|
|
|
label: "Private" | "Public";
|
|
|
|
}
|
|
|
|
|
|
|
|
const commentAccess: commentAccessType[] = [
|
2023-09-06 11:38:19 +00:00
|
|
|
{
|
|
|
|
icon: "lock",
|
|
|
|
key: "INTERNAL",
|
|
|
|
label: "Private",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: "public",
|
|
|
|
key: "EXTERNAL",
|
|
|
|
label: "Public",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
export const AddComment: React.FC<Props> = ({ disabled = false, onSubmit }) => {
|
|
|
|
const editorRef = React.useRef<any>(null);
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
|
|
|
|
const { projectDetails } = useProjectDetails();
|
|
|
|
|
2023-10-13 06:35:49 +00:00
|
|
|
const showAccessSpecifier = projectDetails?.is_deployed || false;
|
2023-09-06 11:38:19 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
formState: { isSubmitting },
|
|
|
|
handleSubmit,
|
|
|
|
reset,
|
|
|
|
} = useForm<IIssueComment>({ defaultValues });
|
|
|
|
|
|
|
|
const handleAddComment = async (formData: IIssueComment) => {
|
|
|
|
if (!formData.comment_html || isSubmitting) return;
|
|
|
|
|
|
|
|
await onSubmit(formData).then(() => {
|
|
|
|
reset(defaultValues);
|
|
|
|
editorRef.current?.clearEditor();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form className="w-full flex gap-x-2" onSubmit={handleSubmit(handleAddComment)}>
|
|
|
|
<div className="relative flex-grow">
|
2023-10-13 06:35:49 +00:00
|
|
|
<Controller
|
|
|
|
name="access"
|
|
|
|
control={control}
|
|
|
|
render={({ field: { onChange: onAccessChange, value: accessValue } }) => (
|
2023-09-06 11:38:19 +00:00
|
|
|
<Controller
|
2023-10-13 06:35:49 +00:00
|
|
|
name="comment_html"
|
2023-09-06 11:38:19 +00:00
|
|
|
control={control}
|
2023-10-13 06:35:49 +00:00
|
|
|
render={({ field: { onChange: onCommentChange, value: commentValue } }) => (
|
|
|
|
<LiteTextEditorWithRef
|
|
|
|
uploadFile={fileService.getUploadFileFunction(workspaceSlug as string)}
|
|
|
|
deleteFile={fileService.deleteImage}
|
|
|
|
ref={editorRef}
|
|
|
|
value={!commentValue || commentValue === "" ? "<p></p>" : commentValue}
|
|
|
|
customClassName="p-3 min-h-[100px] shadow-sm"
|
|
|
|
debouncedUpdatesEnabled={false}
|
|
|
|
onChange={(comment_json: Object, comment_html: string) => onCommentChange(comment_html)}
|
|
|
|
commentAccessSpecifier={{ accessValue, onAccessChange, showAccessSpecifier, commentAccess }}
|
|
|
|
/>
|
2023-09-06 11:38:19 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="inline">
|
2023-09-07 13:12:24 +00:00
|
|
|
<PrimaryButton
|
|
|
|
type="submit"
|
|
|
|
disabled={isSubmitting || disabled}
|
|
|
|
className="mt-2 w-10 h-10 flex items-center justify-center"
|
|
|
|
>
|
2023-09-06 11:38:19 +00:00
|
|
|
<Send className="w-4 h-4" />
|
|
|
|
</PrimaryButton>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|