1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { connect, Dispatch, DispatchProp } from "react-redux";
6 import { RootState } from "../../store/store";
7 import actions from "../../store/context-menu/context-menu-actions";
8 import ContextMenu, { 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 { ContextMenuItemSet } from "./context-menu-item-set";
12 import { emptyItemSet } from "./empty-item-set";
14 type DataProps = Pick<ContextMenuProps, "anchorEl" | "items"> & { resource?: ContextMenuResource };
15 const mapStateToProps = (state: RootState): DataProps => {
16 const { position, resource } = state.contextMenu;
18 anchorEl: resource ? createAnchorAt(position) : undefined,
19 items: getMenuItemSet(resource).getItems(),
24 type ActionProps = Pick<ContextMenuProps, "onClose"> & { onItemClick: (item: ContextMenuItem, resource?: ContextMenuResource) => void };
25 const mapDispatchToProps = (dispatch: Dispatch): ActionProps => ({
27 dispatch(actions.CLOSE_CONTEXT_MENU());
29 onItemClick: (item: ContextMenuItem, resource?: ContextMenuResource) => {
30 dispatch(actions.CLOSE_CONTEXT_MENU());
32 getMenuItemSet(resource).handleItem(dispatch, item, resource);
37 const mergeProps = ({ resource, ...dataProps }: DataProps, actionProps: ActionProps): ContextMenuProps => ({
40 onItemClick: item => {
41 actionProps.onItemClick(item, resource);
45 export const ContextMenuHOC = connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenu);
47 const menuItemSets = new Map<string, ContextMenuItemSet>();
49 export const addMenuItemsSet = (name: string, itemSet: ContextMenuItemSet) => {
50 menuItemSets.set(name, itemSet);
53 const getMenuItemSet = (resource?: ContextMenuResource): ContextMenuItemSet => {
54 return resource ? menuItemSets.get(resource.kind) || emptyItemSet : emptyItemSet;