17427: Fix test
[arvados-workbench2.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     return {
18         anchorEl: resource ? createAnchorAt(position) : undefined,
19         items: getMenuActionSet(resource),
20         open,
21         resource
22     };
23 };
24
25 type ActionProps = Pick<ContextMenuProps, "onClose"> & { onItemClick: (item: ContextMenuItem, resource?: ContextMenuResource) => void };
26 const mapDispatchToProps = (dispatch: Dispatch): ActionProps => ({
27     onClose: () => {
28         dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
29     },
30     onItemClick: (action: ContextMenuAction, resource?: ContextMenuResource) => {
31         dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
32         if (resource) {
33             action.execute(dispatch, resource);
34         }
35     }
36 });
37
38 const handleItemClick = memoize(
39     (resource: DataProps['resource'], onItemClick: ActionProps['onItemClick']): ContextMenuProps['onItemClick'] =>
40         item => {
41             onItemClick(item, resource);
42         }
43 );
44
45 const mergeProps = ({ resource, ...dataProps }: DataProps, actionProps: ActionProps): ContextMenuProps => ({
46     ...dataProps,
47     ...actionProps,
48     onItemClick: handleItemClick(resource, actionProps.onItemClick)
49 });
50
51
52 export const ContextMenu = connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenuComponent);
53
54 const menuActionSets = new Map<string, ContextMenuActionSet>();
55
56 export const addMenuActionSet = (name: string, itemSet: ContextMenuActionSet) => {
57     const sorted = itemSet.map(items => items.sort(sortByProperty('name')));
58     menuActionSets.set(name, sorted);
59 };
60
61 const emptyActionSet: ContextMenuActionSet = [];
62 const getMenuActionSet = (resource?: ContextMenuResource): ContextMenuActionSet => {
63     return resource ? menuActionSets.get(resource.menuKind) || emptyActionSet : emptyActionSet;
64 };
65
66 export enum ContextMenuKind {
67     API_CLIENT_AUTHORIZATION = "ApiClientAuthorization",
68     ROOT_PROJECT = "RootProject",
69     PROJECT = "Project",
70     FILTER_GROUP = "FilterGroup",
71     READONLY_PROJECT = 'ReadOnlyProject',
72     PROJECT_ADMIN = "ProjectAdmin",
73     FILTER_GROUP_ADMIN = "FilterGroupAdmin",
74     RESOURCE = "Resource",
75     FAVORITE = "Favorite",
76     TRASH = "Trash",
77     COLLECTION_FILES = "CollectionFiles",
78     READONLY_COLLECTION_FILES = "ReadOnlyCollectionFiles",
79     COLLECTION_FILES_ITEM = "CollectionFilesItem",
80     READONLY_COLLECTION_FILES_ITEM = "ReadOnlyCollectionFilesItem",
81     COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
82     COLLECTION = 'Collection',
83     COLLECTION_ADMIN = 'CollectionAdmin',
84     READONLY_COLLECTION = 'ReadOnlyCollection',
85     OLD_VERSION_COLLECTION = 'OldVersionCollection',
86     TRASHED_COLLECTION = 'TrashedCollection',
87     PROCESS = "Process",
88     PROCESS_ADMIN = 'ProcessAdmin',
89     PROCESS_RESOURCE = 'ProcessResource',
90     READONLY_PROCESS_RESOURCE = 'ReadOnlyProcessResource',
91     PROCESS_LOGS = "ProcessLogs",
92     REPOSITORY = "Repository",
93     SSH_KEY = "SshKey",
94     VIRTUAL_MACHINE = "VirtualMachine",
95     KEEP_SERVICE = "KeepService",
96     USER = "User",
97     NODE = "Node",
98     GROUPS = "Group",
99     GROUP_MEMBER = "GroupMember",
100     LINK = "Link",
101 }