import React, { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import { mutate } from "swr"; // services import { ModuleService } from "services/module.service"; // hooks import useToast from "hooks/use-toast"; // components import { DeleteModuleModal } from "components/modules"; // ui import { AssigneesList, CustomMenu } from "components/ui"; import { Tooltip } from "@plane/ui"; // icons import { CalendarDays, LinkIcon, Pencil, Star, Target, Trash2 } from "lucide-react"; // helpers import { copyTextToClipboard, truncateText } from "helpers/string.helper"; import { renderShortDateWithYearFormat } from "helpers/date-time.helper"; // types import { IUser, IModule } from "types"; // fetch-key import { MODULE_LIST } from "constants/fetch-keys"; type Props = { module: IModule; handleEditModule: () => void; user: IUser | undefined; }; const moduleService = new ModuleService(); export const SingleModuleCard: React.FC = ({ module, handleEditModule, user }) => { const [moduleDeleteModal, setModuleDeleteModal] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const completionPercentage = ((module.completed_issues + module.cancelled_issues) / module.total_issues) * 100; const handleDeleteModule = () => { if (!module) return; setModuleDeleteModal(true); }; const handleAddToFavorites = () => { if (!workspaceSlug || !projectId || !module) return; mutate( MODULE_LIST(projectId as string), (prevData) => (prevData ?? []).map((m) => ({ ...m, is_favorite: m.id === module.id ? true : m.is_favorite, })), false ); moduleService .addModuleToFavorites(workspaceSlug as string, projectId as string, { module: module.id, }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't add the module to favorites. Please try again.", }); }); }; const handleRemoveFromFavorites = () => { if (!workspaceSlug || !projectId || !module) return; mutate( MODULE_LIST(projectId as string), (prevData) => (prevData ?? []).map((m) => ({ ...m, is_favorite: m.id === module.id ? false : m.is_favorite, })), false ); moduleService.removeModuleFromFavorites(workspaceSlug as string, projectId as string, module.id).catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't remove the module from favorites. Please try again.", }); }); }; const handleCopyText = () => { const originURL = typeof window !== "undefined" && window.location.origin ? window.location.origin : ""; copyTextToClipboard(`${originURL}/${workspaceSlug}/projects/${projectId}/modules/${module.id}`).then(() => { setToastAlert({ type: "success", title: "Link Copied!", message: "Module link copied to clipboard.", }); }); }; const endDate = new Date(module.target_date ?? ""); const startDate = new Date(module.start_date ?? ""); const lastUpdated = new Date(module.updated_at ?? ""); return ( <>

{truncateText(module.name, 75)}

{module?.status?.replace("-", " ")}
{module.is_favorite ? ( ) : ( )} Edit module Delete module Copy module link
Start: {renderShortDateWithYearFormat(startDate, "Not set")}
End: {renderShortDateWithYearFormat(endDate, "Not set")}
Progress
{isNaN(completionPercentage) ? 0 : completionPercentage.toFixed(0)}%

Last updated: {renderShortDateWithYearFormat(lastUpdated)}

{module.members_detail.length > 0 && (
)}
); };