2023-09-01 11:12:30 +00:00
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
|
2024-03-19 14:38:35 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2023-09-01 11:12:30 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
// mobx
|
|
|
|
// lib
|
2023-12-05 10:37:25 +00:00
|
|
|
import { Tooltip } from "@plane/ui";
|
2024-05-07 11:54:45 +00:00
|
|
|
// hooks
|
|
|
|
import { useUser } from "@/hooks/store";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { useMobxStore } from "@/lib/mobx/store-provider";
|
|
|
|
// ui
|
2023-09-01 11:12:30 +00:00
|
|
|
|
|
|
|
export const IssueVotes: React.FC = observer(() => {
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const { workspace_slug, project_slug } = router.query;
|
|
|
|
|
2024-05-07 11:54:45 +00:00
|
|
|
const { issueDetails: issueDetailsStore } = useMobxStore();
|
|
|
|
const { data: user, fetchCurrentUser } = useUser();
|
2023-09-01 11:12:30 +00:00
|
|
|
|
|
|
|
const issueId = issueDetailsStore.peekId;
|
|
|
|
|
|
|
|
const votes = issueId ? issueDetailsStore.details[issueId]?.votes : [];
|
|
|
|
|
|
|
|
const allUpVotes = votes?.filter((vote) => vote.vote === 1);
|
|
|
|
const allDownVotes = votes?.filter((vote) => vote.vote === -1);
|
|
|
|
|
|
|
|
const isUpVotedByUser = allUpVotes?.some((vote) => vote.actor === user?.id);
|
|
|
|
const isDownVotedByUser = allDownVotes?.some((vote) => vote.actor === user?.id);
|
|
|
|
|
|
|
|
const handleVote = async (e: any, voteValue: 1 | -1) => {
|
|
|
|
if (!workspace_slug || !project_slug || !issueId) return;
|
|
|
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
|
|
|
|
const actionPerformed = votes?.find((vote) => vote.actor === user?.id && vote.vote === voteValue);
|
|
|
|
|
|
|
|
if (actionPerformed)
|
|
|
|
await issueDetailsStore.removeIssueVote(workspace_slug.toString(), project_slug.toString(), issueId);
|
|
|
|
else
|
|
|
|
await issueDetailsStore.addIssueVote(workspace_slug.toString(), project_slug.toString(), issueId, {
|
|
|
|
vote: voteValue,
|
|
|
|
});
|
|
|
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (user) return;
|
|
|
|
|
2024-05-07 11:54:45 +00:00
|
|
|
fetchCurrentUser();
|
|
|
|
}, [user, fetchCurrentUser]);
|
2023-09-01 11:12:30 +00:00
|
|
|
|
|
|
|
const VOTES_LIMIT = 1000;
|
|
|
|
|
|
|
|
return (
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="flex items-center gap-2">
|
2023-09-01 11:12:30 +00:00
|
|
|
{/* upvote button 👇 */}
|
|
|
|
<Tooltip
|
|
|
|
tooltipContent={
|
|
|
|
<div>
|
|
|
|
{allUpVotes.length > 0 ? (
|
|
|
|
<>
|
|
|
|
{allUpVotes
|
|
|
|
.map((r) => r.actor_detail.display_name)
|
|
|
|
.splice(0, VOTES_LIMIT)
|
|
|
|
.join(", ")}
|
|
|
|
{allUpVotes.length > VOTES_LIMIT && " and " + (allUpVotes.length - VOTES_LIMIT) + " more"}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
"No upvotes yet"
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
disabled={isSubmitting}
|
|
|
|
onClick={(e) => {
|
2024-05-07 11:54:45 +00:00
|
|
|
if (user) handleVote(e, 1);
|
|
|
|
// userStore.requiredLogin(() => {});
|
2023-09-01 11:12:30 +00:00
|
|
|
}}
|
2023-12-10 10:18:10 +00:00
|
|
|
className={`flex items-center justify-center gap-x-1 overflow-hidden rounded border px-2 focus:outline-none ${
|
2023-09-01 11:12:30 +00:00
|
|
|
isUpVotedByUser ? "border-custom-primary-200 text-custom-primary-200" : "border-custom-border-300"
|
|
|
|
}`}
|
|
|
|
>
|
2023-12-10 10:18:10 +00:00
|
|
|
<span className="material-symbols-rounded !m-0 !p-0 text-base">arrow_upward_alt</span>
|
2023-09-01 11:12:30 +00:00
|
|
|
<span className="text-sm font-normal transition-opacity ease-in-out">{allUpVotes.length}</span>
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
|
|
|
|
{/* downvote button 👇 */}
|
|
|
|
<Tooltip
|
|
|
|
tooltipContent={
|
|
|
|
<div>
|
|
|
|
{allDownVotes.length > 0 ? (
|
|
|
|
<>
|
|
|
|
{allDownVotes
|
|
|
|
.map((r) => r.actor_detail.display_name)
|
|
|
|
.splice(0, VOTES_LIMIT)
|
|
|
|
.join(", ")}
|
|
|
|
{allDownVotes.length > VOTES_LIMIT && " and " + (allDownVotes.length - VOTES_LIMIT) + " more"}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
"No downvotes yet"
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
disabled={isSubmitting}
|
|
|
|
onClick={(e) => {
|
2024-05-07 11:54:45 +00:00
|
|
|
if (user) handleVote(e, -1);
|
|
|
|
// userStore.requiredLogin(() => {});
|
2023-09-01 11:12:30 +00:00
|
|
|
}}
|
2023-12-10 10:18:10 +00:00
|
|
|
className={`flex items-center justify-center gap-x-1 overflow-hidden rounded border px-2 focus:outline-none ${
|
2023-09-01 11:12:30 +00:00
|
|
|
isDownVotedByUser ? "border-red-600 text-red-600" : "border-custom-border-300"
|
|
|
|
}`}
|
|
|
|
>
|
2023-12-10 10:18:10 +00:00
|
|
|
<span className="material-symbols-rounded !m-0 !p-0 text-base">arrow_downward_alt</span>
|
2023-09-01 11:12:30 +00:00
|
|
|
<span className="text-sm font-normal transition-opacity ease-in-out">{allDownVotes.length}</span>
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|