17782: Fixes 'react/jsx-no-target-blank' compile warnings.
[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     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_FILE_ITEM = "CollectionFileItem",
80     COLLECTION_DIRECTORY_ITEM = "CollectionDirectoryItem",
81     READONLY_COLLECTION_FILE_ITEM = "ReadOnlyCollectionFileItem",
82     READONLY_COLLECTION_DIRECTORY_ITEM = "ReadOnlyCollectionDirectoryItem",
83     COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
84     COLLECTION = 'Collection',
85     COLLECTION_ADMIN = 'CollectionAdmin',
86     READONLY_COLLECTION = 'ReadOnlyCollection',
87     OLD_VERSION_COLLECTION = 'OldVersionCollection',
88     TRASHED_COLLECTION = 'TrashedCollection',
89     PROCESS = "Process",
90     PROCESS_ADMIN = 'ProcessAdmin',
91     PROCESS_RESOURCE = 'ProcessResource',
92     READONLY_PROCESS_RESOURCE = 'ReadOnlyProcessResource',
93     PROCESS_LOGS = "ProcessLogs",
94     REPOSITORY = "Repository",
95     SSH_KEY = "SshKey",
96     VIRTUAL_MACHINE = "VirtualMachine",
97     KEEP_SERVICE = "KeepService",
98     USER = "User",
99     NODE = "Node",
100     GROUPS = "Group",
101     GROUP_MEMBER = "GroupMember",
102     LINK = "Link",
103 }