1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
30 type CssRules = 'root' | 'button';
32 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
38 margin: '1rem auto auto 0.5rem',
40 transition: 'width 150ms',
43 backgroundColor: '#017ead',
52 // type MultiselectToolbarAction = {
55 // relevantKinds: Set<ResourceKind>;
58 //gleaned from src/views-components/context-menu/action-sets
59 // export const defaultActions: Array<MultiselectToolbarAction> = [
61 // // name: 'copy and re-run',
62 // // funcName: 'copySelected',
63 // // relevantKinds: new Set([ResourceKind.PROCESS]),
67 // funcName: 'copyCollections',
68 // relevantKinds: new Set([ResourceKind.COLLECTION]),
72 // funcName: 'moveSelected',
73 // relevantKinds: new Set([ResourceKind.PROCESS, ResourceKind.PROJECT]),
77 // funcName: 'removeSelected',
78 // relevantKinds: new Set([ResourceKind.PROCESS, ResourceKind.COLLECTION]),
82 // funcName: 'favoriteSelected',
83 // relevantKinds: new Set([ResourceKind.PROCESS, ResourceKind.PROJECT, ResourceKind.COLLECTION]),
87 export type MultiselectToolbarProps = {
88 // actions: Array<MultiselectToolbarAction>;
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;
99 const CollectionMSActionsFilter = {
100 MAKE_A_COPY: 'Make a copy',
102 TOGGLE_TRASH_ACTION: 'ToggleTrashAction',
105 export const MultiselectToolbar = connect(
109 withStyles(styles)((props: MultiselectToolbarProps & WithStyles<CssRules>) => {
110 const { classes, isVisible, checkedList, resources } = props;
111 const currentResourceKinds = Array.from(selectedToKindSet(checkedList));
113 const buttons = filterActions(collectionActionSet, CollectionMSActionsFilter);
114 console.log(selectedToArray(props.checkedList));
117 <Toolbar className={classes.root} style={{ width: `${buttons.length * 5.5}rem` }}>
119 buttons.map((btn) => (
120 <Tooltip title={btn.name} disableFocusListener>
127 // getResource('tordo-4zz18-2dkyrfnrsjdda5v')(props.resources) as Resource
131 {/* {console.log(btn.component && btn.component)} */}
133 btn.icon({ className: 'foo' })
134 ) : btn.name === 'ToggleTrashAction' ? (
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);
158 return arrayifiedSelectedList;
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);
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));
175 //--------------------------------------------------//
177 function mapStateToProps(state: RootState) {
178 const { isVisible, checkedList } = state.multiselect;
180 isVisible: isVisible,
181 checkedList: checkedList as TCheckedList,
182 resources: state.resources,
186 function mapDispatchToProps(dispatch: Dispatch) {
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) => {
195 fn(dispatch, getResource(uuid)(resources));
200 // function copyMoveMany(dispatch: Dispatch, checkedList: TCheckedList) {
201 // const selectedList: Array<string> = selectedToArray(checkedList);
202 // const uuid = selectedList[0];
203 // dispatch<any>(openCopyManyProcessesDialog(selectedList));
206 // const RemoveFunctions = {
207 // ONE_PROCESS: (uuid: string) => openRemoveProcessDialog(uuid),
208 // MANY_PROCESSES: (list: Array<string>) => openRemoveManyProcessesDialog(list),
211 // function removeMultiProcesses(dispatch: Dispatch, checkedList: TCheckedList): void {
212 // const selectedList: Array<string> = selectedToArray(checkedList);
214 // selectedList.length === 1
215 // ? RemoveFunctions.ONE_PROCESS(selectedList[0])
216 // : RemoveFunctions.MANY_PROCESSES(selectedList)