15768: removed copy-and-rerun, minor cleanup Arvados-DCO-1.1-Signed-off-by: Lisa...
[arvados.git] / src / components / multiselectToolbar / MultiselectToolbar.tsx
index 99d15e07d5b86adb264939c0fbc6b46581d74e92..085174dd142cf6193ddfcea41a1575af5d107764 100644 (file)
@@ -2,39 +2,39 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import React from 'react';
-import { connect } from 'react-redux';
-import { StyleRulesCallback, withStyles, WithStyles, Toolbar, Tooltip, IconButton } from '@material-ui/core';
-import { ArvadosTheme } from 'common/custom-theme';
-import { RootState } from 'store/store';
-import { Dispatch } from 'redux';
-import { TCheckedList } from 'components/data-table/data-table';
-import { ContextMenuResource } from 'store/context-menu/context-menu-actions';
-import { Resource, extractUuidKind } from 'models/resource';
-import { getResource } from 'store/resources/resources';
-import { ResourcesState } from 'store/resources/resources';
-import { ContextMenuAction, ContextMenuActionSet } from 'views-components/context-menu/context-menu-action-set';
-import { RestoreFromTrashIcon, TrashIcon } from 'components/icon/icon';
-import { multiselectActionsFilters, TMultiselectActionsFilters } from './ms-toolbar-action-filters';
-import { kindToActionSet, findActionByName } from './ms-kind-action-differentiator';
-import { toggleTrashAction } from 'views-components/context-menu/action-sets/project-action-set';
-
-type CssRules = 'root' | 'button';
+import React from "react";
+import { connect } from "react-redux";
+import { StyleRulesCallback, withStyles, WithStyles, Toolbar, Tooltip, IconButton } from "@material-ui/core";
+import { ArvadosTheme } from "common/custom-theme";
+import { RootState } from "store/store";
+import { Dispatch } from "redux";
+import { TCheckedList } from "components/data-table/data-table";
+import { ContextMenuResource } from "store/context-menu/context-menu-actions";
+import { Resource, extractUuidKind } from "models/resource";
+import { getResource } from "store/resources/resources";
+import { ResourcesState } from "store/resources/resources";
+import { ContextMenuAction, ContextMenuActionSet } from "views-components/context-menu/context-menu-action-set";
+import { RestoreFromTrashIcon, TrashIcon } from "components/icon/icon";
+import { multiselectActionsFilters, TMultiselectActionsFilters, contextMenuActionConsts } from "./ms-toolbar-action-filters";
+import { kindToActionSet, findActionByName } from "./ms-kind-action-differentiator";
+import { toggleTrashAction } from "views-components/context-menu/action-sets/project-action-set";
+import { copyToClipboardAction } from "store/open-in-new-tab/open-in-new-tab.actions";
+
+type CssRules = "root" | "button";
 
 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     root: {
-        display: 'flex',
-        flexDirection: 'row',
+        display: "flex",
+        flexDirection: "row",
         width: 0,
         padding: 0,
-        margin: '1rem auto auto 0.5rem',
-        overflow: 'hidden',
-        transition: 'width 150ms',
-        // borderBottom: '1px solid gray',
+        margin: "1rem auto auto 0.5rem",
+        overflow: "hidden",
+        transition: "width 150ms",
     },
     button: {
-        width: '1rem',
-        margin: 'auto 5px',
+        width: "1rem",
+        margin: "auto 5px",
     },
 });
 
