import { useState } from "react"; import { observer } from "mobx-react"; import { useRouter } from "next/router"; // icons import { ArchiveRestoreIcon, LinkIcon, Pencil, Trash2 } from "lucide-react"; // ui import { ArchiveIcon, CustomMenu, TOAST_TYPE, setToast } from "@plane/ui"; // components import { ArchiveModuleModal, CreateUpdateModuleModal, DeleteModuleModal } from "@/components/modules"; // constants import { EUserProjectRoles } from "@/constants/project"; // helpers import { copyUrlToClipboard } from "@/helpers/string.helper"; // hooks import { useModule, useEventTracker, useUser } from "@/hooks/store"; type Props = { moduleId: string; projectId: string; workspaceSlug: string; isArchived?: boolean; }; export const ModuleQuickActions: React.FC = observer((props) => { const { moduleId, projectId, workspaceSlug, isArchived } = props; // router const router = useRouter(); // states const [editModal, setEditModal] = useState(false); const [archiveModuleModal, setArchiveModuleModal] = useState(false); const [deleteModal, setDeleteModal] = useState(false); // store hooks const { setTrackElement } = useEventTracker(); const { membership: { currentWorkspaceAllProjectsRole }, } = useUser(); const { getModuleById, restoreModule } = useModule(); // derived values const moduleDetails = getModuleById(moduleId); // auth const isEditingAllowed = !!currentWorkspaceAllProjectsRole && currentWorkspaceAllProjectsRole[projectId] >= EUserProjectRoles.MEMBER; const moduleState = moduleDetails?.status?.toLocaleLowerCase(); const isInArchivableGroup = !!moduleState && ["completed", "cancelled"].includes(moduleState); const handleCopyText = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); copyUrlToClipboard(`${workspaceSlug}/projects/${projectId}/modules/${moduleId}`).then(() => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Link Copied!", message: "Module link copied to clipboard.", }); }); }; const handleEditModule = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); setTrackElement("Modules page list layout"); setEditModal(true); }; const handleArchiveModule = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); setArchiveModuleModal(true); }; const handleRestoreModule = async (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); await restoreModule(workspaceSlug, projectId, moduleId) .then(() => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Restore success", message: "Your module can be found in project modules.", }); router.push(`/${workspaceSlug}/projects/${projectId}/archives/modules`); }) .catch(() => setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Module could not be restored. Please try again.", }) ); }; const handleDeleteModule = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); setTrackElement("Modules page list layout"); setDeleteModal(true); }; return ( <> {moduleDetails && (
setEditModal(false)} data={moduleDetails} projectId={projectId} workspaceSlug={workspaceSlug} /> setArchiveModuleModal(false)} /> setDeleteModal(false)} />
)} {isEditingAllowed && !isArchived && ( Edit module )} {isEditingAllowed && !isArchived && ( {isInArchivableGroup ? (
Archive module
) : (

Archive module

Only completed or cancelled
module can be archived.

)}
)} {isEditingAllowed && isArchived && ( Restore module )} {!isArchived && ( Copy module link )} {isEditingAllowed && (
Delete module
)}
); });