2023-09-06 06:29:57 +00:00
|
|
|
import React from "react";
|
|
|
|
import { observer } from "mobx-react-lite";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-12-05 10:37:25 +00:00
|
|
|
import { Tooltip } from "@plane/ui";
|
2024-05-08 17:31:20 +00:00
|
|
|
// ui
|
2024-03-19 14:38:35 +00:00
|
|
|
import { ReactionSelector } from "@/components/ui";
|
2023-09-06 06:29:57 +00:00
|
|
|
// helpers
|
2024-03-19 14:38:35 +00:00
|
|
|
import { groupReactions, renderEmoji } from "@/helpers/emoji.helper";
|
2024-05-08 17:31:20 +00:00
|
|
|
// hooks
|
|
|
|
import { useMobxStore, useUser } from "@/hooks/store";
|
2023-09-06 06:29:57 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
commentId: string;
|
|
|
|
projectId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const CommentReactions: React.FC<Props> = observer((props) => {
|
|
|
|
const { commentId, projectId } = props;
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspace_slug } = router.query;
|
2024-05-08 17:31:20 +00:00
|
|
|
// hooks
|
|
|
|
const { issueDetails: issueDetailsStore } = useMobxStore();
|
|
|
|
const { data: user } = useUser();
|
2023-09-06 06:29:57 +00:00
|
|
|
|
|
|
|
const peekId = issueDetailsStore.peekId;
|
|
|
|
const commentReactions = peekId
|
|
|
|
? issueDetailsStore.details[peekId].comments.find((c) => c.id === commentId)?.comment_reactions
|
|
|
|
: [];
|
|
|
|
const groupedReactions = peekId ? groupReactions(commentReactions ?? [], "reaction") : {};
|
|
|
|
|
|
|
|
const userReactions = commentReactions?.filter((r) => r.actor_detail.id === user?.id);
|
|
|
|
|
|
|
|
const handleAddReaction = (reactionHex: string) => {
|
|
|
|
if (!workspace_slug || !projectId || !peekId) return;
|
|
|
|
|
|
|
|
issueDetailsStore.addCommentReaction(
|
|
|
|
workspace_slug.toString(),
|
|
|
|
projectId.toString(),
|
|
|
|
peekId,
|
|
|
|
commentId,
|
|
|
|
reactionHex
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemoveReaction = (reactionHex: string) => {
|
|
|
|
if (!workspace_slug || !projectId || !peekId) return;
|
|
|
|
|
|
|
|
issueDetailsStore.removeCommentReaction(
|
|
|
|
workspace_slug.toString(),
|
|
|
|
projectId.toString(),
|
|
|
|
peekId,
|
|
|
|
commentId,
|
|
|
|
reactionHex
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleReactionClick = (reactionHex: string) => {
|
|
|
|
const userReaction = userReactions?.find((r) => r.actor_detail.id === user?.id && r.reaction === reactionHex);
|
|
|
|
|
|
|
|
if (userReaction) handleRemoveReaction(reactionHex);
|
|
|
|
else handleAddReaction(reactionHex);
|
|
|
|
};
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
// TODO: on onclick redirect to login page if the user is not logged in
|
2023-09-06 06:29:57 +00:00
|
|
|
return (
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="mt-2 flex items-center gap-1.5">
|
2023-09-06 06:29:57 +00:00
|
|
|
<ReactionSelector
|
|
|
|
onSelect={(value) => {
|
2024-05-08 17:31:20 +00:00
|
|
|
if (user) handleReactionClick(value);
|
|
|
|
// userStore.requiredLogin(() => {});
|
2023-09-06 06:29:57 +00:00
|
|
|
}}
|
|
|
|
position="top"
|
|
|
|
selected={userReactions?.map((r) => r.reaction)}
|
|
|
|
size="md"
|
|
|
|
/>
|
|
|
|
|
2023-09-06 06:34:18 +00:00
|
|
|
{Object.keys(groupedReactions || {}).map((reaction) => {
|
|
|
|
const reactions = groupedReactions?.[reaction] ?? [];
|
|
|
|
const REACTIONS_LIMIT = 1000;
|
|
|
|
|
|
|
|
if (reactions.length > 0)
|
|
|
|
return (
|
|
|
|
<Tooltip
|
2023-09-06 06:29:57 +00:00
|
|
|
key={reaction}
|
2023-09-06 06:34:18 +00:00
|
|
|
tooltipContent={
|
|
|
|
<div>
|
|
|
|
{reactions
|
|
|
|
.map((r) => r.actor_detail.display_name)
|
|
|
|
.splice(0, REACTIONS_LIMIT)
|
|
|
|
.join(", ")}
|
|
|
|
{reactions.length > REACTIONS_LIMIT && " and " + (reactions.length - REACTIONS_LIMIT) + " more"}
|
|
|
|
</div>
|
|
|
|
}
|
2023-09-06 06:29:57 +00:00
|
|
|
>
|
2023-09-06 06:34:18 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
2024-05-08 17:31:20 +00:00
|
|
|
if (user) handleReactionClick(reaction);
|
|
|
|
// userStore.requiredLogin(() => {});
|
2023-09-06 06:34:18 +00:00
|
|
|
}}
|
2023-12-10 10:18:10 +00:00
|
|
|
className={`flex h-full items-center gap-1 rounded-md px-2 py-1 text-sm text-custom-text-100 ${
|
2024-05-08 17:31:20 +00:00
|
|
|
commentReactions?.some((r) => r.actor_detail.id === user?.id && r.reaction === reaction)
|
2023-09-06 06:34:18 +00:00
|
|
|
? "bg-custom-primary-100/10"
|
|
|
|
: "bg-custom-background-80"
|
|
|
|
}`}
|
2023-09-06 06:29:57 +00:00
|
|
|
>
|
2023-09-06 06:34:18 +00:00
|
|
|
<span>{renderEmoji(reaction)}</span>
|
|
|
|
<span
|
|
|
|
className={
|
2024-05-08 17:31:20 +00:00
|
|
|
commentReactions?.some((r) => r.actor_detail.id === user?.id && r.reaction === reaction)
|
2023-09-06 06:34:18 +00:00
|
|
|
? "text-custom-primary-100"
|
|
|
|
: ""
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{groupedReactions?.[reaction].length}{" "}
|
|
|
|
</span>
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
})}
|
2023-09-06 06:29:57 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|