2023-09-01 11:12:30 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
// lib
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
|
|
|
// helpers
|
|
|
|
import { groupReactions, renderEmoji } from "helpers/emoji.helper";
|
|
|
|
// components
|
2023-12-05 10:37:25 +00:00
|
|
|
import { ReactionSelector } from "components/ui";
|
|
|
|
import { Tooltip } from "@plane/ui";
|
2023-09-01 11:12:30 +00:00
|
|
|
|
|
|
|
export const IssueEmojiReactions: React.FC = observer(() => {
|
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspace_slug, project_slug } = router.query;
|
|
|
|
// store
|
|
|
|
const { user: userStore, issueDetails: issueDetailsStore } = useMobxStore();
|
|
|
|
|
|
|
|
const user = userStore?.currentUser;
|
|
|
|
const issueId = issueDetailsStore.peekId;
|
|
|
|
const reactions = issueId ? issueDetailsStore.details[issueId]?.reactions || [] : [];
|
|
|
|
const groupedReactions = groupReactions(reactions, "reaction");
|
|
|
|
|
2023-09-06 06:29:57 +00:00
|
|
|
const userReactions = reactions?.filter((r) => r.actor_detail.id === user?.id);
|
|
|
|
|
|
|
|
const handleAddReaction = (reactionHex: string) => {
|
2023-09-01 11:12:30 +00:00
|
|
|
if (!workspace_slug || !project_slug || !issueId) return;
|
2023-09-06 06:29:57 +00:00
|
|
|
|
2023-09-01 15:08:53 +00:00
|
|
|
issueDetailsStore.addIssueReaction(workspace_slug.toString(), project_slug.toString(), issueId, reactionHex);
|
2023-09-01 11:12:30 +00:00
|
|
|
};
|
|
|
|
|
2023-09-06 06:29:57 +00:00
|
|
|
const handleRemoveReaction = (reactionHex: string) => {
|
2023-09-01 11:12:30 +00:00
|
|
|
if (!workspace_slug || !project_slug || !issueId) return;
|
2023-09-06 06:29:57 +00:00
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
issueDetailsStore.removeIssueReaction(workspace_slug.toString(), project_slug.toString(), issueId, reactionHex);
|
|
|
|
};
|
|
|
|
|
2023-09-06 06:29:57 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (user) return;
|
|
|
|
userStore.fetchCurrentUser();
|
|
|
|
}, [user, userStore]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ReactionSelector
|
|
|
|
onSelect={(value) => {
|
|
|
|
userStore.requiredLogin(() => {
|
2023-09-06 06:29:57 +00:00
|
|
|
handleReactionClick(value);
|
2023-09-01 11:12:30 +00:00
|
|
|
});
|
|
|
|
}}
|
2023-09-06 06:29:57 +00:00
|
|
|
selected={userReactions?.map((r) => r.reaction)}
|
2023-09-01 11:12:30 +00:00
|
|
|
size="md"
|
|
|
|
/>
|
|
|
|
<div className="flex items-center gap-2 flex-wrap">
|
|
|
|
{Object.keys(groupedReactions || {}).map((reaction) => {
|
|
|
|
const reactions = groupedReactions?.[reaction] ?? [];
|
|
|
|
const REACTIONS_LIMIT = 1000;
|
|
|
|
|
|
|
|
if (reactions.length > 0)
|
|
|
|
return (
|
|
|
|
<Tooltip
|
2023-09-01 15:08:53 +00:00
|
|
|
key={reaction}
|
2023-09-01 11:12:30 +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>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
userStore.requiredLogin(() => {
|
|
|
|
handleReactionClick(reaction);
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
className={`flex items-center gap-1 text-custom-text-100 text-sm h-full px-2 py-1 rounded-md ${
|
|
|
|
reactions?.some((r) => r.actor_detail.id === user?.id && r.reaction === reaction)
|
|
|
|
? "bg-custom-primary-100/10"
|
|
|
|
: "bg-custom-background-80"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<span>{renderEmoji(reaction)}</span>
|
|
|
|
<span
|
|
|
|
className={
|
|
|
|
reactions?.some((r) => r.actor_detail.id === user?.id && r.reaction === reaction)
|
|
|
|
? "text-custom-primary-100"
|
|
|
|
: ""
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{groupedReactions?.[reaction].length}{" "}
|
|
|
|
</span>
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|