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 READONLY_PROJECT = 'ReadOnlyProject',
71 PROJECT_ADMIN = "ProjectAdmin",
72 RESOURCE = "Resource",
73 FAVORITE = "Favorite",
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',
86 PROCESS_ADMIN = 'ProcessAdmin',
87 PROCESS_RESOURCE = 'ProcessResource',
88 PROCESS_LOGS = "ProcessLogs",
89 REPOSITORY = "Repository",
91 VIRTUAL_MACHINE = "VirtualMachine",
92 KEEP_SERVICE = "KeepService",
96 GROUP_MEMBER = "GroupMember",