15768: all tests pass Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa.knox@curii.com>
[arvados-workbench2.git] / src / components / multiselect-toolbar / MultiselectToolbar.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from "react";
6 import { connect } from "react-redux";
7 import { StyleRulesCallback, withStyles, WithStyles, Toolbar, Tooltip, IconButton } from "@material-ui/core";
8 import { ArvadosTheme } from "common/custom-theme";
9 import { RootState } from "store/store";
10 import { Dispatch } from "redux";
11 import { TCheckedList } from "components/data-table/data-table";
12 import { ContextMenuResource } from "store/context-menu/context-menu-actions";
13 import { Resource, extractUuidKind } from "models/resource";
14 import { getResource } from "store/resources/resources";
15 import { ResourcesState } from "store/resources/resources";
16 import { ContextMenuAction, ContextMenuActionSet } from "views-components/context-menu/context-menu-action-set";
17 import { RestoreFromTrashIcon, TrashIcon } from "components/icon/icon";
18 import { multiselectActionsFilters, TMultiselectActionsFilters, contextMenuActionConsts } from "./ms-toolbar-action-filters";
19 import { kindToActionSet, findActionByName } from "./ms-kind-action-differentiator";
20 import { msToggleTrashAction } from "views-components/multiselect-toolbar/ms-project-action-set";
21 import { copyToClipboardAction } from "store/open-in-new-tab/open-in-new-tab.actions";
22
23 type CssRules = "root" | "button";
24
25 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
26     root: {
27         display: "flex",
28         flexDirection: "row",
29         width: 0,
30         padding: 0,
31         margin: "1rem auto auto 0.5rem",
32         overflow: "hidden",
33         transition: "width 150ms",
34     },
35     button: {
36         width: "1rem",
37         margin: "auto 5px",
38     },
39 });
40
41 export type MultiselectToolbarProps = {
42     checkedList: TCheckedList;
43     resources: ResourcesState;
44     executeMulti: (action: ContextMenuAction, checkedList: TCheckedList, resources: ResourcesState) => void;
45 };
46
47 export const MultiselectToolbar = connect(
48     mapStateToProps,
49     mapDispatchToProps
50 )(
51     withStyles(styles)((props: MultiselectToolbarProps & WithStyles<CssRules>) => {
52         const { classes, checkedList } = props;
53         const currentResourceKinds = Array.from(selectedToKindSet(checkedList));
54
55         const currentPathIsTrash = window.location.pathname === "/trash";
56         const buttons =
57             currentPathIsTrash && selectedToKindSet(checkedList).size
58                 ? [msToggleTrashAction]
59                 : selectActionsByKind(currentResourceKinds, multiselectActionsFilters);
60
61         return (
62             <React.Fragment>
63                 <Toolbar
64                     className={classes.root}
65                     style={{ width: `${buttons.length * 2.12}rem` }}>
66                     {buttons.length ? (
67                         buttons.map((btn, i) =>
68                             btn.name === "ToggleTrashAction" ? (
69                                 <Tooltip
70                                     className={classes.button}
71                                     title={currentPathIsTrash ? "Restore selected" : "Move to trash"}
72                                     key={i}
73                                     disableFocusListener>
74                                     <IconButton onClick={() => props.executeMulti(btn, checkedList, props.resources)}>
75                                         {currentPathIsTrash ? <RestoreFromTrashIcon /> : <TrashIcon />}
76                                     </IconButton>
77                                 </Tooltip>
78                             ) : (
79                                 <Tooltip
80                                     className={classes.button}
81                                     title={btn.name}
82                                     key={i}
83                                     disableFocusListener>
84                                     <IconButton onClick={() => props.executeMulti(btn, checkedList, props.resources)}>
85                                         {btn.icon ? btn.icon({}) : <></>}
86                                     </IconButton>
87                                 </Tooltip>
88                             )
89                         )
90                     ) : (
91                         <></>
92                     )}
93                 </Toolbar>
94             </React.Fragment>
95         );
96     })
97 );
98
99 export function selectedToArray(checkedList: TCheckedList): Array<string> {
100     const arrayifiedSelectedList: Array<string> = [];
101     for (const [key, value] of Object.entries(checkedList)) {
102         if (value === true) {
103             arrayifiedSelectedList.push(key);
104         }
105     }
106     return arrayifiedSelectedList;
107 }
108
109 export function selectedToKindSet(checkedList: TCheckedList): Set<string> {
110     const setifiedList = new Set<string>();
111     for (const [key, value] of Object.entries(checkedList)) {
112         if (value === true) {
113             setifiedList.add(extractUuidKind(key) as string);
114         }
115     }
116     return setifiedList;
117 }
118
119 function groupByKind(checkedList: TCheckedList, resources: ResourcesState): Record<string, ContextMenuResource[]> {
120     const result = {};
121     selectedToArray(checkedList).forEach(uuid => {
122         const resource = getResource(uuid)(resources) as Resource;
123         if (!result[resource.kind]) result[resource.kind] = [];
124         result[resource.kind].push(resource);
125     });
126     return result;
127 }
128
129 function filterActions(actionArray: ContextMenuActionSet, filters: Set<string>): Array<ContextMenuAction> {
130     return actionArray[0].filter(action => filters.has(action.name as string));
131 }
132
133 function selectActionsByKind(currentResourceKinds: Array<string>, filterSet: TMultiselectActionsFilters) {
134     const rawResult: Set<ContextMenuAction> = new Set();
135     const resultNames = new Set();
136     const allFiltersArray: ContextMenuAction[][] = [];
137     currentResourceKinds.forEach(kind => {
138         if (filterSet[kind]) {
139             const actions = filterActions(...filterSet[kind]);
140             allFiltersArray.push(actions);
141             actions.forEach(action => {
142                 if (!resultNames.has(action.name)) {
143                     rawResult.add(action);
144                     resultNames.add(action.name);
145                 }
146             });
147         }
148     });
149
150     const filteredNameSet = allFiltersArray.map(filterArray => {
151         const resultSet = new Set();
152         filterArray.forEach(action => resultSet.add(action.name || ""));
153         return resultSet;
154     });
155
156     const filteredResult = Array.from(rawResult).filter(action => {
157         for (let i = 0; i < filteredNameSet.length; i++) {
158             if (!filteredNameSet[i].has(action.name)) return false;
159         }
160         return true;
161     });
162
163     return filteredResult.sort((a, b) => {
164         const nameA = a.name || "";
165         const nameB = b.name || "";
166         if (nameA < nameB) {
167             return -1;
168         }
169         if (nameA > nameB) {
170             return 1;
171         }
172         return 0;
173     });
174 }
175
176 //--------------------------------------------------//
177
178 function mapStateToProps(state: RootState) {
179     return {
180         checkedList: state.multiselect.checkedList as TCheckedList,
181         resources: state.resources,
182     };
183 }
184
185 function mapDispatchToProps(dispatch: Dispatch) {
186     return {
187         executeMulti: (selectedAction: ContextMenuAction, checkedList: TCheckedList, resources: ResourcesState): void => {
188             const kindGroups = groupByKind(checkedList, resources);
189             switch (selectedAction.name) {
190                 case contextMenuActionConsts.MOVE_TO:
191                 case contextMenuActionConsts.REMOVE:
192                     const firstResource = getResource(selectedToArray(checkedList)[0])(resources) as Resource;
193                     const action = findActionByName(selectedAction.name as string, kindToActionSet[firstResource.kind]);
194                     if (action) action.execute(dispatch, kindGroups[firstResource.kind]);
195                     break;
196                 case contextMenuActionConsts.COPY_TO_CLIPBOARD:
197                     const selectedResources = selectedToArray(checkedList).map(uuid => getResource(uuid)(resources));
198                     dispatch<any>(copyToClipboardAction(selectedResources));
199                     break;
200                 default:
201                     for (const kind in kindGroups) {
202                         const action = findActionByName(selectedAction.name as string, kindToActionSet[kind]);
203                         if (action) action.execute(dispatch, kindGroups[kind]);
204                     }
205                     break;
206             }
207         },
208     };
209 }