2023-07-27 13:25:03 +00:00
|
|
|
// hooks
|
|
|
|
import useIssueReaction from "hooks/use-issue-reaction";
|
|
|
|
// components
|
|
|
|
import { ReactionSelector } from "components/core";
|
|
|
|
// string helpers
|
|
|
|
import { renderEmoji } from "helpers/emoji.helper";
|
2023-12-07 12:43:27 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
2023-07-27 13:25:03 +00:00
|
|
|
|
|
|
|
// types
|
|
|
|
type Props = {
|
2023-12-07 12:43:27 +00:00
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
2023-07-27 13:25:03 +00:00
|
|
|
};
|
|
|
|
|
2023-12-07 12:43:27 +00:00
|
|
|
export const IssueReaction: React.FC<Props> = observer((props) => {
|
2023-07-27 13:25:03 +00:00
|
|
|
const { workspaceSlug, projectId, issueId } = props;
|
|
|
|
|
2023-12-07 12:43:27 +00:00
|
|
|
const {
|
|
|
|
user: { currentUser },
|
|
|
|
} = useMobxStore();
|
2023-07-27 13:25:03 +00:00
|
|
|
|
2023-11-08 15:01:46 +00:00
|
|
|
const { reactions, groupedReactions, handleReactionCreate, handleReactionDelete } = useIssueReaction(
|
|
|
|
workspaceSlug,
|
|
|
|
projectId,
|
|
|
|
issueId
|
|
|
|
);
|
2023-07-27 13:25:03 +00:00
|
|
|
|
|
|
|
const handleReactionClick = (reaction: string) => {
|
|
|
|
if (!workspaceSlug || !projectId || !issueId) return;
|
|
|
|
|
2023-12-07 12:43:27 +00:00
|
|
|
const isSelected = reactions?.some((r) => r.actor === currentUser?.id && r.reaction === reaction);
|
2023-07-27 13:25:03 +00:00
|
|
|
|
|
|
|
if (isSelected) {
|
|
|
|
handleReactionDelete(reaction);
|
|
|
|
} else {
|
|
|
|
handleReactionCreate(reaction);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="mt-4 flex items-center gap-1.5">
|
2023-07-27 13:25:03 +00:00
|
|
|
<ReactionSelector
|
|
|
|
size="md"
|
|
|
|
position="top"
|
2023-12-07 12:43:27 +00:00
|
|
|
value={reactions?.filter((reaction) => reaction.actor === currentUser?.id).map((r) => r.reaction) || []}
|
2023-07-27 13:25:03 +00:00
|
|
|
onSelect={handleReactionClick}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{Object.keys(groupedReactions || {}).map(
|
|
|
|
(reaction) =>
|
|
|
|
groupedReactions?.[reaction]?.length &&
|
|
|
|
groupedReactions[reaction].length > 0 && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
handleReactionClick(reaction);
|
|
|
|
}}
|
|
|
|
key={reaction}
|
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 ${
|
2023-12-07 12:43:27 +00:00
|
|
|
reactions?.some((r) => r.actor === currentUser?.id && r.reaction === reaction)
|
2023-07-27 13:25:03 +00:00
|
|
|
? "bg-custom-primary-100/10"
|
|
|
|
: "bg-custom-background-80"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<span>{renderEmoji(reaction)}</span>
|
2023-07-28 05:16:35 +00:00
|
|
|
<span
|
|
|
|
className={
|
2023-12-07 12:43:27 +00:00
|
|
|
reactions?.some((r) => r.actor === currentUser?.id && r.reaction === reaction)
|
2023-07-28 05:16:35 +00:00
|
|
|
? "text-custom-primary-100"
|
|
|
|
: ""
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{groupedReactions?.[reaction].length}{" "}
|
|
|
|
</span>
|
2023-07-27 13:25:03 +00:00
|
|
|
</button>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
2023-12-07 12:43:27 +00:00
|
|
|
});
|