X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/577e47a434702a8bdad5669b3d470cfde5241033..e186e23688c92f91e4cbf564dee5018789e0b8ad:/src/components/multiselectToolbar/MultiselectToolbar.tsx

diff --git a/src/components/multiselectToolbar/MultiselectToolbar.tsx b/src/components/multiselectToolbar/MultiselectToolbar.tsx
index 7f48333c96..22a9c7139c 100644
--- a/src/components/multiselectToolbar/MultiselectToolbar.tsx
+++ b/src/components/multiselectToolbar/MultiselectToolbar.tsx
@@ -2,49 +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 { openRemoveProcessDialog, openRemoveManyProcessesDialog } from 'store/processes/processes-actions';
-import { processResourceActionSet } from '../../views-components/context-menu/action-sets/process-resource-action-set';
-import { ContextMenuResource } from 'store/context-menu/context-menu-actions';
-import { Resource, extractUuidKind } from 'models/resource';
-import { openMoveProcessDialog } from 'store/processes/process-move-actions';
-import { openCopyProcessDialog, openCopyManyProcessesDialog } from 'store/processes/process-copy-actions';
-import { getResource } from 'store/resources/resources';
-import { ResourcesState } from 'store/resources/resources';
-import { getProcess } from 'store/processes/process';
-import { CopyProcessDialog, CopyManyProcessesDialog } from 'views-components/dialog-forms/copy-process-dialog';
-import { collectionActionSet } from 'views-components/context-menu/action-sets/collection-action-set';
-import { ContextMenuAction, ContextMenuActionSet } from 'views-components/context-menu/context-menu-action-set';
-import { TrashIcon } from 'components/icon/icon';
-import {
-    multiselectActionsFilters,
-    TMultiselectActionsFilters,
-    contextMenuActionConsts,
-} from './ms-toolbar-action-filters';
-import { kindToActionSet, findActionByName } from './ms-kind-action-differentiator';
-
-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",
     },
 });
 