@@ -53,7 +53,7 @@ export const MultiselectToolbar = connect(
         const { classes, checkedList } = props;
         const currentResourceKinds = Array.from(selectedToKindSet(checkedList));
 
-        const currentPathIsTrash = window.location.pathname === '/trash';
+        const currentPathIsTrash = window.location.pathname === "/trash";
         const buttons =
             currentPathIsTrash && selectedToKindSet(checkedList).size
                 ? [toggleTrashAction]
@@ -65,10 +65,10 @@ export const MultiselectToolbar = connect(
                 style={{ width: `${buttons.length * 2.12}rem` }}>
                 {buttons.length ? (
                     buttons.map((btn, i) =>
-                        btn.name === 'ToggleTrashAction' ? (
+                        btn.name === "ToggleTrashAction" ? (
                             <Tooltip
                                 className={classes.button}
-                                title={currentPathIsTrash ? 'Restore' : 'Move to trash'}
+                                title={currentPathIsTrash ? "Restore All" : "Move to trash"}
                                 key={i}
                                 disableFocusListener>
                                 <IconButton onClick={() => props.executeMulti(btn, checkedList, props.resources)}>
@@ -105,7 +105,7 @@ export function selectedToArray(checkedList: TCheckedList): Array<string> {
     return arrayifiedSelectedList;
 }
 
-function selectedToKindSet(checkedList: TCheckedList): Set<string> {
+export function selectedToKindSet(checkedList: TCheckedList): Set<string> {
     const setifiedList = new Set<string>();
     for (const [key, value] of Object.entries(checkedList)) {
         if (value === true) {
@@ -115,6 +115,16 @@ function selectedToKindSet(checkedList: TCheckedList): Set<string> {
     return setifiedList;
 }
 
+function groupByKind(checkedList: TCheckedList, resources: ResourcesState): Record<string, ContextMenuResource[]> {
+    const result = {};
+    selectedToArray(checkedList).forEach(uuid => {
+        const resource = getResource(uuid)(resources) as Resource;
+        if (!result[resource.kind]) result[resource.kind] = [];
+        result[resource.kind].push(resource);
+    });
+    return result;
+}
+
 function filterActions(actionArray: ContextMenuActionSet, filters: Set<string>): Array<ContextMenuAction> {
     return actionArray[0].filter(action => filters.has(action.name as string));
 }
@@ -138,7 +148,7 @@ function selectActionsByKind(currentResourceKinds: Array<string>, filterSet: TMu
 
     const filteredNameSet = allFiltersArray.map(filterArray => {
         const resultSet = new Set();
-        filterArray.forEach(action => resultSet.add(action.name || ''));
+        filterArray.forEach(action => resultSet.add(action.name || ""));
         return resultSet;
     });
 
@@ -150,8 +160,8 @@ function selectActionsByKind(currentResourceKinds: Array<string>, filterSet: TMu
     });
 
     return filteredResult.sort((a, b) => {
-        const nameA = a.name || '';
-        const nameB = b.name || '';
+        const nameA = a.name || "";
+        const nameB = b.name || "";
         if (nameA < nameB) {
             return -1;
         }
@@ -177,23 +187,28 @@ function mapDispatchToProps(dispatch: Dispatch) {
     return {
         executeMulti: (selectedAction: ContextMenuAction, checkedList: TCheckedList, resources: ResourcesState): void => {
             const kindGroups = groupByKind(checkedList, resources);
-            for (const kind in kindGroups) {
-                const actionSet = kindToActionSet[kind];
-                const action = findActionByName(selectedAction.name as string, actionSet);
-
-                if (action) action.execute(dispatch, kindGroups[kind]);
-                // if (action && action.name === 'ToggleTrashAction') action.execute(dispatch, kindGroups[kind]);
+            switch (selectedAction.name) {
+                case contextMenuActionConsts.MOVE_TO:
+                    const firstResource = getResource(selectedToArray(checkedList)[0])(resources) as Resource;
+
+                    const actionSet = kindToActionSet[firstResource.kind];
+                    const action = findActionByName(selectedAction.name as string, actionSet);
+
+                    if (action) action.execute(dispatch, kindGroups[firstResource.kind]);
+                    break;
+                case contextMenuActionConsts.COPY_TO_CLIPBOARD:
+                    const selectedResources = selectedToArray(checkedList).map(uuid => getResource(uuid)(resources));
+                    dispatch<any>(copyToClipboardAction(selectedResources));
+                    break;
+                default:
+                    for (const kind in kindGroups) {
+                        const actionSet = kindToActionSet[kind];
+                        const action = findActionByName(selectedAction.name as string, actionSet);
+
+                        if (action) action.execute(dispatch, kindGroups[kind]);
+                    }
+                    break;
             }
         },
     };
 }
-
-function groupByKind(checkedList: TCheckedList, resources: ResourcesState): Record<string, ContextMenuResource[]> {
-    const result = {};
-    selectedToArray(checkedList).forEach(uuid => {
-        const resource = getResource(uuid)(resources) as Resource;
-        if (!result[resource.kind]) result[resource.kind] = [];
-        result[resource.kind].push(resource);
-    });
-    return result;
-}