15768: first step in revamp: imported context menu actions and got them to work for...
[arvados-workbench2.git] / src / components / multiselectToolbar / MultiselectToolbar.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React, { ReactElement } from 'react';
6 import { connect } from 'react-redux';
7 import { StyleRulesCallback, withStyles, WithStyles, Toolbar, Button, 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 { CopyToClipboardSnackbar } from 'components/copy-to-clipboard-snackbar/copy-to-clipboard-snackbar';
12 import { TCheckedList } from 'components/data-table/data-table';
13 import { openRemoveProcessDialog, openRemoveManyProcessesDialog } from 'store/processes/processes-actions';
14 import { processResourceActionSet } from '../../views-components/context-menu/action-sets/process-resource-action-set';
15 import { ContextMenuResource } from 'store/context-menu/context-menu-actions';
16 import { ResourceKind, extractUuidKind } from 'models/resource';
17 import { openMoveProcessDialog } from 'store/processes/process-move-actions';
18 import { openCopyProcessDialog, openCopyManyProcessesDialog } from 'store/processes/process-copy-actions';
19 import { getResource } from 'store/resources/resources';
20 import { ResourceName } from 'views-components/data-explorer/renderers';
21 import { ProcessResource } from 'models/process';
22 import { ResourcesState } from 'store/resources/resources';
23 import { Resource } from 'models/resource';
24 import { getProcess } from 'store/processes/process';
25 import { CopyProcessDialog, CopyManyProcessesDialog } from 'views-components/dialog-forms/copy-process-dialog';
26 import { collectionActionSet } from 'views-components/context-menu/action-sets/collection-action-set';
27 import { ContextMenuAction, ContextMenuActionSet } from 'views-components/context-menu/context-menu-action-set';
28 import { TrashIcon } from 'components/icon/icon';
29
30 type CssRules = 'root' | 'button';
31
32 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
33     root: {
34         display: 'flex',
35         flexDirection: 'row',
36         width: 0,
37         padding: 0,
38         margin: '1rem auto auto 0.5rem',
39         overflow: 'hidden',
40         transition: 'width 150ms',
41     },
42     button: {
43         backgroundColor: '#017ead',
44         color: 'white',
45         fontSize: '0.75rem',
46         width: 'auto',
47         margin: 'auto',
48         padding: '1px',
49     },
50 });
51
52 // type MultiselectToolbarAction = {
53 //     name: string;
54 //     funcName: string;
55 //     relevantKinds: Set<ResourceKind>;
56 // };
57
58 //gleaned from src/views-components/context-menu/action-sets
59 // export const defaultActions: Array<MultiselectToolbarAction> = [
60 //     // {
61 //     //     name: 'copy and re-run',
62 //     //     funcName: 'copySelected',
63 //     //     relevantKinds: new Set([ResourceKind.PROCESS]),
64 //     // },
65 //     {
66 //         name: 'copy',
67 //         funcName: 'copyCollections',
68 //         relevantKinds: new Set([ResourceKind.COLLECTION]),
69 //     },
70 //     {
71 //         name: 'move',
72 //         funcName: 'moveSelected',
73 //         relevantKinds: new Set([ResourceKind.PROCESS, ResourceKind.PROJECT]),
74 //     },
75 //     {
76 //         name: 'remove',
77 //         funcName: 'removeSelected',
78 //         relevantKinds: new Set([ResourceKind.PROCESS, ResourceKind.COLLECTION]),
79 //     },
80 //     {
81 //         name: 'favorite',
82 //         funcName: 'favoriteSelected',
83 //         relevantKinds: new Set([ResourceKind.PROCESS, ResourceKind.PROJECT, ResourceKind.COLLECTION]),
84 //     },
85 // ];
86
87 export type MultiselectToolbarProps = {
88     // actions: Array<MultiselectToolbarAction>;
89     isVisible: boolean;
90     checkedList: TCheckedList;
91     resources: ResourcesState;
92     // copySelected: (checkedList: TCheckedList, resources: ResourcesState) => void;
93     // copyCollections: (fn, resource: Resource) => void;
94     // moveSelected: (resource) => void;
95     // removeSelected: (checkedList: TCheckedList) => void;
96     executeMulti: (fn, checkedList: TCheckedList, resources: ResourcesState) => void;
97 };
98
99 const CollectionMSActionsFilter = {
100     MAKE_A_COPY: 'Make a copy',
101     MOVE_TO: 'Move to',
102     TOGGLE_TRASH_ACTION: 'ToggleTrashAction',
103 };
104
105 export const MultiselectToolbar = connect(
106     mapStateToProps,
107     mapDispatchToProps
108 )(
109     withStyles(styles)((props: MultiselectToolbarProps & WithStyles<CssRules>) => {
110         const { classes, isVisible, checkedList, resources } = props;
111         const currentResourceKinds = Array.from(selectedToKindSet(checkedList));
112
113         const buttons = filterActions(collectionActionSet, CollectionMSActionsFilter);
114         console.log(selectedToArray(props.checkedList));
115
116         return (
117             <Toolbar className={classes.root} style={{ width: `${buttons.length * 5.5}rem` }}>
118                 {buttons.length ? (
119                     buttons.map((btn) => (
120                         <Tooltip title={btn.name} disableFocusListener>
121                             <IconButton
122                                 onClick={() =>
123                                     props.executeMulti(
124                                         btn.execute,
125                                         checkedList,
126                                         props.resources
127                                         // getResource('tordo-4zz18-2dkyrfnrsjdda5v')(props.resources) as Resource
128                                     )
129                                 }
130                             >
131                                 {/* {console.log(btn.component && btn.component)} */}
132                                 {btn.icon ? (
133                                     btn.icon({ className: 'foo' })
134                                 ) : btn.name === 'ToggleTrashAction' ? (
135                                     <TrashIcon />
136                                 ) : (
137                                     <>error</>
138                                 )}
139                             </IconButton>
140                         </Tooltip>
141                     ))
142                 ) : (
143                     <></>
144                 )}
145             </Toolbar>
146         );
147     })
148 );
149
150 //todo: put these all in a /utils
151 function selectedToArray(checkedList: TCheckedList): Array<string> {
152     const arrayifiedSelectedList: Array<string> = [];
153     for (const [key, value] of Object.entries(checkedList)) {
154         if (value === true) {
155             arrayifiedSelectedList.push(key);
156         }
157     }
158     return arrayifiedSelectedList;
159 }
160
161 function selectedToKindSet(checkedList: TCheckedList): Set<string> {
162     const setifiedList = new Set<string>();
163     for (const [key, value] of Object.entries(checkedList)) {
164         if (value === true) {
165             setifiedList.add(extractUuidKind(key) as string);
166         }
167     }
168     return setifiedList;
169 }
170
171 function filterActions(actionArray: ContextMenuActionSet, filters: Record<string, string>): Array<ContextMenuAction> {
172     return actionArray[0].filter((action) => Object.values(filters).includes(action.name as string));
173 }
174
175 //--------------------------------------------------//
176
177 function mapStateToProps(state: RootState) {
178     const { isVisible, checkedList } = state.multiselect;
179     return {
180         isVisible: isVisible,
181         checkedList: checkedList as TCheckedList,
182         resources: state.resources,
183     };
184 }
185
186 function mapDispatchToProps(dispatch: Dispatch) {
187     return {
188         // copySelected: (checkedList: TCheckedList, resources: ResourcesState) => copyMoveMany(dispatch, checkedList),
189         // copyCollections: (fn, resource) => fn(dispatch, resource),
190         // moveSelected: (checkedList: TCheckedList) => {},
191         // removeSelected: (checkedList: TCheckedList) => removeMultiProcesses(dispatch, checkedList),
192         executeMulti: (fn, checkedList: TCheckedList, resources: ResourcesState) =>
193             selectedToArray(checkedList).forEach((uuid) => {
194                 console.log(uuid);
195                 fn(dispatch, getResource(uuid)(resources));
196             }),
197     };
198 }
199
200 // function copyMoveMany(dispatch: Dispatch, checkedList: TCheckedList) {
201 //     const selectedList: Array<string> = selectedToArray(checkedList);
202 //     const uuid = selectedList[0];
203 //     dispatch<any>(openCopyManyProcessesDialog(selectedList));
204 // }
205
206 // const RemoveFunctions = {
207 //     ONE_PROCESS: (uuid: string) => openRemoveProcessDialog(uuid),
208 //     MANY_PROCESSES: (list: Array<string>) => openRemoveManyProcessesDialog(list),
209 // };
210
211 // function removeMultiProcesses(dispatch: Dispatch, checkedList: TCheckedList): void {
212 //     const selectedList: Array<string> = selectedToArray(checkedList);
213 //     dispatch<any>(
214 //         selectedList.length === 1
215 //             ? RemoveFunctions.ONE_PROCESS(selectedList[0])
216 //             : RemoveFunctions.MANY_PROCESSES(selectedList)
217 //     );
218 // }