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";
13 type DataProps = Pick<ContextMenuProps, "anchorEl" | "items" | "open"> & { resource?: ContextMenuResource };
14 const mapStateToProps = (state: RootState): DataProps => {
15 const { open, position, resource } = state.contextMenu;
17 anchorEl: resource ? createAnchorAt(position) : undefined,
18 items: getMenuActionSet(resource),
24 type ActionProps = Pick<ContextMenuProps, "onClose"> & { onItemClick: (item: ContextMenuItem, resource?: ContextMenuResource) => void };
25 const mapDispatchToProps = (dispatch: Dispatch): ActionProps => ({
27 dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
29 onItemClick: (action: ContextMenuAction, resource?: ContextMenuResource) => {
30 dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
32 action.execute(dispatch, resource);
37 const mergeProps = ({ resource, ...dataProps }: DataProps, actionProps: ActionProps): ContextMenuProps => ({
40 onItemClick: item => {
41 actionProps.onItemClick(item, resource);
45 export const ContextMenu = connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenuComponent);
47 const menuActionSets = new Map<string, ContextMenuActionSet>();
49 export const addMenuActionSet = (name: string, itemSet: ContextMenuActionSet) => {
50 menuActionSets.set(name, itemSet);
53 const getMenuActionSet = (resource?: ContextMenuResource): ContextMenuActionSet => {
54 return resource ? menuActionSets.get(resource.menuKind) || [] : [];
57 export enum ContextMenuKind {
58 API_CLIENT_AUTHORIZATION = "ApiClientAuthorization",
59 ROOT_PROJECT = "RootProject",
61 RESOURCE = "Resource",
62 FAVORITE = "Favorite",
64 COLLECTION_FILES = "CollectionFiles",
65 COLLECTION_FILES_ITEM = "CollectionFilesItem",
66 COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
67 COLLECTION = 'Collection',
68 COLLECTION_RESOURCE = 'CollectionResource',
69 TRASHED_COLLECTION = 'TrashedCollection',
71 PROCESS_RESOURCE = 'ProcessResource',
72 PROCESS_LOGS = "ProcessLogs",
73 REPOSITORY = "Repository",
75 VIRTUAL_MACHINE = "VirtualMachine",
76 KEEP_SERVICE = "KeepService",
80 GROUP_MEMBER = "GroupMember",