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 const filteredItems = getMenuActionSet(resource).map((group) => (group.filter((item) => {
19 if (resource && item.filters) {
20 // Execute all filters on this item, every returns true IFF all filters return true
21 return item.filters.every((filter) => filter(state, resource));
28 anchorEl: resource ? createAnchorAt(position) : undefined,
35 type ActionProps = Pick<ContextMenuProps, "onClose"> & { onItemClick: (item: ContextMenuItem, resource?: ContextMenuResource) => void };
36 const mapDispatchToProps = (dispatch: Dispatch): ActionProps => ({
38 dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
40 onItemClick: (action: ContextMenuAction, resource?: ContextMenuResource) => {
41 dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
43 action.execute(dispatch, resource);
48 const handleItemClick = memoize(
49 (resource: DataProps['resource'], onItemClick: ActionProps['onItemClick']): ContextMenuProps['onItemClick'] =>
51 onItemClick(item, resource);
55 const mergeProps = ({ resource, ...dataProps }: DataProps, actionProps: ActionProps): ContextMenuProps => ({
58 onItemClick: handleItemClick(resource, actionProps.onItemClick)
62 export const ContextMenu = connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenuComponent);
64 const menuActionSets = new Map<string, ContextMenuActionSet>();
66 export const addMenuActionSet = (name: string, itemSet: ContextMenuActionSet) => {
67 const sorted = itemSet.map(items => items.sort(sortByProperty('name')));
68 menuActionSets.set(name, sorted);
71 const emptyActionSet: ContextMenuActionSet = [];
72 const getMenuActionSet = (resource?: ContextMenuResource): ContextMenuActionSet => (
73 resource ? menuActionSets.get(resource.menuKind) || emptyActionSet : emptyActionSet
76 export enum ContextMenuKind {
77 API_CLIENT_AUTHORIZATION = "ApiClientAuthorization",
78 ROOT_PROJECT = "RootProject",
80 FILTER_GROUP = "FilterGroup",
81 READONLY_PROJECT = 'ReadOnlyProject',
82 FROZEN_PROJECT = 'FrozenProject',
83 PROJECT_ADMIN = "ProjectAdmin",
84 FILTER_GROUP_ADMIN = "FilterGroupAdmin",
85 RESOURCE = "Resource",
86 FAVORITE = "Favorite",
88 COLLECTION_FILES = "CollectionFiles",
89 READONLY_COLLECTION_FILES = "ReadOnlyCollectionFiles",
90 COLLECTION_FILE_ITEM = "CollectionFileItem",
91 COLLECTION_DIRECTORY_ITEM = "CollectionDirectoryItem",
92 READONLY_COLLECTION_FILE_ITEM = "ReadOnlyCollectionFileItem",
93 READONLY_COLLECTION_DIRECTORY_ITEM = "ReadOnlyCollectionDirectoryItem",
94 COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
95 COLLECTION = 'Collection',
96 COLLECTION_ADMIN = 'CollectionAdmin',
97 READONLY_COLLECTION = 'ReadOnlyCollection',
98 OLD_VERSION_COLLECTION = 'OldVersionCollection',
99 TRASHED_COLLECTION = 'TrashedCollection',
101 PROCESS_ADMIN = 'ProcessAdmin',
102 PROCESS_RESOURCE = 'ProcessResource',
103 READONLY_PROCESS_RESOURCE = 'ReadOnlyProcessResource',
104 PROCESS_LOGS = "ProcessLogs",
105 REPOSITORY = "Repository",
107 VIRTUAL_MACHINE = "VirtualMachine",
108 KEEP_SERVICE = "KeepService",
111 GROUP_MEMBER = "GroupMember",
112 PERMISSION_EDIT = "PermissionEdit",