@@ -63,20 +53,34 @@ export const MultiselectToolbar = connect(
         const { classes, checkedList } = props;
         const currentResourceKinds = Array.from(selectedToKindSet(checkedList));
 
-        const buttons = selectActionsByKind(currentResourceKinds, multiselectActionsFilters);
+        const currentPathIsTrash = window.location.pathname === "/trash";
+        const buttons =
+            currentPathIsTrash && selectedToKindSet(checkedList).size
+                ? [toggleTrashAction]
+                : selectActionsByKind(currentResourceKinds, multiselectActionsFilters);
 
         return (
-            <Toolbar className={classes.root} style={{ width: `${buttons.length * 2.12}rem` }}>
+            <Toolbar
+                className={classes.root}
+                style={{ width: `${buttons.length * 2.12}rem` }}>
                 {buttons.length ? (
                     buttons.map((btn, i) =>
-                        btn.name === 'ToggleTrashAction' ? (
-                            <Tooltip className={classes.button} title={'Move to trash'} key={i} disableFocusListener>
+                        btn.name === "ToggleTrashAction" ? (
+                            <Tooltip
+                                className={classes.button}
+                                title={currentPathIsTrash ? "Restore selected" : "Move to trash"}
+                                key={i}
+                                disableFocusListener>
                                 <IconButton onClick={() => props.executeMulti(btn, checkedList, props.resources)}>
-                                    <TrashIcon />
+                                    {currentPathIsTrash ? <RestoreFromTrashIcon /> : <TrashIcon />}
                                 </IconButton>
                             </Tooltip>
                         ) : (
-                            <Tooltip className={classes.button} title={btn.name} key={i} disableFocusListener>
+                            <Tooltip
+                                className={classes.button}
+                                title={btn.name}
+                                key={i}
+                                disableFocusListener>
                                 <IconButton onClick={() => props.executeMulti(btn, checkedList, props.resources)}>
                                     {btn.icon ? btn.icon({}) : <></>}
                                 </IconButton>
@@ -91,8 +95,7 @@ export const MultiselectToolbar = connect(
     })
 );
 
-//todo: put these all in a /utils
-function selectedToArray(checkedList: TCheckedList): Array<string> {
+export function selectedToArray(checkedList: TCheckedList): Array<string> {
     const arrayifiedSelectedList: Array<string> = [];
     for (const [key, value] of Object.entries(checkedList)) {
         if (value === true) {
@@ -102,7 +105,7 @@ 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) {
@@ -112,53 +115,57 @@ function selectedToKindSet(checkedList: TCheckedList): Set<string> {
     return setifiedList;
 }
 
-//num of currentResourceKinds * num of actions (in ContextMenuActionSet) * num of filters
-//worst case: 14 * n * n -oof
+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));
+    return actionArray[0].filter(action => filters.has(action.name as string));
 }
 
-//this might be the least efficient algorithm I've ever written
 function selectActionsByKind(currentResourceKinds: Array<string>, filterSet: TMultiselectActionsFilters) {
-    let result: Array<ContextMenuAction> = [];
-    const fullFilterArray: ContextMenuAction[][] = [];
-    //start at currentResourceKinds
-    currentResourceKinds.forEach((kind) => {
-        //if there is a matching filter set
+    const rawResult: Set<ContextMenuAction> = new Set();
+    const resultNames = new Set();
+    const allFiltersArray: ContextMenuAction[][] = [];
+    currentResourceKinds.forEach(kind => {
         if (filterSet[kind]) {
-            //filter actions that apply to current kind
             const actions = filterActions(...filterSet[kind]);
-            fullFilterArray.push(actions);
-            //if action name isn't in result, push name
-            actions.forEach((action) => {
-                if (!result.some((item) => item.name === action.name)) {
-                    result.push(action);
+            allFiltersArray.push(actions);
+            actions.forEach(action => {
+                if (!resultNames.has(action.name)) {
+                    rawResult.add(action);
+                    resultNames.add(action.name);
                 }
             });
         }
     });
-    ///take fullFilterSet, make it into an array of string sets
-    const filteredNameSet = fullFilterArray.map((filterArray) => {
+
+    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;
     });
-    //filter results so that the name of each action is present in all of this string arrays
-    const filteredResult = result.filter((action) => {
+
+    const filteredResult = Array.from(rawResult).filter(action => {
         for (let i = 0; i < filteredNameSet.length; i++) {
             if (!filteredNameSet[i].has(action.name)) return false;
         }
         return true;
     });
 
-    //return sorted array of actions
     return filteredResult.sort((a, b) => {
-        a.name = a.name || '';
-        b.name = b.name || '';
-        if (a.name < b.name) {
+        const nameA = a.name || "";
+        const nameB = b.name || "";
+        if (nameA < nameB) {
             return -1;
         }
-        if (a.name > b.name) {
+        if (nameA > nameB) {
             return 1;
         }
         return 0;
@@ -178,17 +185,25 @@ function mapStateToProps(state: RootState) {
 
 function mapDispatchToProps(dispatch: Dispatch) {
     return {
-        executeMulti: (action: ContextMenuAction, checkedList: TCheckedList, resources: ResourcesState) => {
-            selectedToArray(checkedList).forEach((uuid) => {
-                const resource = getResource(uuid)(resources);
-                executeSpecific(dispatch, action.name, resource);
-            });
+        executeMulti: (selectedAction: ContextMenuAction, checkedList: TCheckedList, resources: ResourcesState): void => {
+            const kindGroups = groupByKind(checkedList, resources);
+            switch (selectedAction.name) {
+                case contextMenuActionConsts.MOVE_TO:
+                    const firstResource = getResource(selectedToArray(checkedList)[0])(resources) as Resource;
+                    const action = findActionByName(selectedAction.name as string, kindToActionSet[firstResource.kind]);
+                    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 action = findActionByName(selectedAction.name as string, kindToActionSet[kind]);
+                        if (action) action.execute(dispatch, kindGroups[kind]);
+                    }
+                    break;
+            }
         },
     };
 }
-
-function executeSpecific(dispatch: Dispatch, actionName, resource) {
-    const actionSet = kindToActionSet[resource.kind];
-    const action = findActionByName(actionName, actionSet);
-    if (action) action.execute(dispatch, resource);
-}
