//
// SPDX-License-Identifier: AGPL-3.0
-import React, { ReactElement } from 'react';
+import React from 'react';
import { connect } from 'react-redux';
-import { StyleRulesCallback, withStyles, WithStyles, Toolbar, Button } from '@material-ui/core';
+import { StyleRulesCallback, withStyles, WithStyles, Toolbar, Tooltip, IconButton } from '@material-ui/core';
import { ArvadosTheme } from 'common/custom-theme';
import { RootState } from 'store/store';
import { Dispatch } from 'redux';
-import { CopyToClipboardSnackbar } from 'components/copy-to-clipboard-snackbar/copy-to-clipboard-snackbar';
import { TCheckedList } from 'components/data-table/data-table';
import { openRemoveProcessDialog, openRemoveManyProcessesDialog } from 'store/processes/processes-actions';
import { processResourceActionSet } from '../../views-components/context-menu/action-sets/process-resource-action-set';
import { ContextMenuResource } from 'store/context-menu/context-menu-actions';
-import { toggleTrashed } from 'store/trash/trash-actions';
-import { ResourceKind, extractUuidKind } from 'models/resource';
+import { extractUuidKind } from 'models/resource';
+import { openMoveProcessDialog } from 'store/processes/process-move-actions';
+import { openCopyProcessDialog, openCopyManyProcessesDialog } from 'store/processes/process-copy-actions';
+import { getResource } from 'store/resources/resources';
+import { ResourcesState } from 'store/resources/resources';
+import { getProcess } from 'store/processes/process';
+import { CopyProcessDialog, CopyManyProcessesDialog } from 'views-components/dialog-forms/copy-process-dialog';
+import { collectionActionSet } from 'views-components/context-menu/action-sets/collection-action-set';
+import { ContextMenuAction, ContextMenuActionSet } from 'views-components/context-menu/context-menu-action-set';
+import { TrashIcon } from 'components/icon/icon';
-type CssRules = 'root' | 'expanded' | 'button';
+type CssRules = 'root' | 'button';
const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
root: {
padding: 0,
margin: '1rem auto auto 0.5rem',
overflow: 'hidden',
- },
- expanded: {
transition: 'width 150ms',
},
button: {
},
});
-type MultiselectToolbarAction = {
- name: string;
- action: string;
- relevantKinds: Set<ResourceKind>;
-};
-
-export const defaultActions: Array<MultiselectToolbarAction> = [
- {
- name: 'copy',
- action: 'copySelected',
- relevantKinds: new Set([ResourceKind.COLLECTION]),
- },
- {
- name: 'move',
- action: 'moveSelected',
- relevantKinds: new Set([ResourceKind.COLLECTION, ResourceKind.PROCESS]),
- },
- {
- name: 'remove',
- action: 'removeSelected',
- relevantKinds: new Set([ResourceKind.COLLECTION, ResourceKind.PROCESS, ResourceKind.PROJECT]),
- },
- {
- name: 'foo',
- action: 'barSelected',
- relevantKinds: new Set([ResourceKind.COLLECTION, ResourceKind.PROJECT]),
- },
-];
-
export type MultiselectToolbarProps = {
- actions: Array<MultiselectToolbarAction>;
isVisible: boolean;
checkedList: TCheckedList;
- copySelected: () => void;
- moveSelected: () => void;
- barSelected: () => void;
- removeSelected: (selectedList: TCheckedList) => void;
+ resources: ResourcesState;
+ executeMulti: (fn, checkedList: TCheckedList, resources: ResourcesState) => void;
+};
+
+const collectionMSActionsFilter = {
+ MAKE_A_COPY: 'Make a copy',
+ MOVE_TO: 'Move to',
+ TOGGLE_TRASH_ACTION: 'ToggleTrashAction',
+};
+
+const multiselectActionsFilters = {
+ 'arvados#collection': [collectionActionSet, collectionMSActionsFilter],
};
export const MultiselectToolbar = connect(
mapDispatchToProps
)(
withStyles(styles)((props: MultiselectToolbarProps & WithStyles<CssRules>) => {
- // console.log(props);
- const { classes, actions, isVisible, checkedList } = props;
-
+ const { classes, isVisible, checkedList, resources } = props;
const currentResourceKinds = Array.from(selectedToKindSet(checkedList));
- const buttons = actions.filter((action) => currentResourceKinds.length && currentResourceKinds.every((kind) => action.relevantKinds.has(kind as ResourceKind)));
+
+ const buttons = selectActionsByKind(currentResourceKinds, multiselectActionsFilters);
return (
- <Toolbar className={isVisible && buttons.length ? `${classes.root} ${classes.expanded}` : classes.root} style={{ width: `${buttons.length * 5.5}rem` }}>
+ <Toolbar className={classes.root} style={{ width: `${buttons.length * 3.5}rem` }}>
{buttons.length ? (
- buttons.map((btn) => (
- <Button key={btn.name} className={`${classes.button} ${classes.expanded}`} onClick={() => props[btn.action](checkedList)}>
- {btn.name}
- </Button>
+ buttons.map((btn, i) => (
+ <Tooltip title={btn.name} key={i} disableFocusListener>
+ <IconButton onClick={() => props.executeMulti(btn.execute, checkedList, props.resources)}>
+ {btn.icon ? (
+ btn.icon({ className: 'foo' })
+ ) : btn.name === 'ToggleTrashAction' ? (
+ <TrashIcon />
+ ) : (
+ <>error</>
+ )}
+ </IconButton>
+ </Tooltip>
))
) : (
<></>
})
);
+//todo: put these all in a /utils
function selectedToArray(checkedList: TCheckedList): Array<string> {
const arrayifiedSelectedList: Array<string> = [];
for (const [key, value] of Object.entries(checkedList)) {
return setifiedList;
}
+function filterActions(actionArray: ContextMenuActionSet, filters: Record<string, string>): Array<ContextMenuAction> {
+ return actionArray[0].filter((action) => Object.values(filters).includes(action.name as string));
+}
+
+function selectActionsByKind(resourceKinds: Array<string>, filterSet: any) {
+ const result: Array<ContextMenuAction> = [];
+ resourceKinds.forEach((kind) => result.push(...filterActions(filterSet[kind][0], filterSet[kind][1])));
+ return result;
+}
+
+//--------------------------------------------------//
+
function mapStateToProps(state: RootState) {
const { isVisible, checkedList } = state.multiselect;
return {
isVisible: isVisible,
checkedList: checkedList as TCheckedList,
+ resources: state.resources,
};
}
function mapDispatchToProps(dispatch: Dispatch) {
return {
- copySelected: () => {},
- moveSelected: () => {},
- barSelected: () => {},
- removeSelected: (checkedList: TCheckedList) => removeMulti(dispatch, checkedList),
+ executeMulti: (fn, checkedList: TCheckedList, resources: ResourcesState) =>
+ selectedToArray(checkedList).forEach((uuid) => {
+ console.log(uuid);
+ fn(dispatch, getResource(uuid)(resources));
+ }),
};
}
-
-function removeMulti(dispatch: Dispatch, checkedList: TCheckedList): void {
- const selectedList: Array<string> = selectedToArray(checkedList);
- dispatch<any>(selectedList.length === 1 ? openRemoveProcessDialog(selectedList[0]) : openRemoveManyProcessesDialog(selectedList));
-}