"use client"; import { FC, MouseEvent, useRef } from "react"; import { observer } from "mobx-react"; import Link from "next/link"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { CalendarCheck2, CalendarClock, Info, MoveRight } from "lucide-react"; // types import type { TCycleGroups } from "@plane/types"; // ui import { Avatar, AvatarGroup, Tooltip, LayersIcon, CycleGroupIcon, 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 { EUserWorkspaceRoles } from "@/constants/workspace"; // helpers import { findHowManyDaysLeft, getDate, renderFormattedDate } from "@/helpers/date-time.helper"; import { generateQueryParams } from "@/helpers/router.helper"; // hooks import { useEventTracker, useCycle, useUser, useMember } from "@/hooks/store"; import { usePlatformOS } from "@/hooks/use-platform-os"; export interface ICyclesBoardCard { workspaceSlug: string; projectId: string; cycleId: string; } export const CyclesBoardCard: FC = observer((props) => { const { cycleId, workspaceSlug, projectId } = props; // refs const parentRef = useRef(null); // router const router = useRouter(); const searchParams = useSearchParams(); const pathname = usePathname(); // store const { captureEvent } = useEventTracker(); const { membership: { currentProjectRole }, } = useUser(); const { addCycleToFavorites, removeCycleFromFavorites, getCycleById } = useCycle(); const { getUserDetails } = useMember(); // computed const cycleDetails = getCycleById(cycleId); // hooks const { isMobile } = usePlatformOS(); if (!cycleDetails) return null; const cycleStatus = cycleDetails.status?.toLocaleLowerCase(); // const isCompleted = cycleStatus === "completed"; const endDate = getDate(cycleDetails.end_date); const startDate = getDate(cycleDetails.start_date); const isDateValid = cycleDetails.start_date || cycleDetails.end_date; const isEditingAllowed = !!currentProjectRole && currentProjectRole >= EUserWorkspaceRoles.MEMBER; const currentCycle = CYCLE_STATUS.find((status) => status.value === cycleStatus); const cycleTotalIssues = cycleDetails.backlog_issues + cycleDetails.unstarted_issues + cycleDetails.started_issues + cycleDetails.completed_issues + cycleDetails.cancelled_issues; const completionPercentage = (cycleDetails.completed_issues / cycleTotalIssues) * 100; const issueCount = cycleDetails ? cycleTotalIssues === 0 ? "0 Issue" : cycleTotalIssues === cycleDetails.completed_issues ? `${cycleTotalIssues} Issue${cycleTotalIssues > 1 ? "s" : ""}` : `${cycleDetails.completed_issues}/${cycleTotalIssues} Issues` : "0 Issue"; 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: "Grid 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: "Grid 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) => { e.preventDefault(); e.stopPropagation(); const query = generateQueryParams(searchParams, ["peekCycle"]); if (searchParams.has("peekCycle")) { router.push(`${pathname}?${query}`); } else { router.push(`${pathname}?${query && `${query}&`}peekCycle=${cycleId}`); } }; const daysLeft = findHowManyDaysLeft(cycleDetails.end_date) ?? 0; return (
{cycleDetails.name}
{currentCycle && ( {currentCycle.value === "current" ? `${daysLeft} ${daysLeft > 1 ? "days" : "day"} left` : `${currentCycle.label}`} )}
{issueCount}
{cycleDetails.assignee_ids && cycleDetails.assignee_ids.length > 0 && (
{cycleDetails.assignee_ids.map((assigne_id) => { const member = getUserDetails(assigne_id); return ; })}
)}
{isDateValid && (
{renderFormattedDate(startDate)} {renderFormattedDate(endDate)}
)}
{isEditingAllowed && ( { if (cycleDetails.is_favorite) handleRemoveFromFavorites(e); else handleAddToFavorites(e); }} selected={!!cycleDetails.is_favorite} /> )}
); });