import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { mutate } from "swr"; import { Controller, useForm } from "react-hook-form"; import { Disclosure, Popover, Transition } from "@headlessui/react"; import DatePicker from "react-datepicker"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; // services import { ModuleService } from "services/module.service"; // contexts import { useProjectMyMembership } from "contexts/project-member.context"; // hooks import useToast from "hooks/use-toast"; // components import { LinkModal, LinksList, SidebarProgressStats } from "components/core"; import { DeleteModuleModal, SidebarLeadSelect, SidebarMembersSelect } from "components/modules"; import ProgressChart from "components/core/sidebar/progress-chart"; // ui import { CustomSelect, CustomMenu, Loader, ProgressBar } from "@plane/ui"; // icon import { AlertCircle, CalendarDays, ChevronDown, File, LinkIcon, MoveRight, PieChart, Plus, Trash2, } from "lucide-react"; // helpers import { renderDateFormat, renderShortDateWithYearFormat } from "helpers/date-time.helper"; import { capitalizeFirstLetter, copyUrlToClipboard } from "helpers/string.helper"; // types import { linkDetails, IModule, ModuleLink } from "types"; // fetch-keys import { MODULE_DETAILS } from "constants/fetch-keys"; // constant import { MODULE_STATUS } from "constants/module"; const defaultValues: Partial = { lead: "", members_list: [], start_date: null, target_date: null, status: "backlog", }; type Props = { isOpen: boolean; moduleId: string; }; // services const moduleService = new ModuleService(); // TODO: refactor this component export const ModuleDetailsSidebar: React.FC = observer((props) => { const { isOpen, moduleId } = props; const [moduleDeleteModal, setModuleDeleteModal] = useState(false); const [moduleLinkModal, setModuleLinkModal] = useState(false); const [selectedLinkToUpdate, setSelectedLinkToUpdate] = useState(null); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { module: moduleStore, user: userStore } = useMobxStore(); const user = userStore.currentUser ?? undefined; const moduleDetails = moduleStore.moduleDetails[moduleId] ?? undefined; const { memberRole } = useProjectMyMembership(); const { setToastAlert } = useToast(); const { reset, watch, control } = useForm({ defaultValues, }); const submitChanges = (data: Partial) => { if (!workspaceSlug || !projectId || !moduleId) return; mutate( MODULE_DETAILS(moduleId as string), (prevData) => ({ ...(prevData as IModule), ...data, }), false ); moduleService .patchModule(workspaceSlug as string, projectId as string, moduleId as string, data, user) .then(() => mutate(MODULE_DETAILS(moduleId as string))) .catch((e) => console.log(e)); }; const handleCreateLink = async (formData: ModuleLink) => { if (!workspaceSlug || !projectId || !moduleId) return; const payload = { metadata: {}, ...formData }; await moduleService .createModuleLink(workspaceSlug as string, projectId as string, moduleId as string, payload) .then(() => mutate(MODULE_DETAILS(moduleId as string))) .catch((err) => { if (err.status === 400) setToastAlert({ type: "error", title: "Error!", message: "This URL already exists for this module.", }); else setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again.", }); }); }; const handleUpdateLink = async (formData: ModuleLink, linkId: string) => { if (!workspaceSlug || !projectId || !module) return; const payload = { metadata: {}, ...formData }; const updatedLinks = moduleDetails.link_module.map((l) => l.id === linkId ? { ...l, title: formData.title, url: formData.url, } : l ); mutate( MODULE_DETAILS(module.id), (prevData) => ({ ...(prevData as IModule), link_module: updatedLinks }), false ); await moduleService .updateModuleLink(workspaceSlug as string, projectId as string, module.id, linkId, payload) .then(() => { mutate(MODULE_DETAILS(module.id)); }) .catch((err) => { console.log(err); }); }; const handleDeleteLink = async (linkId: string) => { if (!workspaceSlug || !projectId || !module) return; const updatedLinks = moduleDetails.link_module.filter((l) => l.id !== linkId); mutate( MODULE_DETAILS(module.id), (prevData) => ({ ...(prevData as IModule), link_module: updatedLinks }), false ); await moduleService .deleteModuleLink(workspaceSlug as string, projectId as string, module.id, linkId) .then(() => { mutate(MODULE_DETAILS(module.id)); }) .catch((err) => { console.log(err); }); }; const handleCopyText = () => { copyUrlToClipboard(`${workspaceSlug}/projects/${projectId}/modules/${module?.id}`) .then(() => { setToastAlert({ type: "success", title: "Link copied", message: "Module link copied to clipboard", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Some error occurred", }); }); }; useEffect(() => { if (moduleDetails) reset({ ...moduleDetails, members_list: moduleDetails.members_list ?? moduleDetails.members_detail?.map((m) => m.id), }); }, [moduleDetails, reset]); const isStartValid = new Date(`${moduleDetails?.start_date}`) <= new Date(); const isEndValid = new Date(`${moduleDetails?.target_date}`) >= new Date(`${moduleDetails?.start_date}`); const progressPercentage = moduleDetails ? Math.round((moduleDetails.completed_issues / moduleDetails.total_issues) * 100) : null; const handleEditLink = (link: linkDetails) => { setSelectedLinkToUpdate(link); setModuleLinkModal(true); }; if (!moduleDetails) return null; return ( <> { setModuleLinkModal(false); setSelectedLinkToUpdate(null); }} data={selectedLinkToUpdate} status={selectedLinkToUpdate ? true : false} createIssueLink={handleCreateLink} updateIssueLink={handleUpdateLink} />
{module ? ( <>
( {capitalizeFirstLetter(`${watch("status")}`)} } value={value} onChange={(value: any) => { submitChanges({ status: value }); }} > {MODULE_STATUS.map((option) => ( {option.label} ))} )} />
{({}) => ( <> {renderShortDateWithYearFormat(new Date(`${moduleDetails.start_date}`), "Start date")} { submitChanges({ start_date: renderDateFormat(date), }); }} selectsStart startDate={new Date(`${watch("start_date")}`)} endDate={new Date(`${watch("target_date")}`)} maxDate={new Date(`${watch("target_date")}`)} shouldCloseOnSelect inline /> )} {({}) => ( <> {renderShortDateWithYearFormat(new Date(`${moduleDetails?.target_date}`), "End date")} { submitChanges({ target_date: renderDateFormat(date), }); }} selectsEnd startDate={new Date(`${watch("start_date")}`)} endDate={new Date(`${watch("target_date")}`)} minDate={new Date(`${watch("start_date")}`)} shouldCloseOnSelect inline /> )}

{moduleDetails.name}

setModuleDeleteModal(true)}> Delete Copy link
{moduleDetails.description}
( { submitChanges({ lead: val }); }} /> )} /> ( { submitChanges({ members_list: val }); }} /> )} />
Progress
{moduleDetails.completed_issues}/{moduleDetails.total_issues}
{({ open }) => (
Progress {!open && progressPercentage ? ( {progressPercentage ? `${progressPercentage}%` : ""} ) : ( "" )}
{isStartValid && isEndValid ? ( ) : (
Invalid date. Please enter valid date.
)}
{isStartValid && isEndValid ? (
Pending Issues -{" "} {moduleDetails.total_issues - (moduleDetails.completed_issues + moduleDetails.cancelled_issues)}{" "}
Ideal
Current
) : ( "" )}
)}
{({ open }) => (
Other Information
{moduleDetails.total_issues > 0 ? ( ) : (
No issues found. Please add issue.
)}
{moduleDetails.total_issues > 0 ? ( <>
) : ( "" )}
)}

Links

{memberRole && moduleDetails.link_module && moduleDetails.link_module.length > 0 ? ( ) : null}
) : (
)}
); });