Extract context-menu view component
[arvados-workbench2.git] / src / views-components / context-menu / context-menu.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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, { ContextMenuAction, ContextMenuProps } from "../../components/context-menu/context-menu";
9 import { createAnchorAt } from "../../components/popover/helpers";
10 import projectActions from "../../store/project/project-action";
11 import { ContextMenuResource } from "../../store/context-menu/context-menu-reducer";
12
13
14 type DataProps = Pick<ContextMenuProps, "anchorEl" | "actions"> & { resource?: ContextMenuResource };
15 const mapStateToProps = (state: RootState): DataProps => {
16     const { position, resource } = state.contextMenu;
17     return {
18         anchorEl: resource ? createAnchorAt(position) : undefined,
19         actions: contextMenuActions,
20         resource
21     };
22 };
23
24 type ActionProps = Pick<ContextMenuProps, "onClose"> & {onActionClick: (action: ContextMenuAction, resource?: ContextMenuResource) => void};
25 const mapDispatchToProps = (dispatch: Dispatch): ActionProps => ({
26     onClose: () => {
27         dispatch(actions.CLOSE_CONTEXT_MENU());
28     },
29     onActionClick: (action: ContextMenuAction, resource?: ContextMenuResource) => {
30         dispatch(actions.CLOSE_CONTEXT_MENU());
31         if (resource) {
32             if (action.name === "New project") {
33                 dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: resource.uuid }));
34             }
35         }
36     }
37 });
38
39 const mergeProps = ({ resource, ...dataProps }: DataProps, actionProps: ActionProps): ContextMenuProps => ({
40     ...dataProps,
41     ...actionProps,
42     onActionClick: (action: ContextMenuAction) => {
43         actionProps.onActionClick(action, resource);
44     }
45 });
46
47 export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenu);
48
49 const contextMenuActions = [[{
50     icon: "fas fa-plus fa-fw",
51     name: "New project"
52 }, {
53     icon: "fas fa-users fa-fw",
54     name: "Share"
55 }, {
56     icon: "fas fa-sign-out-alt fa-fw",
57     name: "Move to"
58 }, {
59     icon: "fas fa-star fa-fw",
60     name: "Add to favourite"
61 }, {
62     icon: "fas fa-edit fa-fw",
63     name: "Rename"
64 }, {
65     icon: "fas fa-copy fa-fw",
66     name: "Make a copy"
67 }, {
68     icon: "fas fa-download fa-fw",
69     name: "Download"
70 }], [{
71     icon: "fas fa-trash-alt fa-fw",
72     name: "Remove"
73 }
74 ]];