2024-01-23 07:58:58 +00:00
|
|
|
import { FC, useEffect, useRef, useState } from "react";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { Check, Globe2, Lock, Pencil, Trash2, X } from "lucide-react";
|
2024-04-11 15:58:59 +00:00
|
|
|
import { EditorReadOnlyRefApi, EditorRefApi } from "@plane/lite-text-editor";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { TIssueComment } from "@plane/types";
|
2024-04-11 15:58:59 +00:00
|
|
|
// ui
|
2024-03-06 13:09:14 +00:00
|
|
|
import { CustomMenu } from "@plane/ui";
|
2024-04-11 15:58:59 +00:00
|
|
|
// components
|
|
|
|
import { LiteTextEditor, LiteTextReadOnlyEditor } from "@/components/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 { useIssueDetail, useUser, useWorkspace } from "@/hooks/store";
|
2024-01-23 07:58:58 +00:00
|
|
|
// components
|
2024-03-06 13:09:14 +00:00
|
|
|
import { IssueCommentReaction } from "../../reactions/issue-comment";
|
2024-01-23 07:58:58 +00:00
|
|
|
import { TActivityOperations } from "../root";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { IssueCommentBlock } from "./comment-block";
|
2024-01-23 07:58:58 +00:00
|
|
|
|
|
|
|
type TIssueCommentCard = {
|
2024-04-11 15:58:59 +00:00
|
|
|
projectId: string;
|
2024-01-23 07:58:58 +00:00
|
|
|
workspaceSlug: string;
|
|
|
|
commentId: string;
|
|
|
|
activityOperations: TActivityOperations;
|
|
|
|
ends: "top" | "bottom" | undefined;
|
|
|
|
showAccessSpecifier?: boolean;
|
2024-04-03 12:32:07 +00:00
|
|
|
disabled?: boolean;
|
2024-01-23 07:58:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const IssueCommentCard: FC<TIssueCommentCard> = (props) => {
|
2024-04-11 15:58:59 +00:00
|
|
|
const {
|
|
|
|
workspaceSlug,
|
|
|
|
projectId,
|
|
|
|
commentId,
|
|
|
|
activityOperations,
|
|
|
|
ends,
|
|
|
|
showAccessSpecifier = false,
|
|
|
|
disabled = false,
|
|
|
|
} = props;
|
2024-01-23 07:58:58 +00:00
|
|
|
// hooks
|
|
|
|
const {
|
|
|
|
comment: { getCommentById },
|
|
|
|
} = useIssueDetail();
|
2024-04-29 06:42:33 +00:00
|
|
|
const { data: currentUser } = useUser();
|
2024-01-23 07:58:58 +00:00
|
|
|
// refs
|
2024-04-11 15:58:59 +00:00
|
|
|
const editorRef = useRef<EditorRefApi>(null);
|
|
|
|
const showEditorRef = useRef<EditorReadOnlyRefApi>(null);
|
2024-01-23 07:58:58 +00:00
|
|
|
// state
|
|
|
|
const [isEditing, setIsEditing] = useState(false);
|
|
|
|
|
|
|
|
const comment = getCommentById(commentId);
|
2024-01-24 13:26:19 +00:00
|
|
|
const workspaceStore = useWorkspace();
|
|
|
|
const workspaceId = workspaceStore.getWorkspaceBySlug(comment?.workspace_detail?.slug as string)?.id as string;
|
2024-01-23 07:58:58 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
formState: { isSubmitting },
|
|
|
|
handleSubmit,
|
|
|
|
setFocus,
|
|
|
|
watch,
|
|
|
|
setValue,
|
|
|
|
} = useForm<Partial<TIssueComment>>({
|
|
|
|
defaultValues: { comment_html: comment?.comment_html },
|
|
|
|
});
|
|
|
|
|
|
|
|
const onEnter = (formData: Partial<TIssueComment>) => {
|
|
|
|
if (isSubmitting || !comment) return;
|
|
|
|
setIsEditing(false);
|
|
|
|
|
|
|
|
activityOperations.updateComment(comment.id, formData);
|
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
editorRef.current?.setEditorValue(formData?.comment_html ?? "<p></p>");
|
|
|
|
showEditorRef.current?.setEditorValue(formData?.comment_html ?? "<p></p>");
|
2024-01-23 07:58:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
isEditing && setFocus("comment_html");
|
|
|
|
}, [isEditing, setFocus]);
|
|
|
|
|
2024-02-28 09:49:54 +00:00
|
|
|
const isEmpty =
|
|
|
|
watch("comment_html") === "" ||
|
|
|
|
watch("comment_html")?.trim() === "" ||
|
|
|
|
watch("comment_html") === "<p></p>" ||
|
|
|
|
isEmptyHtmlString(watch("comment_html") ?? "");
|
|
|
|
|
2024-01-23 07:58:58 +00:00
|
|
|
if (!comment || !currentUser) return <></>;
|
|
|
|
return (
|
|
|
|
<IssueCommentBlock
|
|
|
|
commentId={commentId}
|
|
|
|
quickActions={
|
|
|
|
<>
|
2024-04-03 12:32:07 +00:00
|
|
|
{!disabled && currentUser?.id === comment.actor && (
|
2024-01-23 07:58:58 +00:00
|
|
|
<CustomMenu ellipsis>
|
|
|
|
<CustomMenu.MenuItem onClick={() => setIsEditing(true)} className="flex items-center gap-1">
|
|
|
|
<Pencil className="h-3 w-3" />
|
|
|
|
Edit comment
|
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
{showAccessSpecifier && (
|
|
|
|
<>
|
|
|
|
{comment.access === "INTERNAL" ? (
|
|
|
|
<CustomMenu.MenuItem
|
2024-04-11 15:58:59 +00:00
|
|
|
onClick={() =>
|
|
|
|
activityOperations.updateComment(comment.id, { access: EIssueCommentAccessSpecifier.EXTERNAL })
|
|
|
|
}
|
2024-01-23 07:58:58 +00:00
|
|
|
className="flex items-center gap-1"
|
|
|
|
>
|
|
|
|
<Globe2 className="h-3 w-3" />
|
|
|
|
Switch to public comment
|
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
) : (
|
|
|
|
<CustomMenu.MenuItem
|
2024-04-11 15:58:59 +00:00
|
|
|
onClick={() =>
|
|
|
|
activityOperations.updateComment(comment.id, { access: EIssueCommentAccessSpecifier.INTERNAL })
|
|
|
|
}
|
2024-01-23 07:58:58 +00:00
|
|
|
className="flex items-center gap-1"
|
|
|
|
>
|
|
|
|
<Lock className="h-3 w-3" />
|
|
|
|
Switch to private comment
|
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<CustomMenu.MenuItem
|
|
|
|
onClick={() => activityOperations.removeComment(comment.id)}
|
|
|
|
className="flex items-center gap-1"
|
|
|
|
>
|
|
|
|
<Trash2 className="h-3 w-3" />
|
|
|
|
Delete comment
|
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
</CustomMenu>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
ends={ends}
|
|
|
|
>
|
|
|
|
<>
|
|
|
|
<form className={`flex-col gap-2 ${isEditing ? "flex" : "hidden"}`}>
|
2024-02-28 09:49:54 +00:00
|
|
|
<div
|
|
|
|
onKeyDown={(e) => {
|
2024-04-11 15:58:59 +00:00
|
|
|
if (e.key === "Enter" && !e.shiftKey && !isEmpty) handleSubmit(onEnter)(e);
|
2024-02-28 09:49:54 +00:00
|
|
|
}}
|
|
|
|
>
|
2024-04-11 15:58:59 +00:00
|
|
|
<LiteTextEditor
|
|
|
|
workspaceId={workspaceId}
|
|
|
|
projectId={projectId}
|
|
|
|
workspaceSlug={workspaceSlug}
|
2024-01-23 07:58:58 +00:00
|
|
|
ref={editorRef}
|
2024-04-11 15:58:59 +00:00
|
|
|
initialValue={watch("comment_html") ?? ""}
|
|
|
|
value={null}
|
|
|
|
onChange={(comment_json, comment_html) => setValue("comment_html", comment_html)}
|
|
|
|
showSubmitButton={false}
|
2024-01-23 07:58:58 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="flex gap-1 self-end">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={handleSubmit(onEnter)}
|
2024-02-28 09:49:54 +00:00
|
|
|
disabled={isSubmitting || isEmpty}
|
2024-02-28 11:18:26 +00:00
|
|
|
className={`group rounded border border-green-500 bg-green-500/20 p-2 shadow-md duration-300 ${
|
2024-03-06 13:09:14 +00:00
|
|
|
isEmpty ? "cursor-not-allowed bg-gray-200" : "hover:bg-green-500"
|
2024-02-28 11:18:26 +00:00
|
|
|
}`}
|
2024-01-23 07:58:58 +00:00
|
|
|
>
|
2024-02-28 11:18:26 +00:00
|
|
|
<Check
|
|
|
|
className={`h-3 w-3 text-green-500 duration-300 ${isEmpty ? "text-black" : "group-hover:text-white"}`}
|
|
|
|
/>
|
2024-01-23 07:58:58 +00:00
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="group rounded border border-red-500 bg-red-500/20 p-2 shadow-md duration-300 hover:bg-red-500"
|
|
|
|
onClick={() => setIsEditing(false)}
|
|
|
|
>
|
|
|
|
<X className="h-3 w-3 text-red-500 duration-300 group-hover:text-white" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<div className={`relative ${isEditing ? "hidden" : ""}`}>
|
|
|
|
{showAccessSpecifier && (
|
|
|
|
<div className="absolute right-2.5 top-2.5 z-[1] text-custom-text-300">
|
2024-04-11 15:58:59 +00:00
|
|
|
{comment.access === EIssueCommentAccessSpecifier.INTERNAL ? (
|
|
|
|
<Lock className="h-3 w-3" />
|
|
|
|
) : (
|
|
|
|
<Globe2 className="h-3 w-3" />
|
|
|
|
)}
|
2024-01-23 07:58:58 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2024-04-11 15:58:59 +00:00
|
|
|
<LiteTextReadOnlyEditor ref={showEditorRef} initialValue={comment.comment_html ?? ""} />
|
2024-01-23 07:58:58 +00:00
|
|
|
|
|
|
|
<IssueCommentReaction
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
projectId={comment?.project_detail?.id}
|
|
|
|
commentId={comment.id}
|
|
|
|
currentUser={currentUser}
|
2024-04-03 12:32:07 +00:00
|
|
|
disabled={disabled}
|
2024-01-23 07:58:58 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
</IssueCommentBlock>
|
|
|
|
);
|
|
|
|
};
|