2024-06-10 15:13:10 +05:30
|
|
|
"use client";
|
|
|
|
|
|
2024-03-20 21:02:58 +05:30
|
|
|
import { useState } from "react";
|
|
|
|
|
import { observer } from "mobx-react";
|
2024-06-18 11:35:20 +05:30
|
|
|
|
2024-03-20 21:02:58 +05:30
|
|
|
// icons
|
2024-04-30 18:59:07 +05:30
|
|
|
import { ArchiveRestoreIcon, ExternalLink, LinkIcon, Pencil, Trash2 } from "lucide-react";
|
2025-02-06 20:41:31 +05:30
|
|
|
// plane imports
|
|
|
|
|
import { EUserPermissions, EUserPermissionsLevel } from "@plane/constants";
|
|
|
|
|
import { useTranslation } from "@plane/i18n";
|
2024-03-20 21:02:58 +05:30
|
|
|
// ui
|
2024-04-30 18:59:07 +05:30
|
|
|
import { ArchiveIcon, ContextMenu, CustomMenu, TContextMenuItem, TOAST_TYPE, setToast } from "@plane/ui";
|
2024-03-20 21:02:58 +05:30
|
|
|
// components
|
|
|
|
|
import { ArchiveModuleModal, CreateUpdateModuleModal, DeleteModuleModal } from "@/components/modules";
|
|
|
|
|
// helpers
|
2024-04-30 18:59:07 +05:30
|
|
|
import { cn } from "@/helpers/common.helper";
|
2024-03-20 21:02:58 +05:30
|
|
|
import { copyUrlToClipboard } from "@/helpers/string.helper";
|
|
|
|
|
// hooks
|
2024-09-11 17:10:15 +05:30
|
|
|
import { useModule, useEventTracker, useUserPermissions } from "@/hooks/store";
|
2024-06-18 11:35:20 +05:30
|
|
|
import { useAppRouter } from "@/hooks/use-app-router";
|
2024-03-20 21:02:58 +05:30
|
|
|
|
|
|
|
|
type Props = {
|
2024-04-30 18:59:07 +05:30
|
|
|
parentRef: React.RefObject<HTMLDivElement>;
|
2024-03-20 21:02:58 +05:30
|
|
|
moduleId: string;
|
|
|
|
|
projectId: string;
|
|
|
|
|
workspaceSlug: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const ModuleQuickActions: React.FC<Props> = observer((props) => {
|
2024-04-30 18:59:07 +05:30
|
|
|
const { parentRef, moduleId, projectId, workspaceSlug } = props;
|
2024-03-20 21:02:58 +05:30
|
|
|
// router
|
2024-06-18 11:35:20 +05:30
|
|
|
const router = useAppRouter();
|
2024-03-20 21:02:58 +05:30
|
|
|
// states
|
|
|
|
|
const [editModal, setEditModal] = useState(false);
|
|
|
|
|
const [archiveModuleModal, setArchiveModuleModal] = useState(false);
|
|
|
|
|
const [deleteModal, setDeleteModal] = useState(false);
|
|
|
|
|
// store hooks
|
|
|
|
|
const { setTrackElement } = useEventTracker();
|
2024-09-11 17:10:15 +05:30
|
|
|
const { allowPermissions } = useUserPermissions();
|
|
|
|
|
|
2024-03-20 21:02:58 +05:30
|
|
|
const { getModuleById, restoreModule } = useModule();
|
2025-02-06 20:41:31 +05:30
|
|
|
|
|
|
|
|
const { t } = useTranslation();
|
2024-03-20 21:02:58 +05:30
|
|
|
// derived values
|
|
|
|
|
const moduleDetails = getModuleById(moduleId);
|
2024-04-30 18:59:07 +05:30
|
|
|
const isArchived = !!moduleDetails?.archived_at;
|
2024-03-20 21:02:58 +05:30
|
|
|
// auth
|
2024-09-11 17:10:15 +05:30
|
|
|
const isEditingAllowed = allowPermissions(
|
|
|
|
|
[EUserPermissions.ADMIN, EUserPermissions.MEMBER],
|
|
|
|
|
EUserPermissionsLevel.PROJECT,
|
|
|
|
|
workspaceSlug,
|
|
|
|
|
projectId
|
|
|
|
|
);
|
2024-03-20 21:02:58 +05:30
|
|
|
|
2024-04-05 20:05:55 +05:30
|
|
|
const moduleState = moduleDetails?.status?.toLocaleLowerCase();
|
2024-03-20 21:02:58 +05:30
|
|
|
const isInArchivableGroup = !!moduleState && ["completed", "cancelled"].includes(moduleState);
|
|
|
|
|
|
2024-04-30 18:59:07 +05:30
|
|
|
const moduleLink = `${workspaceSlug}/projects/${projectId}/modules/${moduleId}`;
|
|
|
|
|
const handleCopyText = () =>
|
|
|
|
|
copyUrlToClipboard(moduleLink).then(() => {
|
2024-03-20 21:02:58 +05:30
|
|
|
setToast({
|
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
|
|
|
|
title: "Link Copied!",
|
|
|
|
|
message: "Module link copied to clipboard.",
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-04-30 18:59:07 +05:30
|
|
|
const handleOpenInNewTab = () => window.open(`/${moduleLink}`, "_blank");
|
2024-03-20 21:02:58 +05:30
|
|
|
|
2024-04-30 18:59:07 +05:30
|
|
|
const handleEditModule = () => {
|
2024-03-20 21:02:58 +05:30
|
|
|
setTrackElement("Modules page list layout");
|
|
|
|
|
setEditModal(true);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-30 18:59:07 +05:30
|
|
|
const handleArchiveModule = () => setArchiveModuleModal(true);
|
2024-03-20 21:02:58 +05:30
|
|
|
|
2024-04-30 18:59:07 +05:30
|
|
|
const handleRestoreModule = async () =>
|
2024-03-20 21:02:58 +05:30
|
|
|
await restoreModule(workspaceSlug, projectId, moduleId)
|
|
|
|
|
.then(() => {
|
|
|
|
|
setToast({
|
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
|
|
|
|
title: "Restore success",
|
|
|
|
|
message: "Your module can be found in project modules.",
|
|
|
|
|
});
|
2024-04-03 18:19:34 +05:30
|
|
|
router.push(`/${workspaceSlug}/projects/${projectId}/archives/modules`);
|
2024-03-20 21:02:58 +05:30
|
|
|
})
|
|
|
|
|
.catch(() =>
|
|
|
|
|
setToast({
|
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
|
title: "Error!",
|
|
|
|
|
message: "Module could not be restored. Please try again.",
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
2024-04-30 18:59:07 +05:30
|
|
|
const handleDeleteModule = () => {
|
2024-03-20 21:02:58 +05:30
|
|
|
setTrackElement("Modules page list layout");
|
|
|
|
|
setDeleteModal(true);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-30 18:59:07 +05:30
|
|
|
const MENU_ITEMS: TContextMenuItem[] = [
|
|
|
|
|
{
|
|
|
|
|
key: "edit",
|
2025-02-06 20:41:31 +05:30
|
|
|
title: t("edit"),
|
2024-04-30 18:59:07 +05:30
|
|
|
icon: Pencil,
|
|
|
|
|
action: handleEditModule,
|
|
|
|
|
shouldRender: isEditingAllowed && !isArchived,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: "open-new-tab",
|
|
|
|
|
action: handleOpenInNewTab,
|
2025-02-06 20:41:31 +05:30
|
|
|
title: t("open_in_new_tab"),
|
2024-04-30 18:59:07 +05:30
|
|
|
icon: ExternalLink,
|
|
|
|
|
shouldRender: !isArchived,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: "copy-link",
|
|
|
|
|
action: handleCopyText,
|
2025-02-06 20:41:31 +05:30
|
|
|
title: t("copy_link"),
|
2024-04-30 18:59:07 +05:30
|
|
|
icon: LinkIcon,
|
|
|
|
|
shouldRender: !isArchived,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: "archive",
|
|
|
|
|
action: handleArchiveModule,
|
2025-02-06 20:41:31 +05:30
|
|
|
title: t("archive"),
|
|
|
|
|
description: isInArchivableGroup ? undefined : t("project_module.quick_actions.archive_module_description"),
|
2024-04-30 18:59:07 +05:30
|
|
|
icon: ArchiveIcon,
|
|
|
|
|
className: "items-start",
|
|
|
|
|
iconClassName: "mt-1",
|
|
|
|
|
shouldRender: isEditingAllowed && !isArchived,
|
|
|
|
|
disabled: !isInArchivableGroup,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: "restore",
|
|
|
|
|
action: handleRestoreModule,
|
2025-02-06 20:41:31 +05:30
|
|
|
title: t("restore"),
|
2024-04-30 18:59:07 +05:30
|
|
|
icon: ArchiveRestoreIcon,
|
|
|
|
|
shouldRender: isEditingAllowed && isArchived,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: "delete",
|
|
|
|
|
action: handleDeleteModule,
|
2025-02-06 20:41:31 +05:30
|
|
|
title: t("delete"),
|
2024-04-30 18:59:07 +05:30
|
|
|
icon: Trash2,
|
|
|
|
|
shouldRender: isEditingAllowed,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2024-03-20 21:02:58 +05:30
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{moduleDetails && (
|
|
|
|
|
<div className="fixed">
|
|
|
|
|
<CreateUpdateModuleModal
|
|
|
|
|
isOpen={editModal}
|
|
|
|
|
onClose={() => setEditModal(false)}
|
|
|
|
|
data={moduleDetails}
|
|
|
|
|
projectId={projectId}
|
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
|
/>
|
|
|
|
|
<ArchiveModuleModal
|
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
|
projectId={projectId}
|
|
|
|
|
moduleId={moduleId}
|
|
|
|
|
isOpen={archiveModuleModal}
|
|
|
|
|
handleClose={() => setArchiveModuleModal(false)}
|
|
|
|
|
/>
|
|
|
|
|
<DeleteModuleModal data={moduleDetails} isOpen={deleteModal} onClose={() => setDeleteModal(false)} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-04-30 18:59:07 +05:30
|
|
|
<ContextMenu parentRef={parentRef} items={MENU_ITEMS} />
|
|
|
|
|
<CustomMenu ellipsis placement="bottom-end" closeOnSelect>
|
|
|
|
|
{MENU_ITEMS.map((item) => {
|
|
|
|
|
if (item.shouldRender === false) return null;
|
|
|
|
|
return (
|
|
|
|
|
<CustomMenu.MenuItem
|
|
|
|
|
key={item.key}
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
item.action();
|
|
|
|
|
}}
|
|
|
|
|
className={cn(
|
|
|
|
|
"flex items-center gap-2",
|
|
|
|
|
{
|
|
|
|
|
"text-custom-text-400": item.disabled,
|
|
|
|
|
},
|
|
|
|
|
item.className
|
|
|
|
|
)}
|
2024-05-02 16:13:58 +05:30
|
|
|
disabled={item.disabled}
|
2024-04-30 18:59:07 +05:30
|
|
|
>
|
|
|
|
|
{item.icon && <item.icon className={cn("h-3 w-3", item.iconClassName)} />}
|
|
|
|
|
<div>
|
|
|
|
|
<h5>{item.title}</h5>
|
|
|
|
|
{item.description && (
|
|
|
|
|
<p
|
|
|
|
|
className={cn("text-custom-text-300 whitespace-pre-line", {
|
|
|
|
|
"text-custom-text-400": item.disabled,
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
{item.description}
|
2024-03-20 21:02:58 +05:30
|
|
|
</p>
|
2024-04-30 18:59:07 +05:30
|
|
|
)}
|
2024-03-20 21:02:58 +05:30
|
|
|
</div>
|
2024-04-30 18:59:07 +05:30
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2024-03-20 21:02:58 +05:30
|
|
|
</CustomMenu>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
});
|