X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/ba27bdf218da8894df4ffa6174c67c1fb3e70c81..ca3155fabcfe6dd61b2151c52861b2786e9bec40:/src/views-components/context-menu/context-menu.tsx

diff --git a/src/views-components/context-menu/context-menu.tsx b/src/views-components/context-menu/context-menu.tsx
index b6d2b91b1a..76832ccf85 100644
--- a/src/views-components/context-menu/context-menu.tsx
+++ b/src/views-components/context-menu/context-menu.tsx
@@ -2,26 +2,39 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { connect } from "react-redux";
-import { RootState } from "~/store/store";
-import { contextMenuActions, ContextMenuResource } from "~/store/context-menu/context-menu-actions";
-import { ContextMenu as ContextMenuComponent, ContextMenuProps, ContextMenuItem } from "~/components/context-menu/context-menu";
-import { createAnchorAt } from "~/components/popover/helpers";
-import { ContextMenuActionSet, ContextMenuAction } from "./context-menu-action-set";
-import { Dispatch } from "redux";
-
-type DataProps = Pick<ContextMenuProps, "anchorEl" | "items" | "open"> & { resource?: ContextMenuResource };
+import { connect } from 'react-redux';
+import { RootState } from 'store/store';
+import { contextMenuActions, ContextMenuResource } from 'store/context-menu/context-menu-actions';
+import { ContextMenu as ContextMenuComponent, ContextMenuProps, ContextMenuItem } from 'components/context-menu/context-menu';
+import { createAnchorAt } from 'components/popover/helpers';
+import { ContextMenuActionSet, ContextMenuAction } from './context-menu-action-set';
+import { Dispatch } from 'redux';
+import { memoize } from 'lodash';
+import { sortByProperty } from 'common/array-utils';
+type DataProps = Pick<ContextMenuProps, 'anchorEl' | 'items' | 'open'> & { resource?: ContextMenuResource };
 const mapStateToProps = (state: RootState): DataProps => {
     const { open, position, resource } = state.contextMenu;
+
+    const filteredItems = getMenuActionSet(resource).map((group) =>
+        group.filter((item) => {
+            if (resource && item.filters) {
+                // Execute all filters on this item, every returns true IFF all filters return true
+                return item.filters.every((filter) => filter(state, resource));
+            } else {
+                return true;
+            }
+        })
+    );
+
     return {
         anchorEl: resource ? createAnchorAt(position) : undefined,
-        items: getMenuActionSet(resource),
+        items: filteredItems,
         open,
-        resource
+        resource,
     };
 };
 
