16622: Tweak icon & titles
[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     READONLY_PROJECT = 'ReadOnlyProject',
71     PROJECT_ADMIN = "ProjectAdmin",
72     RESOURCE = "Resource",
73     FAVORITE = "Favorite",
74     TRASH = "Trash",
75     COLLECTION_FILES = "CollectionFiles",
76     READONLY_COLLECTION_FILES = "ReadOnlyCollectionFiles",
77     COLLECTION_FILES_ITEM = "CollectionFilesItem",
78     READONLY_COLLECTION_FILES_ITEM = "ReadOnlyCollectionFilesItem",
79     COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
80     COLLECTION = 'Collection',
81     COLLECTION_ADMIN = 'CollectionAdmin',
82     READONLY_COLLECTION = 'ReadOnlyCollection',
83     OLD_VERSION_COLLECTION = 'OldVersionCollection',
84     TRASHED_COLLECTION = 'TrashedCollection',
85     PROCESS = "Process",
86     PROCESS_ADMIN = 'ProcessAdmin',
87     PROCESS_RESOURCE = 'ProcessResource',
88     PROCESS_LOGS = "ProcessLogs",
89     REPOSITORY = "Repository",
90     SSH_KEY = "SshKey",
91     VIRTUAL_MACHINE = "VirtualMachine",
92     KEEP_SERVICE = "KeepService",
93     USER = "User",
94     NODE = "Node",
95     GROUPS = "Group",
96     GROUP_MEMBER = "GroupMember",
97     LINK = "Link",
98 }