2024-01-23 07:58:58 +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";
|
|
|
|
// services
|
|
|
|
import { FileService } from "services/file.service";
|
|
|
|
// types
|
|
|
|
import { TActivityOperations } from "../root";
|
|
|
|
import { TIssueComment } from "@plane/types";
|
|
|
|
// icons
|
|
|
|
import { Globe2, Lock } from "lucide-react";
|
2024-01-31 12:36:12 +00:00
|
|
|
import { useMention, useWorkspace } from "hooks/store";
|
2024-01-23 07:58:58 +00:00
|
|
|
|
|
|
|
const fileService = new FileService();
|
|
|
|
|
|
|
|
type TIssueCommentCreate = {
|
|
|
|
workspaceSlug: string;
|
|
|
|
activityOperations: TActivityOperations;
|
|
|
|
showAccessSpecifier?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type commentAccessType = {
|
|
|
|
icon: any;
|
|
|
|
key: string;
|
|
|
|
label: "Private" | "Public";
|
|
|
|
};
|
|
|
|
const commentAccess: commentAccessType[] = [
|
|
|
|
{
|
|
|
|
icon: Lock,
|
|
|
|
key: "INTERNAL",
|
|
|
|
label: "Private",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: Globe2,
|
|
|
|
key: "EXTERNAL",
|
|
|
|
label: "Public",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
export const IssueCommentCreate: FC<TIssueCommentCreate> = (props) => {
|
2024-01-30 14:43:28 +00:00
|
|
|
const { workspaceSlug, activityOperations, showAccessSpecifier = false } = props;
|
2024-01-24 13:26:19 +00:00
|
|
|
const workspaceStore = useWorkspace();
|
|
|
|
const workspaceId = workspaceStore.getWorkspaceBySlug(workspaceSlug as string)?.id as string;
|
|
|
|
|
2024-01-31 12:36:12 +00:00
|
|
|
const { mentionHighlights, mentionSuggestions } = useMention();
|
|
|
|
|
2024-01-23 07:58:58 +00:00
|
|
|
// refs
|
|
|
|
const editorRef = useRef<any>(null);
|
|
|
|
// react hook form
|
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
|
|
|
formState: { isSubmitting },
|
|
|
|
reset,
|
2024-02-22 14:31:42 +00:00
|
|
|
watch,
|
|
|
|
} = useForm<Partial<TIssueComment>>({ defaultValues: { comment_html: "" } });
|
2024-01-23 07:58:58 +00:00
|
|
|
|
|
|
|
const onSubmit = async (formData: Partial<TIssueComment>) => {
|
|
|
|
await activityOperations.createComment(formData).finally(() => {
|
|
|
|
reset({ comment_html: "" });
|
|
|
|
editorRef.current?.clearEditor();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-01-31 12:36:12 +00:00
|
|
|
<div
|
|
|
|
// onKeyDown={(e) => {
|
|
|
|
// if (e.key === "Enter" && !e.shiftKey) {
|
|
|
|
// e.preventDefault();
|
|
|
|
// // handleSubmit(onSubmit)(e);
|
|
|
|
// }
|
|
|
|
// }}
|
|
|
|
>
|
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 } }) => (
|
|
|
|
<LiteTextEditorWithRef
|
|
|
|
onEnterKeyPress={(e) => {
|
|
|
|
handleSubmit(onSubmit)(e);
|
|
|
|
}}
|
|
|
|
cancelUploadImage={fileService.cancelUpload}
|
|
|
|
uploadFile={fileService.getUploadFileFunction(workspaceSlug as string)}
|
2024-01-24 13:26:19 +00:00
|
|
|
deleteFile={fileService.getDeleteImageFunction(workspaceId)}
|
|
|
|
restoreFile={fileService.getRestoreImageFunction(workspaceId)}
|
2024-01-23 07:58:58 +00:00
|
|
|
ref={editorRef}
|
2024-02-22 14:31:42 +00:00
|
|
|
value={value ?? ""}
|
2024-01-23 07:58:58 +00:00
|
|
|
customClassName="p-2"
|
|
|
|
editorContentCustomClassNames="min-h-[35px]"
|
|
|
|
debouncedUpdatesEnabled={false}
|
|
|
|
onChange={(comment_json: Object, comment_html: string) => {
|
|
|
|
onChange(comment_html);
|
|
|
|
}}
|
2024-01-31 12:36:12 +00:00
|
|
|
mentionSuggestions={mentionSuggestions}
|
|
|
|
mentionHighlights={mentionHighlights}
|
2024-01-23 07:58:58 +00:00
|
|
|
commentAccessSpecifier={
|
|
|
|
showAccessSpecifier
|
|
|
|
? { accessValue: accessValue ?? "INTERNAL", onAccessChange, showAccessSpecifier, commentAccess }
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
submitButton={
|
|
|
|
<Button
|
2024-02-22 14:31:42 +00:00
|
|
|
disabled={isSubmitting || watch("comment_html") === ""}
|
2024-01-23 07:58:58 +00:00
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
className="!px-2.5 !py-1.5 !text-xs"
|
|
|
|
onClick={(e) => {
|
|
|
|
handleSubmit(onSubmit)(e);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{isSubmitting ? "Adding..." : "Comment"}
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|