|
| 1 | +// types |
| 2 | +import type { ICycle, IModule, IProjectView, IWorkspaceView } from "@plane/types"; |
| 3 | +import type { TContextMenuItem } from "@plane/ui"; |
| 4 | +// hooks |
| 5 | +import { useQuickActionsFactory } from "@/plane-web/components/common/quick-actions-factory"; |
| 6 | + |
| 7 | +// Types |
| 8 | +interface UseCycleMenuItemsProps { |
| 9 | + cycleDetails: ICycle | undefined; |
| 10 | + isEditingAllowed: boolean; |
| 11 | + workspaceSlug: string; |
| 12 | + projectId: string; |
| 13 | + cycleId: string; |
| 14 | + handleEdit: () => void; |
| 15 | + handleArchive: () => void; |
| 16 | + handleRestore: () => void; |
| 17 | + handleDelete: () => void; |
| 18 | + handleCopyLink: () => void; |
| 19 | + handleOpenInNewTab: () => void; |
| 20 | +} |
| 21 | + |
| 22 | +interface UseModuleMenuItemsProps { |
| 23 | + moduleDetails: IModule | undefined; |
| 24 | + isEditingAllowed: boolean; |
| 25 | + workspaceSlug: string; |
| 26 | + projectId: string; |
| 27 | + moduleId: string; |
| 28 | + handleEdit: () => void; |
| 29 | + handleArchive: () => void; |
| 30 | + handleRestore: () => void; |
| 31 | + handleDelete: () => void; |
| 32 | + handleCopyLink: () => void; |
| 33 | + handleOpenInNewTab: () => void; |
| 34 | +} |
| 35 | + |
| 36 | +interface UseViewMenuItemsProps { |
| 37 | + isOwner: boolean; |
| 38 | + isAdmin: boolean; |
| 39 | + workspaceSlug: string; |
| 40 | + projectId?: string; |
| 41 | + view: IProjectView | IWorkspaceView; |
| 42 | + handleEdit: () => void; |
| 43 | + handleDelete: () => void; |
| 44 | + handleCopyLink: () => void; |
| 45 | + handleOpenInNewTab: () => void; |
| 46 | +} |
| 47 | + |
| 48 | +interface UseLayoutMenuItemsProps { |
| 49 | + workspaceSlug: string; |
| 50 | + projectId: string; |
| 51 | + storeType: "PROJECT" | "EPIC"; |
| 52 | + handleCopyLink: () => void; |
| 53 | + handleOpenInNewTab: () => void; |
| 54 | +} |
| 55 | + |
| 56 | +type MenuResult = { |
| 57 | + items: TContextMenuItem[]; |
| 58 | + modals: JSX.Element | null; |
| 59 | +}; |
| 60 | + |
| 61 | +export const useCycleMenuItems = (props: UseCycleMenuItemsProps): MenuResult => { |
| 62 | + const factory = useQuickActionsFactory(); |
| 63 | + const { cycleDetails, isEditingAllowed, ...handlers } = props; |
| 64 | + |
| 65 | + const isArchived = !!cycleDetails?.archived_at; |
| 66 | + const isCompleted = cycleDetails?.status?.toLowerCase() === "completed"; |
| 67 | + |
| 68 | + // Assemble final menu items - order defined here |
| 69 | + const items = [ |
| 70 | + factory.createEditMenuItem(handlers.handleEdit, isEditingAllowed && !isCompleted && !isArchived), |
| 71 | + factory.createOpenInNewTabMenuItem(handlers.handleOpenInNewTab), |
| 72 | + factory.createCopyLinkMenuItem(handlers.handleCopyLink), |
| 73 | + factory.createArchiveMenuItem(handlers.handleArchive, { |
| 74 | + shouldRender: isEditingAllowed && !isArchived, |
| 75 | + disabled: !isCompleted, |
| 76 | + description: isCompleted ? undefined : "Only completed cycles can be archived", |
| 77 | + }), |
| 78 | + factory.createRestoreMenuItem(handlers.handleRestore, isEditingAllowed && isArchived), |
| 79 | + factory.createDeleteMenuItem(handlers.handleDelete, isEditingAllowed && !isCompleted && !isArchived), |
| 80 | + ].filter((item) => item.shouldRender !== false); |
| 81 | + |
| 82 | + return { items, modals: null }; |
| 83 | +}; |
| 84 | + |
| 85 | +export const useModuleMenuItems = (props: UseModuleMenuItemsProps): MenuResult => { |
| 86 | + const factory = useQuickActionsFactory(); |
| 87 | + const { moduleDetails, isEditingAllowed, ...handlers } = props; |
| 88 | + |
| 89 | + const isArchived = !!moduleDetails?.archived_at; |
| 90 | + const moduleState = moduleDetails?.status?.toLocaleLowerCase(); |
| 91 | + const isInArchivableGroup = !!moduleState && ["completed", "cancelled"].includes(moduleState); |
| 92 | + |
| 93 | + // Assemble final menu items - order defined here |
| 94 | + const items = [ |
| 95 | + factory.createEditMenuItem(handlers.handleEdit, isEditingAllowed && !isArchived), |
| 96 | + factory.createOpenInNewTabMenuItem(handlers.handleOpenInNewTab), |
| 97 | + factory.createCopyLinkMenuItem(handlers.handleCopyLink), |
| 98 | + factory.createArchiveMenuItem(handlers.handleArchive, { |
| 99 | + shouldRender: isEditingAllowed && !isArchived, |
| 100 | + disabled: !isInArchivableGroup, |
| 101 | + description: isInArchivableGroup ? undefined : "Only completed or cancelled modules can be archived", |
| 102 | + }), |
| 103 | + factory.createRestoreMenuItem(handlers.handleRestore, isEditingAllowed && isArchived), |
| 104 | + factory.createDeleteMenuItem(handlers.handleDelete, isEditingAllowed && !isArchived), |
| 105 | + ].filter((item) => item.shouldRender !== false); |
| 106 | + |
| 107 | + return { items, modals: null }; |
| 108 | +}; |
| 109 | + |
| 110 | +export const useViewMenuItems = (props: UseViewMenuItemsProps): MenuResult => { |
| 111 | + const factory = useQuickActionsFactory(); |
| 112 | + const { workspaceSlug, isOwner, isAdmin, projectId, view, ...handlers } = props; |
| 113 | + |
| 114 | + if (!view) return { items: [], modals: null }; |
| 115 | + |
| 116 | + // Assemble final menu items - order defined here |
| 117 | + const items = [ |
| 118 | + factory.createEditMenuItem(handlers.handleEdit, isOwner), |
| 119 | + factory.createOpenInNewTabMenuItem(handlers.handleOpenInNewTab), |
| 120 | + factory.createCopyLinkMenuItem(handlers.handleCopyLink), |
| 121 | + factory.createDeleteMenuItem(handlers.handleDelete, isOwner || isAdmin), |
| 122 | + ].filter((item) => item.shouldRender !== false); |
| 123 | + |
| 124 | + return { items, modals: null }; |
| 125 | +}; |
| 126 | + |
| 127 | +export const useLayoutMenuItems = (props: UseLayoutMenuItemsProps): MenuResult => { |
| 128 | + const factory = useQuickActionsFactory(); |
| 129 | + const { ...handlers } = props; |
| 130 | + |
| 131 | + // Assemble final menu items - order defined here |
| 132 | + const items = [ |
| 133 | + factory.createOpenInNewTab(handlers.handleOpenInNewTab), |
| 134 | + factory.createCopyLayoutLinkMenuItem(handlers.handleCopyLink), |
| 135 | + ].filter((item) => item.shouldRender !== false); |
| 136 | + |
| 137 | + return { items, modals: null }; |
| 138 | +}; |
| 139 | + |
| 140 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 141 | +export const useIntakeHeaderMenuItems = (props: { |
| 142 | + workspaceSlug: string; |
| 143 | + projectId: string; |
| 144 | + handleCopyLink: () => void; |
| 145 | +}): MenuResult => ({ items: [], modals: null }); |
0 commit comments