"use client"; import React, { useRef } from "react"; import { observer } from "mobx-react-lite"; import Link from "next/link"; import { useParams, usePathname, useRouter, useSearchParams } from "next/navigation"; import { CalendarCheck2, CalendarClock, Info, MoveRight, SquareUser } from "lucide-react"; // ui import { LayersIcon, Tooltip, setPromiseToast } from "@plane/ui"; // components import { FavoriteStar } from "@/components/core"; import { ButtonAvatars } from "@/components/dropdowns/member/avatar"; import { ModuleQuickActions } from "@/components/modules"; // constants import { EEstimateSystem } from "@/constants/estimates"; import { MODULE_FAVORITED, MODULE_UNFAVORITED } from "@/constants/event-tracker"; import { MODULE_STATUS } from "@/constants/module"; import { EUserProjectRoles } from "@/constants/project"; // helpers import { getDate, renderFormattedDate } from "@/helpers/date-time.helper"; import { generateQueryParams } from "@/helpers/router.helper"; // hooks import { useEventTracker, useMember, useModule, useProjectEstimates, useUser } from "@/hooks/store"; import { usePlatformOS } from "@/hooks/use-platform-os"; type Props = { moduleId: string; }; export const ModuleCardItem: React.FC = observer((props) => { const { moduleId } = props; // refs const parentRef = useRef(null); // router const router = useRouter(); const { workspaceSlug, projectId } = useParams(); const searchParams = useSearchParams(); const pathname = usePathname(); // store hooks const { membership: { currentProjectRole }, } = useUser(); const { getModuleById, addModuleToFavorites, removeModuleFromFavorites } = useModule(); const { getUserDetails } = useMember(); const { captureEvent } = useEventTracker(); const { currentActiveEstimateId, areEstimateEnabledByProjectId, estimateById } = useProjectEstimates(); // derived values const moduleDetails = getModuleById(moduleId); const isEditingAllowed = !!currentProjectRole && currentProjectRole >= EUserProjectRoles.MEMBER; const { isMobile } = usePlatformOS(); const handleAddToFavorites = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); if (!workspaceSlug || !projectId) return; const addToFavoritePromise = addModuleToFavorites(workspaceSlug.toString(), projectId.toString(), moduleId).then( () => { captureEvent(MODULE_FAVORITED, { module_id: moduleId, element: "Grid layout", state: "SUCCESS", }); } ); setPromiseToast(addToFavoritePromise, { loading: "Adding module to favorites...", success: { title: "Success!", message: () => "Module added to favorites.", }, error: { title: "Error!", message: () => "Couldn't add the module to favorites. Please try again.", }, }); }; const handleRemoveFromFavorites = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); if (!workspaceSlug || !projectId) return; const removeFromFavoritePromise = removeModuleFromFavorites( workspaceSlug.toString(), projectId.toString(), moduleId ).then(() => { captureEvent(MODULE_UNFAVORITED, { module_id: moduleId, element: "Grid layout", state: "SUCCESS", }); }); setPromiseToast(removeFromFavoritePromise, { loading: "Removing module from favorites...", success: { title: "Success!", message: () => "Module removed from favorites.", }, error: { title: "Error!", message: () => "Couldn't remove the module from favorites. Please try again.", }, }); }; const openModuleOverview = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); const query = generateQueryParams(searchParams, ["peekModule"]); if (searchParams.has("peekModule")) { router.push(`${pathname}?${query}`); } else { router.push(`${pathname}?${query}&peekModule=${moduleId}`); } }; if (!moduleDetails) return null; /** * NOTE: This completion percentage calculation is based on the total issues count. * when estimates are available and estimate type is points, we should consider the estimate point count * when estimates are available and estimate type is not points, then by default we consider the issue count */ const isEstimateEnabled = projectId && currentActiveEstimateId && areEstimateEnabledByProjectId(projectId.toString()) && estimateById(currentActiveEstimateId)?.type === EEstimateSystem.POINTS; const moduleTotalIssues = isEstimateEnabled ? moduleDetails?.total_estimate_points || 0 : moduleDetails.backlog_issues + moduleDetails.unstarted_issues + moduleDetails.started_issues + moduleDetails.completed_issues + moduleDetails.cancelled_issues; const moduleCompletedIssues = isEstimateEnabled ? moduleDetails?.completed_estimate_points || 0 : moduleDetails.completed_issues; const completionPercentage = (moduleCompletedIssues / moduleTotalIssues) * 100; const endDate = getDate(moduleDetails.target_date); const startDate = getDate(moduleDetails.start_date); const isDateValid = moduleDetails.target_date || moduleDetails.start_date; // const areYearsEqual = startDate.getFullYear() === endDate.getFullYear(); const moduleStatus = MODULE_STATUS.find((status) => status.value === moduleDetails.status); const issueCount = module ? !moduleTotalIssues || moduleTotalIssues === 0 ? `0 ${isEstimateEnabled ? `Point` : `Issue`}` : moduleTotalIssues === moduleCompletedIssues ? `${moduleTotalIssues} Issue${moduleTotalIssues > 1 ? `s` : ``}` : `${moduleCompletedIssues}/${moduleTotalIssues} ${isEstimateEnabled ? `Points` : `Issues`}` : `0 ${isEstimateEnabled ? `Point` : `Issue`}`; const moduleLeadDetails = moduleDetails.lead_id ? getUserDetails(moduleDetails.lead_id) : undefined; return (
{moduleDetails.name}
{moduleStatus && ( {moduleStatus.label} )}
{issueCount ?? "0 Issue"}
{moduleLeadDetails ? ( ) : ( )}
{isDateValid ? (
{renderFormattedDate(startDate)} {renderFormattedDate(endDate)}
) : ( No due date )}
{isEditingAllowed && ( { if (moduleDetails.is_favorite) handleRemoveFromFavorites(e); else handleAddToFavorites(e); }} selected={!!moduleDetails.is_favorite} /> )} {workspaceSlug && projectId && ( )}
); });