-type ActionProps = Pick<ContextMenuProps, "onClose"> & { onItemClick: (item: ContextMenuItem, resource?: ContextMenuResource) => void };
+type ActionProps = Pick<ContextMenuProps, 'onClose'> & { onItemClick: (item: ContextMenuItem, resource?: ContextMenuResource) => void };
 const mapDispatchToProps = (dispatch: Dispatch): ActionProps => ({
     onClose: () => {
         dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
@@ -29,17 +42,22 @@ const mapDispatchToProps = (dispatch: Dispatch): ActionProps => ({
     onItemClick: (action: ContextMenuAction, resource?: ContextMenuResource) => {
         dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
         if (resource) {
-            action.execute(dispatch, resource);
+            action.execute(dispatch, [resource]);
         }
-    }
+    },
 });
 
+const handleItemClick = memoize(
+    (resource: DataProps['resource'], onItemClick: ActionProps['onItemClick']): ContextMenuProps['onItemClick'] =>
+        (item) => {
+            onItemClick(item, resource);
+        }
+);
+
 const mergeProps = ({ resource, ...dataProps }: DataProps, actionProps: ActionProps): ContextMenuProps => ({
     ...dataProps,
     ...actionProps,
-    onItemClick: item => {
-        actionProps.onItemClick(item, resource);
-    }
+    onItemClick: handleItemClick(resource, actionProps.onItemClick),
 });
 
 export const ContextMenu = connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenuComponent);
@@ -47,26 +65,53 @@ export const ContextMenu = connect(mapStateToProps, mapDispatchToProps, mergePro
 const menuActionSets = new Map<string, ContextMenuActionSet>();
 
 export const addMenuActionSet = (name: string, itemSet: ContextMenuActionSet) => {
-    menuActionSets.set(name, itemSet);
+    const sorted = itemSet.map((items) => items.sort(sortByProperty('name')));
+    menuActionSets.set(name, sorted);
 };
 
-const getMenuActionSet = (resource?: ContextMenuResource): ContextMenuActionSet => {
-    return resource ? menuActionSets.get(resource.menuKind) || [] : [];
-};
+const emptyActionSet: ContextMenuActionSet = [];
+const getMenuActionSet = (resource?: ContextMenuResource): ContextMenuActionSet =>
+    resource ? menuActionSets.get(resource.menuKind) || emptyActionSet : emptyActionSet;
 
 export enum ContextMenuKind {
-    ROOT_PROJECT = "RootProject",
-    PROJECT = "Project",
-    RESOURCE = "Resource",
-    FAVORITE = "Favorite",
-    TRASH = "Trash",
-    COLLECTION_FILES = "CollectionFiles",
-    COLLECTION_FILES_ITEM = "CollectionFilesItem",
-    COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
+    API_CLIENT_AUTHORIZATION = 'ApiClientAuthorization',
+    ROOT_PROJECT = 'RootProject',
+    PROJECT = 'Project',
+    FILTER_GROUP = 'FilterGroup',
+    READONLY_PROJECT = 'ReadOnlyProject',
+    FROZEN_PROJECT = 'FrozenProject',
+    FROZEN_PROJECT_ADMIN = 'FrozenProjectAdmin',
+    PROJECT_ADMIN = 'ProjectAdmin',
+    FILTER_GROUP_ADMIN = 'FilterGroupAdmin',
+    RESOURCE = 'Resource',
+    FAVORITE = 'Favorite',
+    TRASH = 'Trash',
+    COLLECTION_FILES = 'CollectionFiles',
+    READONLY_COLLECTION_FILES = 'ReadOnlyCollectionFiles',
+    COLLECTION_FILE_ITEM = 'CollectionFileItem',
+    COLLECTION_DIRECTORY_ITEM = 'CollectionDirectoryItem',
+    READONLY_COLLECTION_FILE_ITEM = 'ReadOnlyCollectionFileItem',
+    READONLY_COLLECTION_DIRECTORY_ITEM = 'ReadOnlyCollectionDirectoryItem',
+    COLLECTION_FILES_NOT_SELECTED = 'CollectionFilesNotSelected',
     COLLECTION = 'Collection',
-    COLLECTION_RESOURCE = 'CollectionResource',
+    COLLECTION_ADMIN = 'CollectionAdmin',
+    READONLY_COLLECTION = 'ReadOnlyCollection',
+    OLD_VERSION_COLLECTION = 'OldVersionCollection',
     TRASHED_COLLECTION = 'TrashedCollection',
-    PROCESS = "Process",
+    PROCESS = 'Process',
+    PROCESS_ADMIN = 'ProcessAdmin',
     PROCESS_RESOURCE = 'ProcessResource',
-    PROCESS_LOGS = "ProcessLogs"
+    READONLY_PROCESS_RESOURCE = 'ReadOnlyProcessResource',
+    PROCESS_LOGS = 'ProcessLogs',
+    REPOSITORY = 'Repository',
+    SSH_KEY = 'SshKey',
+    VIRTUAL_MACHINE = 'VirtualMachine',
+    KEEP_SERVICE = 'KeepService',
+    USER = 'User',
+    GROUPS = 'Group',
+    GROUP_MEMBER = 'GroupMember',
+    PERMISSION_EDIT = 'PermissionEdit',
+    LINK = 'Link',
+    WORKFLOW = 'Workflow',
+    SEARCH_RESULTS = 'SearchResults',
 }
