import { FC, MouseEvent } from "react"; import { observer } from "mobx-react"; import Link from "next/link"; import { useRouter } from "next/router"; // icons import { Check, Info, User2 } from "lucide-react"; // types import type { TCycleGroups } from "@plane/types"; // ui import { Tooltip, CircularProgressIndicator, CycleGroupIcon, AvatarGroup, Avatar, setPromiseToast } from "@plane/ui"; // components import { FavoriteStar } from "@/components/core"; import { CycleQuickActions } from "@/components/cycles"; // constants import { CYCLE_STATUS } from "@/constants/cycle"; import { CYCLE_FAVORITED, CYCLE_UNFAVORITED } from "@/constants/event-tracker"; import { EUserProjectRoles } from "@/constants/project"; // helpers import { findHowManyDaysLeft, getDate, renderFormattedDate } from "@/helpers/date-time.helper"; // hooks import { useEventTracker, useCycle, useUser, useMember } from "@/hooks/store"; import { usePlatformOS } from "@/hooks/use-platform-os"; type TCyclesListItem = { cycleId: string; handleEditCycle?: () => void; handleDeleteCycle?: () => void; handleAddToFavorites?: () => void; handleRemoveFromFavorites?: () => void; workspaceSlug: string; projectId: string; isArchived?: boolean; }; export const CyclesListItem: FC = observer((props) => { const { cycleId, workspaceSlug, projectId, isArchived } = props; // router const router = useRouter(); // hooks const { isMobile } = usePlatformOS(); // store hooks const { captureEvent } = useEventTracker(); const { membership: { currentProjectRole }, } = useUser(); const { getCycleById, addCycleToFavorites, removeCycleFromFavorites } = useCycle(); const { getUserDetails } = useMember(); const handleAddToFavorites = (e: MouseEvent) => { e.preventDefault(); if (!workspaceSlug || !projectId) return; const addToFavoritePromise = addCycleToFavorites(workspaceSlug?.toString(), projectId.toString(), cycleId).then( () => { captureEvent(CYCLE_FAVORITED, { cycle_id: cycleId, element: "List layout", state: "SUCCESS", }); } ); setPromiseToast(addToFavoritePromise, { loading: "Adding cycle to favorites...", success: { title: "Success!", message: () => "Cycle added to favorites.", }, error: { title: "Error!", message: () => "Couldn't add the cycle to favorites. Please try again.", }, }); }; const handleRemoveFromFavorites = (e: MouseEvent) => { e.preventDefault(); if (!workspaceSlug || !projectId) return; const removeFromFavoritePromise = removeCycleFromFavorites( workspaceSlug?.toString(), projectId.toString(), cycleId ).then(() => { captureEvent(CYCLE_UNFAVORITED, { cycle_id: cycleId, element: "List layout", state: "SUCCESS", }); }); setPromiseToast(removeFromFavoritePromise, { loading: "Removing cycle from favorites...", success: { title: "Success!", message: () => "Cycle removed from favorites.", }, error: { title: "Error!", message: () => "Couldn't remove the cycle from favorites. Please try again.", }, }); }; const openCycleOverview = (e: MouseEvent) => { const { query } = router; e.preventDefault(); e.stopPropagation(); if (query.peekCycle) { delete query.peekCycle; router.push({ pathname: router.pathname, query: { ...query }, }); } else { 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 = getDate(cycleDetails.end_date); const startDate = getDate(cycleDetails.start_date); const isEditingAllowed = !!currentProjectRole && currentProjectRole >= EUserProjectRoles.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 (
{ if (isArchived) { openCycleOverview(e); } }} >
{isCompleted ? ( progress === 100 ? ( ) : ( {`!`} ) ) : progress === 100 ? ( ) : ( {`${progress}%`} )}
{cycleDetails.name}
{renderDate && `${renderFormattedDate(startDate) ?? `_ _`} - ${renderFormattedDate(endDate) ?? `_ _`}`}
{currentCycle && (
{currentCycle.value === "current" ? `${daysLeft} ${daysLeft > 1 ? "days" : "day"} left` : `${currentCycle.label}`}
)}
{cycleDetails.assignee_ids && cycleDetails.assignee_ids?.length > 0 ? ( {cycleDetails.assignee_ids?.map((assignee_id) => { const member = getUserDetails(assignee_id); return ; })} ) : ( )}
{isEditingAllowed && !isArchived && ( { if (cycleDetails.is_favorite) handleRemoveFromFavorites(e); else handleAddToFavorites(e); }} selected={!!cycleDetails.is_favorite} /> )}
); });