18692: Fixed context menu naming
[arvados.git] / src / views-components / context-menu / context-menu.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from "react-redux";
6 import { RootState } from "store/store";
7 import { contextMenuActions, ContextMenuResource } from "store/context-menu/context-menu-actions";
8 import { ContextMenu as ContextMenuComponent, ContextMenuProps, ContextMenuItem } from "components/context-menu/context-menu";
9 import { createAnchorAt } from "components/popover/helpers";
10 import { ContextMenuActionSet, ContextMenuAction } from "./context-menu-action-set";
11 import { Dispatch } from "redux";
12 import { memoize } from 'lodash';
13 import { sortByProperty } from "common/array-utils";
14 type DataProps = Pick<ContextMenuProps, "anchorEl" | "items" | "open"> & { resource?: ContextMenuResource };
15 const mapStateToProps = (state: RootState): DataProps => {
16     const { open, position, resource } = state.contextMenu;
17
18     const filteredItems = getMenuActionSet(resource).map((group) => (group.filter((item) => {
19         if (resource && item.filters) {
20             // Execute all filters on this item, every returns true IFF all filters return true
21             return item.filters.every((filter) => filter(state, resource));
22         } else {
23             return true;
24         }
25     })));
26
27     return {
28         anchorEl: resource ? createAnchorAt(position) : undefined,
29         items: filteredItems,
30         open,
31         resource
32     };
33 };
34
35 type ActionProps = Pick<ContextMenuProps, "onClose"> & { onItemClick: (item: ContextMenuItem, resource?: ContextMenuResource) => void };
36 const mapDispatchToProps = (dispatch: Dispatch): ActionProps => ({
37     onClose: () => {
38         dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
39     },
40     onItemClick: (action: ContextMenuAction, resource?: ContextMenuResource) => {
41         dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
42         if (resource) {
43             action.execute(dispatch, resource);
44         }
45     }
46 });
47
48 const handleItemClick = memoize(
49     (resource: DataProps['resource'], onItemClick: ActionProps['onItemClick']): ContextMenuProps['onItemClick'] =>
50         item => {
51             onItemClick(item, resource);
52         }
53 );
54
55 const mergeProps = ({ resource, ...dataProps }: DataProps, actionProps: ActionProps): ContextMenuProps => ({
56     ...dataProps,
57     ...actionProps,
58     onItemClick: handleItemClick(resource, actionProps.onItemClick)
59 });
60
61
62 export const ContextMenu = connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenuComponent);
63
64 const menuActionSets = new Map<string, ContextMenuActionSet>();
65
66 export const addMenuActionSet = (name: string, itemSet: ContextMenuActionSet) => {
67     const sorted = itemSet.map(items => items.sort(sortByProperty('name')));
68     menuActionSets.set(name, sorted);
69 };
70
71 const emptyActionSet: ContextMenuActionSet = [];
72 const getMenuActionSet = (resource?: ContextMenuResource): ContextMenuActionSet => (
73     resource ? menuActionSets.get(resource.menuKind) || emptyActionSet : emptyActionSet
74 );
75
76 export enum ContextMenuKind {
77     API_CLIENT_AUTHORIZATION = "ApiClientAuthorization",
78     ROOT_PROJECT = "RootProject",
79     PROJECT = "Project",
80     FILTER_GROUP = "FilterGroup",
81     READONLY_PROJECT = 'ReadOnlyProject',
82     FROZEN_PROJECT = 'FrozenProject',
83     FROZEN_PROJECT_ADMIN = 'FrozenProjectAdmin',
84     PROJECT_ADMIN = "ProjectAdmin",
85     FILTER_GROUP_ADMIN = "FilterGroupAdmin",
86     RESOURCE = "Resource",
87     FAVORITE = "Favorite",
88     TRASH = "Trash",
89     COLLECTION_FILES = "CollectionFiles",
90     READONLY_COLLECTION_FILES = "ReadOnlyCollectionFiles",
91     COLLECTION_FILE_ITEM = "CollectionFileItem",
92     COLLECTION_DIRECTORY_ITEM = "CollectionDirectoryItem",
93     READONLY_COLLECTION_FILE_ITEM = "ReadOnlyCollectionFileItem",
94     READONLY_COLLECTION_DIRECTORY_ITEM = "ReadOnlyCollectionDirectoryItem",
95     COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
96     COLLECTION = 'Collection',
97     COLLECTION_ADMIN = 'CollectionAdmin',
98     READONLY_COLLECTION = 'ReadOnlyCollection',
99     OLD_VERSION_COLLECTION = 'OldVersionCollection',
100     TRASHED_COLLECTION = 'TrashedCollection',
101     PROCESS = "Process",
102     PROCESS_ADMIN = 'ProcessAdmin',
103     PROCESS_RESOURCE = 'ProcessResource',
104     READONLY_PROCESS_RESOURCE = 'ReadOnlyProcessResource',
105     PROCESS_LOGS = "ProcessLogs",
106     REPOSITORY = "Repository",
107     SSH_KEY = "SshKey",
108     VIRTUAL_MACHINE = "VirtualMachine",
109     KEEP_SERVICE = "KeepService",
110     USER = "User",
111     GROUPS = "Group",
112     GROUP_MEMBER = "GroupMember",
113     PERMISSION_EDIT = "PermissionEdit",
114     LINK = "Link",
115     WORKFLOW = "Workflow",
116     SEARCH_RESULTS = "SearchResults"
117 }