1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 { openRemoveProcessDialog, openRemoveManyProcessesDialog } from 'store/processes/processes-actions';
13 import { processResourceActionSet } from '../../views-components/context-menu/action-sets/process-resource-action-set';
14 import { ContextMenuResource } from 'store/context-menu/context-menu-actions';
15 import { extractUuidKind } from 'models/resource';
16 import { openMoveProcessDialog } from 'store/processes/process-move-actions';
17 import { openCopyProcessDialog, openCopyManyProcessesDialog } from 'store/processes/process-copy-actions';
18 import { getResource } from 'store/resources/resources';
19 import { ResourcesState } from 'store/resources/resources';
20 import { getProcess } from 'store/processes/process';
21 import { CopyProcessDialog, CopyManyProcessesDialog } from 'views-components/dialog-forms/copy-process-dialog';
22 import { collectionActionSet } from 'views-components/context-menu/action-sets/collection-action-set';
23 import { ContextMenuAction, ContextMenuActionSet } from 'views-components/context-menu/context-menu-action-set';
24 import { TrashIcon } from 'components/icon/icon';
26 type CssRules = 'root' | 'button';
28 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
34 margin: '1rem auto auto 0.5rem',
36 transition: 'width 150ms',
39 backgroundColor: '#017ead',
48 export type MultiselectToolbarProps = {
50 checkedList: TCheckedList;
51 resources: ResourcesState;
52 executeMulti: (fn, checkedList: TCheckedList, resources: ResourcesState) => void;
55 const collectionMSActionsFilter = {
56 MAKE_A_COPY: 'Make a copy',
58 TOGGLE_TRASH_ACTION: 'ToggleTrashAction',
61 const multiselectActionsFilters = {
62 'arvados#collection': [collectionActionSet, collectionMSActionsFilter],
65 export const MultiselectToolbar = connect(
69 withStyles(styles)((props: MultiselectToolbarProps & WithStyles<CssRules>) => {
70 const { classes, isVisible, checkedList, resources } = props;
71 const currentResourceKinds = Array.from(selectedToKindSet(checkedList));
73 const buttons = selectActionsByKind(currentResourceKinds, multiselectActionsFilters);
76 <Toolbar className={classes.root} style={{ width: `${buttons.length * 3.5}rem` }}>
78 buttons.map((btn, i) => (
79 <Tooltip title={btn.name} key={i} disableFocusListener>
80 <IconButton onClick={() => props.executeMulti(btn.execute, checkedList, props.resources)}>
82 btn.icon({ className: 'foo' })
83 ) : btn.name === 'ToggleTrashAction' ? (
99 //todo: put these all in a /utils
100 function selectedToArray(checkedList: TCheckedList): Array<string> {
101 const arrayifiedSelectedList: Array<string> = [];
102 for (const [key, value] of Object.entries(checkedList)) {
103 if (value === true) {
104 arrayifiedSelectedList.push(key);
107 return arrayifiedSelectedList;
110 function selectedToKindSet(checkedList: TCheckedList): Set<string> {
111 const setifiedList = new Set<string>();
112 for (const [key, value] of Object.entries(checkedList)) {
113 if (value === true) {
114 setifiedList.add(extractUuidKind(key) as string);
120 function filterActions(actionArray: ContextMenuActionSet, filters: Record<string, string>): Array<ContextMenuAction> {
121 return actionArray[0].filter((action) => Object.values(filters).includes(action.name as string));
124 function selectActionsByKind(resourceKinds: Array<string>, filterSet: any) {
125 const result: Array<ContextMenuAction> = [];
126 resourceKinds.forEach((kind) => result.push(...filterActions(filterSet[kind][0], filterSet[kind][1])));
130 //--------------------------------------------------//
132 function mapStateToProps(state: RootState) {
133 const { isVisible, checkedList } = state.multiselect;
135 isVisible: isVisible,
136 checkedList: checkedList as TCheckedList,
137 resources: state.resources,
141 function mapDispatchToProps(dispatch: Dispatch) {
143 executeMulti: (fn, checkedList: TCheckedList, resources: ResourcesState) =>
144 selectedToArray(checkedList).forEach((uuid) => {
146 fn(dispatch, getResource(uuid)(resources));