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, Transition } from "@headlessui/react"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; // services import { ModuleService } from "services/module.service"; // 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 { CustomMenu, Loader, LayersIcon, CustomSelect, ModuleStatusIcon } from "@plane/ui"; // icon import { AlertCircle, ChevronDown, ChevronRight, Info, LinkIcon, Plus, Trash2 } from "lucide-react"; // helpers import { renderShortDate, renderShortMonthDate } from "helpers/date-time.helper"; import { 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: [], start_date: null, target_date: null, status: "backlog", }; type Props = { moduleId: string; handleClose: () => void; }; // services const moduleService = new ModuleService(); // TODO: refactor this component export const ModuleDetailsSidebar: React.FC = observer((props) => { const { moduleId, handleClose } = props; const [moduleDeleteModal, setModuleDeleteModal] = useState(false); const [moduleLinkModal, setModuleLinkModal] = useState(false); const [selectedLinkToUpdate, setSelectedLinkToUpdate] = useState(null); const router = useRouter(); const { workspaceSlug, projectId, peekModule } = router.query; const { module: moduleStore, user: userStore } = useMobxStore(); const user = userStore.currentUser ?? undefined; const userRole = userStore.currentProjectRole; const moduleDetails = moduleStore.moduleDetails[moduleId] ?? undefined; const { setToastAlert } = useToast(); const { reset, 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, }); }, [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) => { console.log("link", link); setSelectedLinkToUpdate(link); setModuleLinkModal(true); }; if (!moduleDetails) return null; const startDate = new Date(moduleDetails.start_date ?? ""); const endDate = new Date(moduleDetails.target_date ?? ""); const areYearsEqual = startDate.getFullYear() === endDate.getFullYear(); const moduleStatus = MODULE_STATUS.find((status) => status.value === moduleDetails.status); const issueCount = moduleDetails.total_issues === 0 ? "0 Issue" : moduleDetails.total_issues === moduleDetails.completed_issues ? moduleDetails.total_issues > 1 ? `${moduleDetails.total_issues}` : `${moduleDetails.total_issues}` : `${moduleDetails.completed_issues}/${moduleDetails.total_issues}`; return ( <> { setModuleLinkModal(false); setSelectedLinkToUpdate(null); }} data={selectedLinkToUpdate} status={selectedLinkToUpdate ? true : false} createIssueLink={handleCreateLink} updateIssueLink={handleUpdateLink} /> setModuleDeleteModal(false)} data={moduleDetails} /> {module ? ( <>
setModuleDeleteModal(true)}> Delete

{moduleDetails.name}

( {moduleStatus?.label ?? "Backlog"} } value={value} onChange={(value: any) => { submitChanges({ status: value }); }} > {MODULE_STATUS.map((status) => (
{status.label}
))}
)} /> {areYearsEqual ? renderShortDate(startDate, "_ _") : renderShortMonthDate(startDate, "_ _")} -{" "} {areYearsEqual ? renderShortDate(endDate, "_ _") : renderShortMonthDate(endDate, "_ _")}
{moduleDetails.description && ( {moduleDetails.description} )}
( { submitChanges({ lead: val }); }} /> )} /> ( { submitChanges({ members: val }); }} /> )} />
Issues
{issueCount}
{({ open }) => (
Progress
{progressPercentage ? ( {progressPercentage ? `${progressPercentage}%` : ""} ) : ( "" )} {isStartValid && isEndValid ? (
{isStartValid && isEndValid ? (
Ideal
Current
) : ( "" )} {moduleDetails.total_issues > 0 && (
)}
)}
{({ open }) => (
Links
{userRole && moduleDetails.link_module && moduleDetails.link_module.length > 0 ? ( <>
) : (
No links added yet
)}
)}
) : (
)} ); });