2024-01-10 14:39:45 +00:00
|
|
|
import { FC, useMemo } from "react";
|
|
|
|
import { observer } from "mobx-react-lite";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { IUser } from "@plane/types";
|
2024-03-12 13:42:25 +00:00
|
|
|
// hooks
|
2024-03-06 08:48:41 +00:00
|
|
|
// ui
|
2024-03-12 13:42:25 +00:00
|
|
|
import { TOAST_TYPE, Tooltip, setToast } from "@plane/ui";
|
|
|
|
// helpers
|
2024-04-03 12:32:07 +00:00
|
|
|
import { cn } from "@/helpers/common.helper";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { renderEmoji } from "@/helpers/emoji.helper";
|
|
|
|
import { formatTextList } from "@/helpers/issue.helper";
|
|
|
|
import { useIssueDetail, useMember } from "@/hooks/store";
|
2024-01-10 14:39:45 +00:00
|
|
|
// types
|
2024-03-06 13:09:14 +00:00
|
|
|
import { ReactionSelector } from "./reaction-selector";
|
2024-01-10 14:39:45 +00:00
|
|
|
|
|
|
|
export type TIssueReaction = {
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
|
|
|
currentUser: IUser;
|
2024-04-03 12:32:07 +00:00
|
|
|
disabled?: boolean;
|
2024-01-10 14:39:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const IssueReaction: FC<TIssueReaction> = observer((props) => {
|
2024-04-03 12:32:07 +00:00
|
|
|
const { workspaceSlug, projectId, issueId, currentUser, disabled = false } = props;
|
2024-01-10 14:39:45 +00:00
|
|
|
// hooks
|
|
|
|
const {
|
2024-03-12 13:42:25 +00:00
|
|
|
reaction: { getReactionsByIssueId, reactionsByUser, getReactionById },
|
2024-01-10 14:39:45 +00:00
|
|
|
createReaction,
|
|
|
|
removeReaction,
|
|
|
|
} = useIssueDetail();
|
2024-03-12 13:42:25 +00:00
|
|
|
const { getUserDetails } = useMember();
|
2024-01-10 14:39:45 +00:00
|
|
|
|
|
|
|
const reactionIds = getReactionsByIssueId(issueId);
|
|
|
|
const userReactions = reactionsByUser(issueId, currentUser.id).map((r) => r.reaction);
|
|
|
|
|
|
|
|
const issueReactionOperations = useMemo(
|
|
|
|
() => ({
|
|
|
|
create: async (reaction: string) => {
|
|
|
|
try {
|
|
|
|
if (!workspaceSlug || !projectId || !issueId) throw new Error("Missing fields");
|
|
|
|
await createReaction(workspaceSlug, projectId, issueId, reaction);
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
2024-01-10 14:39:45 +00:00
|
|
|
title: "Reaction created successfully",
|
2024-03-06 08:48:41 +00:00
|
|
|
type: TOAST_TYPE.SUCCESS,
|
2024-01-10 14:39:45 +00:00
|
|
|
message: "Reaction created successfully",
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
2024-01-10 14:39:45 +00:00
|
|
|
title: "Reaction creation failed",
|
2024-03-06 08:48:41 +00:00
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-01-10 14:39:45 +00:00
|
|
|
message: "Reaction creation failed",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remove: async (reaction: string) => {
|
|
|
|
try {
|
2024-01-23 07:58:58 +00:00
|
|
|
if (!workspaceSlug || !projectId || !issueId || !currentUser?.id) throw new Error("Missing fields");
|
2024-01-10 14:39:45 +00:00
|
|
|
await removeReaction(workspaceSlug, projectId, issueId, reaction, currentUser.id);
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
2024-01-10 14:39:45 +00:00
|
|
|
title: "Reaction removed successfully",
|
2024-03-06 08:48:41 +00:00
|
|
|
type: TOAST_TYPE.SUCCESS,
|
2024-01-10 14:39:45 +00:00
|
|
|
message: "Reaction removed successfully",
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
2024-01-10 14:39:45 +00:00
|
|
|
title: "Reaction remove failed",
|
2024-03-06 08:48:41 +00:00
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-01-10 14:39:45 +00:00
|
|
|
message: "Reaction remove failed",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
react: async (reaction: string) => {
|
|
|
|
if (userReactions.includes(reaction)) await issueReactionOperations.remove(reaction);
|
|
|
|
else await issueReactionOperations.create(reaction);
|
|
|
|
},
|
|
|
|
}),
|
2024-03-06 08:48:41 +00:00
|
|
|
[workspaceSlug, projectId, issueId, currentUser, createReaction, removeReaction, userReactions]
|
2024-01-10 14:39:45 +00:00
|
|
|
);
|
|
|
|
|
2024-03-12 13:42:25 +00:00
|
|
|
const getReactionUsers = (reaction: string): string => {
|
|
|
|
const reactionUsers = (reactionIds?.[reaction] || [])
|
|
|
|
.map((reactionId) => {
|
|
|
|
const reactionDetails = getReactionById(reactionId);
|
2024-03-15 11:56:38 +00:00
|
|
|
return reactionDetails ? getUserDetails(reactionDetails.actor)?.display_name : null;
|
2024-03-12 13:42:25 +00:00
|
|
|
})
|
|
|
|
.filter((displayName): displayName is string => !!displayName);
|
|
|
|
|
|
|
|
const formattedUsers = formatTextList(reactionUsers);
|
|
|
|
return formattedUsers;
|
|
|
|
};
|
|
|
|
|
2024-01-10 14:39:45 +00:00
|
|
|
return (
|
|
|
|
<div className="mt-4 relative flex items-center gap-1.5">
|
2024-04-03 12:32:07 +00:00
|
|
|
{!disabled && (
|
|
|
|
<ReactionSelector size="md" position="top" value={userReactions} onSelect={issueReactionOperations.react} />
|
|
|
|
)}
|
2024-01-10 14:39:45 +00:00
|
|
|
|
|
|
|
{reactionIds &&
|
|
|
|
Object.keys(reactionIds || {}).map(
|
|
|
|
(reaction) =>
|
|
|
|
reactionIds[reaction]?.length > 0 && (
|
|
|
|
<>
|
2024-03-12 13:42:25 +00:00
|
|
|
<Tooltip tooltipContent={getReactionUsers(reaction)}>
|
|
|
|
<button
|
|
|
|
type="button"
|
2024-04-03 12:32:07 +00:00
|
|
|
onClick={() => !disabled && issueReactionOperations.react(reaction)}
|
2024-03-12 13:42:25 +00:00
|
|
|
key={reaction}
|
2024-04-03 12:32:07 +00:00
|
|
|
className={cn(
|
|
|
|
"flex h-full items-center gap-1 rounded-md px-2 py-1 text-sm text-custom-text-100",
|
|
|
|
userReactions.includes(reaction) ? "bg-custom-primary-100/10" : "bg-custom-background-80",
|
|
|
|
{
|
|
|
|
"cursor-not-allowed": disabled,
|
|
|
|
}
|
|
|
|
)}
|
2024-03-12 13:42:25 +00:00
|
|
|
>
|
|
|
|
<span>{renderEmoji(reaction)}</span>
|
|
|
|
<span className={userReactions.includes(reaction) ? "text-custom-primary-100" : ""}>
|
|
|
|
{(reactionIds || {})[reaction].length}{" "}
|
|
|
|
</span>
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
2024-01-10 14:39:45 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|