Merge branch '17098-old-version-as-head'
[arvados.git] / src / views-components / context-menu / context-menu.tsx
index 21d851df30ce75a797fb7ed36adf4b82b5d71cd7..219913cdd13ce4a2549779159a8ab8f62a4be9c7 100644 (file)
@@ -2,73 +2,97 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { connect, Dispatch, DispatchProp } from "react-redux";
-import { RootState } from "../../store/store";
-import actions from "../../store/context-menu/context-menu-actions";
-import ContextMenu, { ContextMenuAction, ContextMenuProps } from "../../components/context-menu/context-menu";
-import { createAnchorAt } from "../../components/popover/helpers";
-import projectActions from "../../store/project/project-action";
-import { ContextMenuResource } from "../../store/context-menu/context-menu-reducer";
-
-
-type DataProps = Pick<ContextMenuProps, "anchorEl" | "actions"> & { resource?: ContextMenuResource };
+import { connect } from "react-redux";
+import { RootState } from "~/store/store";
+import { contextMenuActions, ContextMenuResource } from "~/store/context-menu/context-menu-actions";
+import { ContextMenu as ContextMenuComponent, ContextMenuProps, ContextMenuItem } from "~/components/context-menu/context-menu";
+import { createAnchorAt } from "~/components/popover/helpers";
+import { ContextMenuActionSet, ContextMenuAction } from "./context-menu-action-set";
+import { Dispatch } from "redux";
+import { memoize } from 'lodash';
+import { sortByProperty } from "~/common/array-utils";
+type DataProps = Pick<ContextMenuProps, "anchorEl" | "items" | "open"> & { resource?: ContextMenuResource };
 const mapStateToProps = (state: RootState): DataProps => {
-    const { position, resource } = state.contextMenu;
+    const { open, position, resource } = state.contextMenu;
     return {
         anchorEl: resource ? createAnchorAt(position) : undefined,
-        actions: contextMenuActions,
+        items: getMenuActionSet(resource),
+        open,
         resource
     };
 };
 
-type ActionProps = Pick<ContextMenuProps, "onClose"> & {onActionClick: (action: ContextMenuAction, resource?: ContextMenuResource) => void};
+type ActionProps = Pick<ContextMenuProps, "onClose"> & { onItemClick: (item: ContextMenuItem, resource?: ContextMenuResource) => void };
 const mapDispatchToProps = (dispatch: Dispatch): ActionProps => ({
     onClose: () => {
-        dispatch(actions.CLOSE_CONTEXT_MENU());
+        dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
     },
-    onActionClick: (action: ContextMenuAction, resource?: ContextMenuResource) => {
-        dispatch(actions.CLOSE_CONTEXT_MENU());
+    onItemClick: (action: ContextMenuAction, resource?: ContextMenuResource) => {
+        dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
         if (resource) {
-            if (action.name === "New project") {
-                dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: resource.uuid }));
-            }
+            action.execute(dispatch, resource);
         }
     }
 });
 
+const handleItemClick = memoize(
+    (resource: DataProps['resource'], onItemClick: ActionProps['onItemClick']): ContextMenuProps['onItemClick'] =>
+        item => {
+            onItemClick(item, resource);
+        }
+);
+
 const mergeProps = ({ resource, ...dataProps }: DataProps, actionProps: ActionProps): ContextMenuProps => ({
     ...dataProps,
     ...actionProps,
-    onActionClick: (action: ContextMenuAction) => {
-        actionProps.onActionClick(action, resource);
-    }
+    onItemClick: handleItemClick(resource, actionProps.onItemClick)
 });
 
-export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenu);
 
-const contextMenuActions = [[{
-    icon: "fas fa-plus fa-fw",
-    name: "New project"
-}, {
-    icon: "fas fa-users fa-fw",
-    name: "Share"
-}, {
-    icon: "fas fa-sign-out-alt fa-fw",
-    name: "Move to"
-}, {
-    icon: "fas fa-star fa-fw",
-    name: "Add to favourite"
-}, {
-    icon: "fas fa-edit fa-fw",
-    name: "Rename"
-}, {
-    icon: "fas fa-copy fa-fw",
-    name: "Make a copy"
-}, {
-    icon: "fas fa-download fa-fw",
-    name: "Download"
-}], [{
-    icon: "fas fa-trash-alt fa-fw",
-    name: "Remove"
+export const ContextMenu = connect(mapStateToProps, mapDispatchToProps, mergeProps)(ContextMenuComponent);
+
+const menuActionSets = new Map<string, ContextMenuActionSet>();
+
+export const addMenuActionSet = (name: string, itemSet: ContextMenuActionSet) => {
+    const sorted = itemSet.map(items => items.sort(sortByProperty('name')));
+    menuActionSets.set(name, sorted);
+};
+
+const emptyActionSet: ContextMenuActionSet = [];
+const getMenuActionSet = (resource?: ContextMenuResource): ContextMenuActionSet => {
+    return resource ? menuActionSets.get(resource.menuKind) || emptyActionSet : emptyActionSet;
+};
+
+export enum ContextMenuKind {
+    API_CLIENT_AUTHORIZATION = "ApiClientAuthorization",
+    ROOT_PROJECT = "RootProject",
+    PROJECT = "Project",
+    READONLY_PROJECT = 'ReadOnlyProject',
+    PROJECT_ADMIN = "ProjectAdmin",
+    RESOURCE = "Resource",
+    FAVORITE = "Favorite",
+    TRASH = "Trash",
+    COLLECTION_FILES = "CollectionFiles",
+    READONLY_COLLECTION_FILES = "ReadOnlyCollectionFiles",
+    COLLECTION_FILES_ITEM = "CollectionFilesItem",
+    READONLY_COLLECTION_FILES_ITEM = "ReadOnlyCollectionFilesItem",
+    COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
+    COLLECTION = 'Collection',
+    COLLECTION_ADMIN = 'CollectionAdmin',
+    READONLY_COLLECTION = 'ReadOnlyCollection',
+    OLD_VERSION_COLLECTION = 'OldVersionCollection',
+    TRASHED_COLLECTION = 'TrashedCollection',
+    PROCESS = "Process",
+    PROCESS_ADMIN = 'ProcessAdmin',
+    PROCESS_RESOURCE = 'ProcessResource',
+    PROCESS_LOGS = "ProcessLogs",
+    REPOSITORY = "Repository",
+    SSH_KEY = "SshKey",
+    VIRTUAL_MACHINE = "VirtualMachine",
+    KEEP_SERVICE = "KeepService",
+    USER = "User",
+    NODE = "Node",
+    GROUPS = "Group",
+    GROUP_MEMBER = "GroupMember",
+    LINK = "Link",
 }
-]];
\ No newline at end of file