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 } 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 { ContextMenuResource } from "../../store/context-menu/context-menu-reducer";
11 import { ContextMenuActionSet, ContextMenuAction } from "./context-menu-action-set";
12 import { Dispatch } from "redux";
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 mergeProps = ({ resource, ...dataProps }: DataProps, actionProps: ActionProps): ContextMenuProps => ({
41 onItemClick: item => {
42 actionProps.onItemClick(item, resource);
46 export const ContextMenu = connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenuComponent);
48 const menuActionSets = new Map<string, ContextMenuActionSet>();
50 export const addMenuActionSet = (name: string, itemSet: ContextMenuActionSet) => {
51 menuActionSets.set(name, itemSet);
54 const getMenuActionSet = (resource?: ContextMenuResource): ContextMenuActionSet => {
55 return resource ? menuActionSets.get(resource.kind) || [] : [];
58 export enum ContextMenuKind {
59 ROOT_PROJECT = "RootProject",
61 RESOURCE = "Resource",
62 FAVORITE = "Favorite",
63 COLLECTION_FILES = "CollectionFiles",
64 COLLECTION_FILES_ITEM = "CollectionFilesItem",
65 COLLECTION = 'Collection'