import { FC, MouseEvent, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import { observer } from "mobx-react"; // hooks import { useEventTracker, useCycle, useUser, useMember } from "hooks/store"; import useToast from "hooks/use-toast"; // components import { CycleCreateUpdateModal, CycleDeleteModal } from "components/cycles"; // ui import { CustomMenu, Tooltip, CircularProgressIndicator, CycleGroupIcon, AvatarGroup, Avatar } from "@plane/ui"; // icons import { Check, Info, LinkIcon, Pencil, Star, Trash2, User2 } from "lucide-react"; // helpers import { findHowManyDaysLeft, renderFormattedDate } from "helpers/date-time.helper"; import { copyTextToClipboard } from "helpers/string.helper"; // constants import { CYCLE_STATUS } from "constants/cycle"; import { EUserWorkspaceRoles } from "constants/workspace"; // types import { TCycleGroups } from "@plane/types"; import { CYCLE_FAVORITED, CYCLE_UNFAVORITED } from "constants/event-tracker"; type TCyclesListItem = { cycleId: string; handleEditCycle?: () => void; handleDeleteCycle?: () => void; handleAddToFavorites?: () => void; handleRemoveFromFavorites?: () => void; workspaceSlug: string; projectId: string; }; export const CyclesListItem: FC = observer((props) => { const { cycleId, workspaceSlug, projectId } = props; // states const [updateModal, setUpdateModal] = useState(false); const [deleteModal, setDeleteModal] = useState(false); // router const router = useRouter(); // store hooks const { setTrackElement, captureEvent } = useEventTracker(); const { membership: { currentProjectRole }, } = useUser(); const { getCycleById, addCycleToFavorites, removeCycleFromFavorites } = useCycle(); const { getUserDetails } = useMember(); // toast alert const { setToastAlert } = useToast(); const handleCopyText = (e: MouseEvent) => { e.preventDefault(); e.stopPropagation(); const originURL = typeof window !== "undefined" && window.location.origin ? window.location.origin : ""; copyTextToClipboard(`${originURL}/${workspaceSlug}/projects/${projectId}/cycles/${cycleId}`).then(() => { setToastAlert({ type: "success", title: "Link Copied!", message: "Cycle link copied to clipboard.", }); }); }; const handleAddToFavorites = (e: MouseEvent) => { e.preventDefault(); if (!workspaceSlug || !projectId) return; addCycleToFavorites(workspaceSlug?.toString(), projectId.toString(), cycleId) .then(() => { captureEvent(CYCLE_FAVORITED, { cycle_id: cycleId, element: "List layout", state: "SUCCESS", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't add the cycle to favorites. Please try again.", }); }); }; const handleRemoveFromFavorites = (e: MouseEvent) => { e.preventDefault(); if (!workspaceSlug || !projectId) return; removeCycleFromFavorites(workspaceSlug?.toString(), projectId.toString(), cycleId) .then(() => { captureEvent(CYCLE_UNFAVORITED, { cycle_id: cycleId, element: "List layout", state: "SUCCESS", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't add the cycle to favorites. Please try again.", }); }); }; const handleEditCycle = (e: MouseEvent) => { e.preventDefault(); e.stopPropagation(); setTrackElement("Cycles page list layout"); setUpdateModal(true); }; const handleDeleteCycle = (e: MouseEvent) => { e.preventDefault(); e.stopPropagation(); setTrackElement("Cycles page list layout"); setDeleteModal(true); }; const openCycleOverview = (e: MouseEvent) => { const { query } = router; e.preventDefault(); e.stopPropagation(); router.push({ pathname: router.pathname, query: { ...query, peekCycle: cycleId }, }); }; const cycleDetails = getCycleById(cycleId); if (!cycleDetails) return null; // computed // TODO: change this logic once backend fix the response const cycleStatus = cycleDetails.status ? (cycleDetails.status.toLocaleLowerCase() as TCycleGroups) : "draft"; const isCompleted = cycleStatus === "completed"; const endDate = new Date(cycleDetails.end_date ?? ""); const startDate = new Date(cycleDetails.start_date ?? ""); const isEditingAllowed = !!currentProjectRole && currentProjectRole >= EUserWorkspaceRoles.MEMBER; const cycleTotalIssues = cycleDetails.backlog_issues + cycleDetails.unstarted_issues + cycleDetails.started_issues + cycleDetails.completed_issues + cycleDetails.cancelled_issues; const renderDate = cycleDetails.start_date || cycleDetails.end_date; // const areYearsEqual = startDate.getFullYear() === endDate.getFullYear(); const completionPercentage = (cycleDetails.completed_issues / cycleTotalIssues) * 100; const progress = isNaN(completionPercentage) ? 0 : Math.floor(completionPercentage); const currentCycle = CYCLE_STATUS.find((status) => status.value === cycleStatus); const daysLeft = findHowManyDaysLeft(cycleDetails.end_date) ?? 0; return ( <> setUpdateModal(false)} workspaceSlug={workspaceSlug} projectId={projectId} /> setDeleteModal(false)} workspaceSlug={workspaceSlug} projectId={projectId} />
{isCompleted ? ( progress === 100 ? ( ) : ( {`!`} ) ) : progress === 100 ? ( ) : ( {`${progress}%`} )}
{cycleDetails.name}
{currentCycle && (
{currentCycle.value === "current" ? `${daysLeft} ${daysLeft > 1 ? "days" : "day"} left` : `${currentCycle.label}`}
)}
{renderDate && `${renderFormattedDate(startDate) ?? `_ _`} - ${renderFormattedDate(endDate) ?? `_ _`}`}
{cycleDetails.assignee_ids?.length > 0 ? ( {cycleDetails.assignee_ids?.map((assigne_id) => { const member = getUserDetails(assigne_id); return ; })} ) : ( )}
{isEditingAllowed && ( <> {cycleDetails.is_favorite ? ( ) : ( )} {!isCompleted && isEditingAllowed && ( <> Edit cycle Delete cycle )} Copy cycle link )}
); });