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 } 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 { toggleTrashed } from 'store/trash/trash-actions';
17 import { ResourceKind, extractUuidKind } from 'models/resource';
19 type CssRules = 'root' | 'expanded' | 'button';
21 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
27 margin: '1rem auto auto 0.5rem',
31 transition: 'width 150ms',
34 backgroundColor: '#017ead',
43 type MultiselectToolbarAction = {
46 relevantKinds: Array<ResourceKind>;
49 export const defaultActions: Array<MultiselectToolbarAction> = [
52 action: 'copySelected',
53 relevantKinds: [ResourceKind.COLLECTION],
57 action: 'moveSelected',
58 relevantKinds: [ResourceKind.COLLECTION, ResourceKind.PROCESS],
62 action: 'removeSelected',
63 relevantKinds: [ResourceKind.COLLECTION, ResourceKind.PROCESS, ResourceKind.PROJECT],
67 action: 'barSelected',
68 relevantKinds: [ResourceKind.COLLECTION, ResourceKind.PROJECT],
72 export type MultiselectToolbarProps = {
73 actions: Array<MultiselectToolbarAction>;
75 checkedList: TCheckedList;
76 copySelected: () => void;
77 moveSelected: () => void;
78 barSelected: () => void;
79 removeSelected: (selectedList: TCheckedList) => void;
82 export const MultiselectToolbar = connect(
86 withStyles(styles)((props: MultiselectToolbarProps & WithStyles<CssRules>) => {
87 // console.log(props);
88 const { classes, actions, isVisible, checkedList } = props;
90 const currentResourceKinds = selectedToArray(checkedList).map((element) => extractUuidKind(element));
92 const buttons = actions.filter(
93 (action) => currentResourceKinds.length && currentResourceKinds.every((kind) => action.relevantKinds.includes(kind as ResourceKind))
97 <Toolbar className={isVisible && buttons.length ? `${classes.root} ${classes.expanded}` : classes.root} style={{ width: `${buttons.length * 5.5}rem` }}>
99 buttons.map((btn) => (
100 <Button key={btn.name} className={`${classes.button} ${classes.expanded}`} onClick={() => props[btn.action](checkedList)}>
112 function selectedToString(checkedList: TCheckedList) {
113 let stringifiedSelectedList: string = '';
114 for (const [key, value] of Object.entries(checkedList)) {
115 if (value === true) {
116 stringifiedSelectedList += key + ',';
119 return stringifiedSelectedList.slice(0, -1);
122 function selectedToArray<T>(checkedList: TCheckedList): Array<string> {
123 const arrayifiedSelectedList: Array<string> = [];
124 for (const [key, value] of Object.entries(checkedList)) {
125 if (value === true) {
126 arrayifiedSelectedList.push(key);
129 return arrayifiedSelectedList;
132 function mapStateToProps(state: RootState) {
133 const { isVisible, checkedList } = state.multiselect;
135 isVisible: isVisible,
136 checkedList: checkedList as TCheckedList,
140 function mapDispatchToProps(dispatch: Dispatch) {
142 copySelected: () => {},
143 moveSelected: () => {},
144 barSelected: () => {},
145 removeSelected: (checkedList: TCheckedList) => removeMulti(dispatch, checkedList),
149 function removeMulti(dispatch: Dispatch, checkedList: TCheckedList): void {
150 const list: Array<string> = selectedToArray(checkedList);
151 dispatch<any>(list.length === 1 ? openRemoveProcessDialog(list[0]) : openRemoveManyProcessesDialog(list));