1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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;
18 anchorEl: resource ? createAnchorAt(position) : undefined,
19 items: getMenuActionSet(resource),
25 type ActionProps = Pick<ContextMenuProps, "onClose"> & { onItemClick: (item: ContextMenuItem, resource?: ContextMenuResource) => void };
26 const mapDispatchToProps = (dispatch: Dispatch): ActionProps => ({
28 dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
30 onItemClick: (action: ContextMenuAction, resource?: ContextMenuResource) => {
31 dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
33 action.execute(dispatch, resource);
38 const handleItemClick = memoize(
39 (resource: DataProps['resource'], onItemClick: ActionProps['onItemClick']): ContextMenuProps['onItemClick'] =>
41 onItemClick(item, resource);
45 const mergeProps = ({ resource, ...dataProps }: DataProps, actionProps: ActionProps): ContextMenuProps => ({
48 onItemClick: handleItemClick(resource, actionProps.onItemClick)
52 export const ContextMenu = connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenuComponent);
54 const menuActionSets = new Map<string, ContextMenuActionSet>();
56 export const addMenuActionSet = (name: string, itemSet: ContextMenuActionSet) => {
57 const sorted = itemSet.map(items => items.sort(sortByProperty('name')));
58 menuActionSets.set(name, sorted);
61 const emptyActionSet: ContextMenuActionSet = [];
62 const getMenuActionSet = (resource?: ContextMenuResource): ContextMenuActionSet => {
63 return resource ? menuActionSets.get(resource.menuKind) || emptyActionSet : emptyActionSet;
66 export enum ContextMenuKind {
67 API_CLIENT_AUTHORIZATION = "ApiClientAuthorization",
68 ROOT_PROJECT = "RootProject",
70 FILTER_GROUP = "FilterGroup",
71 READONLY_PROJECT = 'ReadOnlyProject',
72 PROJECT_ADMIN = "ProjectAdmin",
73 FILTER_GROUP_ADMIN = "FilterGroupAdmin",
74 RESOURCE = "Resource",
75 FAVORITE = "Favorite",
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',
90 PROCESS_ADMIN = 'ProcessAdmin',
91 PROCESS_RESOURCE = 'ProcessResource',
92 READONLY_PROCESS_RESOURCE = 'ReadOnlyProcessResource',
93 PROCESS_LOGS = "ProcessLogs",
94 REPOSITORY = "Repository",
96 VIRTUAL_MACHINE = "VirtualMachine",
97 KEEP_SERVICE = "KeepService",
101 GROUP_MEMBER = "GroupMember",