import { FC, useEffect, useRef, useState } from "react"; import { useForm } from "react-hook-form"; import { Check, Globe2, Lock, Pencil, Trash2, X } from "lucide-react"; import { EditorReadOnlyRefApi, EditorRefApi } from "@plane/lite-text-editor"; import { TIssueComment } from "@plane/types"; // ui import { CustomMenu } from "@plane/ui"; // components import { LiteTextEditor, LiteTextReadOnlyEditor } from "@/components/editor"; // constants import { EIssueCommentAccessSpecifier } from "@/constants/issue"; // helpers import { isEmptyHtmlString } from "@/helpers/string.helper"; // hooks import { useIssueDetail, useUser, useWorkspace } from "@/hooks/store"; // components import { IssueCommentReaction } from "../../reactions/issue-comment"; import { TActivityOperations } from "../root"; import { IssueCommentBlock } from "./comment-block"; type TIssueCommentCard = { projectId: string; workspaceSlug: string; commentId: string; activityOperations: TActivityOperations; ends: "top" | "bottom" | undefined; showAccessSpecifier?: boolean; disabled?: boolean; }; export const IssueCommentCard: FC = (props) => { const { workspaceSlug, projectId, commentId, activityOperations, ends, showAccessSpecifier = false, disabled = false, } = props; // hooks const { comment: { getCommentById }, } = useIssueDetail(); const { currentUser } = useUser(); // refs const editorRef = useRef(null); const showEditorRef = useRef(null); // state const [isEditing, setIsEditing] = useState(false); const comment = getCommentById(commentId); const workspaceStore = useWorkspace(); const workspaceId = workspaceStore.getWorkspaceBySlug(comment?.workspace_detail?.slug as string)?.id as string; const { formState: { isSubmitting }, handleSubmit, setFocus, watch, setValue, } = useForm>({ defaultValues: { comment_html: comment?.comment_html }, }); const onEnter = (formData: Partial) => { if (isSubmitting || !comment) return; setIsEditing(false); activityOperations.updateComment(comment.id, formData); editorRef.current?.setEditorValue(formData?.comment_html ?? "

"); showEditorRef.current?.setEditorValue(formData?.comment_html ?? "

"); }; useEffect(() => { isEditing && setFocus("comment_html"); }, [isEditing, setFocus]); const isEmpty = watch("comment_html") === "" || watch("comment_html")?.trim() === "" || watch("comment_html") === "

" || isEmptyHtmlString(watch("comment_html") ?? ""); if (!comment || !currentUser) return <>; return ( {!disabled && currentUser?.id === comment.actor && ( setIsEditing(true)} className="flex items-center gap-1"> Edit comment {showAccessSpecifier && ( <> {comment.access === "INTERNAL" ? ( activityOperations.updateComment(comment.id, { access: EIssueCommentAccessSpecifier.EXTERNAL }) } className="flex items-center gap-1" > Switch to public comment ) : ( activityOperations.updateComment(comment.id, { access: EIssueCommentAccessSpecifier.INTERNAL }) } className="flex items-center gap-1" > Switch to private comment )} )} activityOperations.removeComment(comment.id)} className="flex items-center gap-1" > Delete comment )} } ends={ends} > <>
{ if (e.key === "Enter" && !e.shiftKey && !isEmpty) handleSubmit(onEnter)(e); }} > setValue("comment_html", comment_html)} showSubmitButton={false} />
{showAccessSpecifier && (
{comment.access === EIssueCommentAccessSpecifier.INTERNAL ? ( ) : ( )}
)}
); };