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