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 { ContextMenuResource } from "store/context-menu/context-menu-actions";
13 import { Resource, extractUuidKind } from "models/resource";
14 import { getResource } from "store/resources/resources";
15 import { ResourcesState } from "store/resources/resources";
16 import { ContextMenuAction, ContextMenuActionSet } from "views-components/context-menu/context-menu-action-set";
17 import { RestoreFromTrashIcon, TrashIcon } from "components/icon/icon";
18 import { multiselectActionsFilters, TMultiselectActionsFilters, contextMenuActionConsts } from "./ms-toolbar-action-filters";
19 import { kindToActionSet, findActionByName } from "./ms-kind-action-differentiator";
20 import { msToggleTrashAction } from "views-components/multiselect-toolbar/ms-project-action-set";
21 import { copyToClipboardAction } from "store/open-in-new-tab/open-in-new-tab.actions";
22 import { ContainerRequestResource } from "models/container-request";
24 type CssRules = "root" | "button";
26 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
32 margin: "1rem auto auto 0.5rem",
34 transition: "width 150ms",
42 export type MultiselectToolbarProps = {
43 checkedList: TCheckedList;
44 resources: ResourcesState;
45 executeMulti: (action: ContextMenuAction, checkedList: TCheckedList, resources: ResourcesState) => void;
48 export const MultiselectToolbar = connect(
52 withStyles(styles)((props: MultiselectToolbarProps & WithStyles<CssRules>) => {
53 const { classes, checkedList } = props;
54 const currentResourceKinds = Array.from(selectedToKindSet(checkedList));
56 const currentPathIsTrash = window.location.pathname === "/trash";
58 currentPathIsTrash && selectedToKindSet(checkedList).size
59 ? [msToggleTrashAction]
60 : selectActionsByKind(currentResourceKinds, multiselectActionsFilters);
65 className={classes.root}
66 style={{ width: `${buttons.length * 2.5}rem` }}
69 buttons.map((btn, i) =>
70 btn.name === "ToggleTrashAction" ? (
72 className={classes.button}
73 title={currentPathIsTrash ? "Restore selected" : "Move to trash"}
77 <IconButton onClick={() => props.executeMulti(btn, checkedList, props.resources)}>
78 {currentPathIsTrash ? <RestoreFromTrashIcon /> : <TrashIcon />}
83 className={classes.button}
88 <IconButton onClick={() => props.executeMulti(btn, checkedList, props.resources)}>
89 {btn.icon ? btn.icon({}) : <></>}
103 export function selectedToArray(checkedList: TCheckedList): Array<string> {
104 const arrayifiedSelectedList: Array<string> = [];
105 for (const [key, value] of Object.entries(checkedList)) {
106 if (value === true) {
107 arrayifiedSelectedList.push(key);
110 return arrayifiedSelectedList;
113 export function selectedToKindSet(checkedList: TCheckedList): Set<string> {
114 const setifiedList = new Set<string>();
115 for (const [key, value] of Object.entries(checkedList)) {
116 if (value === true) {
117 setifiedList.add(extractUuidKind(key) as string);
123 function groupByKind(checkedList: TCheckedList, resources: ResourcesState): Record<string, ContextMenuResource[]> {
125 selectedToArray(checkedList).forEach(uuid => {
126 const resource = getResource(uuid)(resources) as ContainerRequestResource | Resource;
127 if (!result[resource.kind]) result[resource.kind] = [];
128 result[resource.kind].push(resource);
133 function filterActions(actionArray: ContextMenuActionSet, filters: Set<string>): Array<ContextMenuAction> {
134 return actionArray[0].filter(action => filters.has(action.name as string));
137 function selectActionsByKind(currentResourceKinds: Array<string>, filterSet: TMultiselectActionsFilters) {
138 const rawResult: Set<ContextMenuAction> = new Set();
139 const resultNames = new Set();
140 const allFiltersArray: ContextMenuAction[][] = [];
141 currentResourceKinds.forEach(kind => {
142 if (filterSet[kind]) {
143 const actions = filterActions(...filterSet[kind]);
144 allFiltersArray.push(actions);
145 actions.forEach(action => {
146 if (!resultNames.has(action.name)) {
147 rawResult.add(action);
148 resultNames.add(action.name);
154 const filteredNameSet = allFiltersArray.map(filterArray => {
155 const resultSet = new Set();
156 filterArray.forEach(action => resultSet.add(action.name || ""));
160 const filteredResult = Array.from(rawResult).filter(action => {
161 for (let i = 0; i < filteredNameSet.length; i++) {
162 if (!filteredNameSet[i].has(action.name)) return false;
167 return filteredResult.sort((a, b) => {
168 const nameA = a.name || "";
169 const nameB = b.name || "";
180 //--------------------------------------------------//
182 function mapStateToProps(state: RootState) {
184 checkedList: state.multiselect.checkedList as TCheckedList,
185 resources: state.resources,
189 function mapDispatchToProps(dispatch: Dispatch) {
191 executeMulti: (selectedAction: ContextMenuAction, checkedList: TCheckedList, resources: ResourcesState): void => {
192 const kindGroups = groupByKind(checkedList, resources);
193 switch (selectedAction.name) {
194 case contextMenuActionConsts.MOVE_TO:
195 case contextMenuActionConsts.REMOVE:
196 const firstResource = getResource(selectedToArray(checkedList)[0])(resources) as ContainerRequestResource | Resource;
197 const action = findActionByName(selectedAction.name as string, kindToActionSet[firstResource.kind]);
198 if (action) action.execute(dispatch, kindGroups[firstResource.kind]);
200 case contextMenuActionConsts.COPY_TO_CLIPBOARD:
201 const selectedResources = selectedToArray(checkedList).map(uuid => getResource(uuid)(resources));
202 dispatch<any>(copyToClipboardAction(selectedResources));
205 for (const kind in kindGroups) {
206 const action = findActionByName(selectedAction.name as string, kindToActionSet[kind]);
207 if (action) action.execute(dispatch, kindGroups[kind]);