|
| 1 | +import React, { useCallback, useMemo, useState } from "react"; |
| 2 | +import { |
| 3 | + getHasDeleteActionPermission, |
| 4 | + getHasManageActionPermission, |
| 5 | +} from "ee/utils/BusinessFeatures/permissionPageHelpers"; |
| 6 | +import { useFeatureFlag } from "utils/hooks/useFeatureFlag"; |
| 7 | +import { FEATURE_FLAG } from "ee/entities/FeatureFlag"; |
| 8 | +import { usePluginActionContext } from "PluginActionEditor"; |
| 9 | +import { |
| 10 | + MenuItem, |
| 11 | + MenuSub, |
| 12 | + MenuSubContent, |
| 13 | + MenuSubTrigger, |
| 14 | +} from "@appsmith/ads"; |
| 15 | +import { |
| 16 | + CONFIRM_CONTEXT_DELETE, |
| 17 | + CONTEXT_COPY, |
| 18 | + CONTEXT_DELETE, |
| 19 | + CONTEXT_MOVE, |
| 20 | + createMessage, |
| 21 | +} from "ee/constants/messages"; |
| 22 | +import { useDispatch, useSelector } from "react-redux"; |
| 23 | +import { |
| 24 | + copyActionRequest, |
| 25 | + deleteAction, |
| 26 | + moveActionRequest, |
| 27 | +} from "actions/pluginActionActions"; |
| 28 | +import { getCurrentPageId } from "selectors/editorSelectors"; |
| 29 | +import type { Page } from "entities/Page"; |
| 30 | +import { getPageList } from "ee/selectors/entitiesSelector"; |
| 31 | +import { ConvertToModuleCTA } from "./ConvertToModule"; |
| 32 | + |
| 33 | +const PageMenuItem = (props: { |
| 34 | + page: Page; |
| 35 | + onSelect: (id: string) => void; |
| 36 | +}) => { |
| 37 | + const handleOnSelect = useCallback(() => { |
| 38 | + props.onSelect(props.page.pageId); |
| 39 | + }, [props]); |
| 40 | + return <MenuItem onSelect={handleOnSelect}>{props.page.pageName}</MenuItem>; |
| 41 | +}; |
| 42 | + |
| 43 | +const Copy = () => { |
| 44 | + const menuPages = useSelector(getPageList); |
| 45 | + const { action } = usePluginActionContext(); |
| 46 | + const dispatch = useDispatch(); |
| 47 | + |
| 48 | + const copyActionToPage = useCallback( |
| 49 | + (pageId: string) => |
| 50 | + dispatch( |
| 51 | + copyActionRequest({ |
| 52 | + id: action.id, |
| 53 | + destinationPageId: pageId, |
| 54 | + name: action.name, |
| 55 | + }), |
| 56 | + ), |
| 57 | + [action.id, action.name, dispatch], |
| 58 | + ); |
| 59 | + |
| 60 | + return ( |
| 61 | + <MenuSub> |
| 62 | + <MenuSubTrigger startIcon="duplicate"> |
| 63 | + {createMessage(CONTEXT_COPY)} |
| 64 | + </MenuSubTrigger> |
| 65 | + <MenuSubContent> |
| 66 | + {menuPages.map((page) => { |
| 67 | + return ( |
| 68 | + <PageMenuItem |
| 69 | + key={page.basePageId} |
| 70 | + onSelect={copyActionToPage} |
| 71 | + page={page} |
| 72 | + /> |
| 73 | + ); |
| 74 | + })} |
| 75 | + </MenuSubContent> |
| 76 | + </MenuSub> |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +const Move = () => { |
| 81 | + const dispatch = useDispatch(); |
| 82 | + const { action } = usePluginActionContext(); |
| 83 | + |
| 84 | + const currentPageId = useSelector(getCurrentPageId); |
| 85 | + const allPages = useSelector(getPageList); |
| 86 | + const menuPages = useMemo(() => { |
| 87 | + return allPages.filter((page) => page.pageId !== currentPageId); |
| 88 | + }, [allPages, currentPageId]); |
| 89 | + |
| 90 | + const moveActionToPage = useCallback( |
| 91 | + (destinationPageId: string) => |
| 92 | + dispatch( |
| 93 | + moveActionRequest({ |
| 94 | + id: action.id, |
| 95 | + destinationPageId, |
| 96 | + originalPageId: currentPageId, |
| 97 | + name: action.name, |
| 98 | + }), |
| 99 | + ), |
| 100 | + [dispatch, action.id, action.name, currentPageId], |
| 101 | + ); |
| 102 | + |
| 103 | + return ( |
| 104 | + <MenuSub> |
| 105 | + <MenuSubTrigger startIcon="swap-horizontal"> |
| 106 | + {createMessage(CONTEXT_MOVE)} |
| 107 | + </MenuSubTrigger> |
| 108 | + <MenuSubContent> |
| 109 | + {menuPages.length > 1 ? ( |
| 110 | + menuPages.map((page) => { |
| 111 | + return ( |
| 112 | + <PageMenuItem |
| 113 | + key={page.basePageId} |
| 114 | + onSelect={moveActionToPage} |
| 115 | + page={page} |
| 116 | + /> |
| 117 | + ); |
| 118 | + }) |
| 119 | + ) : ( |
| 120 | + <MenuItem key="no-pages">No pages</MenuItem> |
| 121 | + )} |
| 122 | + </MenuSubContent> |
| 123 | + </MenuSub> |
| 124 | + ); |
| 125 | +}; |
| 126 | + |
| 127 | +const Delete = () => { |
| 128 | + const dispatch = useDispatch(); |
| 129 | + const { action } = usePluginActionContext(); |
| 130 | + |
| 131 | + const [confirmDelete, setConfirmDelete] = useState(false); |
| 132 | + |
| 133 | + const deleteActionFromPage = useCallback(() => { |
| 134 | + dispatch(deleteAction({ id: action.id, name: action.name })); |
| 135 | + }, [action.id, action.name, dispatch]); |
| 136 | + |
| 137 | + const handleSelect = useCallback(() => { |
| 138 | + confirmDelete ? deleteActionFromPage() : setConfirmDelete(true); |
| 139 | + }, [confirmDelete, deleteActionFromPage]); |
| 140 | + |
| 141 | + const menuLabel = confirmDelete |
| 142 | + ? createMessage(CONFIRM_CONTEXT_DELETE) |
| 143 | + : createMessage(CONTEXT_DELETE); |
| 144 | + |
| 145 | + return ( |
| 146 | + <MenuItem |
| 147 | + className="t--apiFormDeleteBtn error-menuitem" |
| 148 | + onSelect={handleSelect} |
| 149 | + startIcon="trash" |
| 150 | + > |
| 151 | + {menuLabel} |
| 152 | + </MenuItem> |
| 153 | + ); |
| 154 | +}; |
| 155 | + |
| 156 | +const AppPluginActionMenu = () => { |
| 157 | + const { action } = usePluginActionContext(); |
| 158 | + |
| 159 | + const isFeatureEnabled = useFeatureFlag(FEATURE_FLAG.license_gac_enabled); |
| 160 | + const isChangePermitted = getHasManageActionPermission( |
| 161 | + isFeatureEnabled, |
| 162 | + action.userPermissions, |
| 163 | + ); |
| 164 | + const isDeletePermitted = getHasDeleteActionPermission( |
| 165 | + isFeatureEnabled, |
| 166 | + action?.userPermissions, |
| 167 | + ); |
| 168 | + |
| 169 | + return ( |
| 170 | + <> |
| 171 | + <ConvertToModuleCTA /> |
| 172 | + {isChangePermitted && ( |
| 173 | + <> |
| 174 | + <Copy /> |
| 175 | + <Move /> |
| 176 | + </> |
| 177 | + )} |
| 178 | + {isDeletePermitted && <Delete />} |
| 179 | + </> |
| 180 | + ); |
| 181 | +}; |
| 182 | + |
| 183 | +export default AppPluginActionMenu; |
0 commit comments