2024-04-11 15:58:59 +00:00
|
|
|
import React, { useRef, useState } from "react";
|
2023-09-01 11:12:30 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2023-09-11 07:24:19 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2023-10-20 12:21:01 +00:00
|
|
|
import { Check, MessageSquare, MoreVertical, X } from "lucide-react";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { Menu, Transition } from "@headlessui/react";
|
2023-09-06 06:29:57 +00:00
|
|
|
// components
|
2024-04-11 15:58:59 +00:00
|
|
|
import { EditorRefApi } from "@plane/lite-text-editor";
|
|
|
|
import { LiteTextEditor, LiteTextReadOnlyEditor } from "@/components/editor";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { CommentReactions } from "@/components/issues/peek-overview";
|
2023-09-01 11:12:30 +00:00
|
|
|
// helpers
|
2024-03-19 14:38:35 +00:00
|
|
|
import { timeAgo } from "@/helpers/date-time.helper";
|
2024-05-08 17:31:20 +00:00
|
|
|
// hooks
|
2024-06-10 06:46:23 +00:00
|
|
|
import { useIssueDetails, usePublish, useUser } from "@/hooks/store";
|
2024-05-22 07:09:34 +00:00
|
|
|
import useIsInIframe from "@/hooks/use-is-in-iframe";
|
2024-04-11 15:58:59 +00:00
|
|
|
// types
|
|
|
|
import { Comment } from "@/types/issue";
|
2023-09-01 11:12:30 +00:00
|
|
|
|
|
|
|
type Props = {
|
2024-06-10 06:46:23 +00:00
|
|
|
anchor: string;
|
2023-09-01 11:12:30 +00:00
|
|
|
comment: Comment;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const CommentCard: React.FC<Props> = observer((props) => {
|
2024-06-10 06:46:23 +00:00
|
|
|
const { anchor, comment } = props;
|
2024-05-14 08:56:54 +00:00
|
|
|
// store hooks
|
|
|
|
const { peekId, deleteIssueComment, updateIssueComment } = 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-05-22 07:09:34 +00:00
|
|
|
const isInIframe = useIsInIframe();
|
2024-05-14 08:56:54 +00:00
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
// states
|
|
|
|
const [isEditing, setIsEditing] = useState(false);
|
2024-04-11 15:58:59 +00:00
|
|
|
// refs
|
|
|
|
const editorRef = useRef<EditorRefApi>(null);
|
|
|
|
const showEditorRef = useRef<EditorRefApi>(null);
|
|
|
|
// form info
|
2023-09-01 11:12:30 +00:00
|
|
|
const {
|
2023-09-11 07:24:19 +00:00
|
|
|
control,
|
2023-09-01 11:12:30 +00:00
|
|
|
formState: { isSubmitting },
|
|
|
|
handleSubmit,
|
|
|
|
} = useForm<any>({
|
|
|
|
defaultValues: { comment_html: comment.comment_html },
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleDelete = () => {
|
2024-06-10 06:46:23 +00:00
|
|
|
if (!anchor || !peekId) return;
|
|
|
|
deleteIssueComment(anchor, peekId, comment.id);
|
2023-09-01 11:12:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleCommentUpdate = async (formData: Comment) => {
|
2024-06-10 06:46:23 +00:00
|
|
|
if (!anchor || !peekId) return;
|
|
|
|
updateIssueComment(anchor, peekId, comment.id, formData);
|
2023-09-01 11:12:30 +00:00
|
|
|
setIsEditing(false);
|
2023-09-11 07:24:19 +00:00
|
|
|
editorRef.current?.setEditorValue(formData.comment_html);
|
|
|
|
showEditorRef.current?.setEditorValue(formData.comment_html);
|
2023-09-01 11:12:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="relative flex items-start space-x-3">
|
|
|
|
<div className="relative px-1">
|
|
|
|
{comment.actor_detail.avatar && comment.actor_detail.avatar !== "" ? (
|
|
|
|
// eslint-disable-next-line @next/next/no-img-element
|
|
|
|
<img
|
|
|
|
src={comment.actor_detail.avatar}
|
|
|
|
alt={
|
|
|
|
comment.actor_detail.is_bot ? comment.actor_detail.first_name + " Bot" : comment.actor_detail.display_name
|
|
|
|
}
|
|
|
|
height={30}
|
|
|
|
width={30}
|
|
|
|
className="grid h-7 w-7 place-items-center rounded-full border-2 border-custom-border-200"
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className={`grid h-7 w-7 place-items-center rounded-full border-2 border-white bg-gray-500 text-white`}>
|
|
|
|
{comment.actor_detail.is_bot
|
2023-09-01 15:22:12 +00:00
|
|
|
? comment?.actor_detail?.first_name?.charAt(0)
|
|
|
|
: comment?.actor_detail?.display_name?.charAt(0)}
|
2023-09-01 11:12:30 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<span className="absolute -bottom-0.5 -right-1 rounded-tl bg-custom-background-80 px-0.5 py-px">
|
2023-10-20 12:21:01 +00:00
|
|
|
<MessageSquare className="h-3 w-3 text-custom-text-200" aria-hidden="true" strokeWidth={2} />
|
2023-09-01 11:12:30 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
<div>
|
|
|
|
<div className="text-xs">
|
|
|
|
{comment.actor_detail.is_bot ? comment.actor_detail.first_name + " Bot" : comment.actor_detail.display_name}
|
|
|
|
</div>
|
|
|
|
<p className="mt-0.5 text-xs text-custom-text-200">
|
2023-09-06 06:29:57 +00:00
|
|
|
<>commented {timeAgo(comment.created_at)}</>
|
2023-09-01 11:12:30 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className="issue-comments-section p-0">
|
|
|
|
<form
|
|
|
|
onSubmit={handleSubmit(handleCommentUpdate)}
|
|
|
|
className={`flex-col gap-2 ${isEditing ? "flex" : "hidden"}`}
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="comment_html"
|
|
|
|
render={({ field: { onChange, value } }) => (
|
2024-04-11 15:58:59 +00:00
|
|
|
<LiteTextEditor
|
2024-06-10 06:46:23 +00:00
|
|
|
workspaceId={workspaceID?.toString() ?? ""}
|
|
|
|
workspaceSlug={workspaceSlug?.toString() ?? ""}
|
2023-10-13 06:35:49 +00:00
|
|
|
onEnterKeyPress={handleSubmit(handleCommentUpdate)}
|
2023-09-11 07:24:19 +00:00
|
|
|
ref={editorRef}
|
2024-04-11 15:58:59 +00:00
|
|
|
initialValue={value}
|
|
|
|
value={null}
|
|
|
|
onChange={(comment_json, comment_html) => onChange(comment_html)}
|
|
|
|
isSubmitting={isSubmitting}
|
|
|
|
showSubmitButton={false}
|
2023-09-01 11:12:30 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="flex gap-1 self-end">
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
disabled={isSubmitting}
|
|
|
|
className="group rounded border border-green-500 bg-green-500/20 p-2 shadow-md duration-300 hover:bg-green-500"
|
|
|
|
>
|
2023-10-20 12:21:01 +00:00
|
|
|
<Check className="h-3 w-3 text-green-500 duration-300 group-hover:text-white" strokeWidth={2} />
|
2023-09-01 11:12:30 +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)}
|
|
|
|
>
|
2023-10-20 12:21:01 +00:00
|
|
|
<X className="h-3 w-3 text-red-500 duration-300 group-hover:text-white" strokeWidth={2} />
|
2023-09-01 11:12:30 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<div className={`${isEditing ? "hidden" : ""}`}>
|
2024-04-11 15:58:59 +00:00
|
|
|
<LiteTextReadOnlyEditor ref={showEditorRef} initialValue={comment.comment_html} />
|
2024-06-10 06:46:23 +00:00
|
|
|
<CommentReactions anchor={anchor} commentId={comment.id} />
|
2023-09-01 11:12:30 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2024-05-22 07:09:34 +00:00
|
|
|
{!isInIframe && currentUser?.id === comment?.actor_detail?.id && (
|
2023-09-01 11:12:30 +00:00
|
|
|
<Menu as="div" className="relative w-min text-left">
|
|
|
|
<Menu.Button
|
|
|
|
type="button"
|
2023-10-20 12:21:01 +00:00
|
|
|
onClick={() => {}}
|
2023-12-10 10:18:10 +00:00
|
|
|
className="relative grid cursor-pointer place-items-center rounded p-1 text-custom-text-200 outline-none hover:bg-custom-background-80 hover:text-custom-text-100"
|
2023-09-01 11:12:30 +00:00
|
|
|
>
|
2023-10-20 12:21:01 +00:00
|
|
|
<MoreVertical className="h-4 w-4 text-custom-text-200 duration-300" strokeWidth={2} />
|
2023-09-01 11:12:30 +00:00
|
|
|
</Menu.Button>
|
|
|
|
|
|
|
|
<Transition
|
|
|
|
as={React.Fragment}
|
|
|
|
enter="transition ease-out duration-100"
|
|
|
|
enterFrom="transform opacity-0 scale-95"
|
|
|
|
enterTo="transform opacity-100 scale-100"
|
|
|
|
leave="transition ease-in duration-75"
|
|
|
|
leaveFrom="transform opacity-100 scale-100"
|
|
|
|
leaveTo="transform opacity-0 scale-95"
|
|
|
|
>
|
2023-12-10 10:18:10 +00:00
|
|
|
<Menu.Items className="absolute right-0 z-10 mt-1 max-h-36 min-w-[8rem] origin-top-right overflow-auto overflow-y-scroll whitespace-nowrap rounded-md border border-custom-border-300 bg-custom-background-90 p-1 text-xs shadow-lg focus:outline-none">
|
2023-09-01 11:12:30 +00:00
|
|
|
<Menu.Item>
|
|
|
|
{({ active }) => (
|
|
|
|
<div className="py-1">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
setIsEditing(true);
|
|
|
|
}}
|
2023-10-20 12:21:01 +00:00
|
|
|
className={`w-full select-none truncate rounded px-1 py-1.5 text-left text-custom-text-200 hover:bg-custom-background-80 ${
|
|
|
|
active ? "bg-custom-background-80" : ""
|
|
|
|
}`}
|
2023-09-01 11:12:30 +00:00
|
|
|
>
|
|
|
|
Edit
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Menu.Item>
|
|
|
|
<Menu.Item>
|
|
|
|
{({ active }) => (
|
|
|
|
<div className="py-1">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={handleDelete}
|
2023-10-20 12:21:01 +00:00
|
|
|
className={`w-full select-none truncate rounded px-1 py-1.5 text-left text-custom-text-200 hover:bg-custom-background-80 ${
|
|
|
|
active ? "bg-custom-background-80" : ""
|
|
|
|
}`}
|
2023-09-01 11:12:30 +00:00
|
|
|
>
|
|
|
|
Delete
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu.Items>
|
|
|
|
</Transition>
|
|
|
|
</Menu>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|