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) => ({
33 margin: "1rem auto auto 0.5rem",
35 transition: "width 150ms",
43 export type MultiselectToolbarProps = {
44 checkedList: TCheckedList;
45 resources: ResourcesState;
46 executeMulti: (action: ContextMenuAction, checkedList: TCheckedList, resources: ResourcesState) => void;
49 export const MultiselectToolbar = connect(
53 withStyles(styles)((props: MultiselectToolbarProps & WithStyles<CssRules>) => {
54 const { classes, checkedList } = props;
55 const currentResourceKinds = Array.from(selectedToKindSet(checkedList));
57 const currentPathIsTrash = window.location.pathname === "/trash";
59 currentPathIsTrash && selectedToKindSet(checkedList).size
60 ? [msToggleTrashAction]
61 : selectActionsByKind(currentResourceKinds, multiselectActionsFilters);
66 className={classes.root}
67 style={{ width: `${buttons.length * 2.5}rem` }}
70 buttons.map((btn, i) =>
71 btn.name === "ToggleTrashAction" ? (
73 className={classes.button}
74 title={currentPathIsTrash ? "Restore selected" : "Move to trash"}
78 <IconButton onClick={() => props.executeMulti(btn, checkedList, props.resources)}>
79 {currentPathIsTrash ? <RestoreFromTrashIcon /> : <TrashIcon />}
84 className={classes.button}
89 <IconButton onClick={() => props.executeMulti(btn, checkedList, props.resources)}>
90 {btn.icon ? btn.icon({}) : <></>}
104 export function selectedToArray(checkedList: TCheckedList): Array<string> {
105 const arrayifiedSelectedList: Array<string> = [];
106 for (const [key, value] of Object.entries(checkedList)) {
107 if (value === true) {
108 arrayifiedSelectedList.push(key);
111 return arrayifiedSelectedList;
114 export function selectedToKindSet(checkedList: TCheckedList): Set<string> {
115 const setifiedList = new Set<string>();
116 for (const [key, value] of Object.entries(checkedList)) {
117 if (value === true) {
118 setifiedList.add(extractUuidKind(key) as string);
124 function groupByKind(checkedList: TCheckedList, resources: ResourcesState): Record<string, ContextMenuResource[]> {
126 selectedToArray(checkedList).forEach(uuid => {
127 const resource = getResource(uuid)(resources) as ContainerRequestResource | Resource;
128 if (!result[resource.kind]) result[resource.kind] = [];
129 result[resource.kind].push(resource);
134 function filterActions(actionArray: ContextMenuActionSet, filters: Set<string>): Array<ContextMenuAction> {
135 return actionArray[0].filter(action => filters.has(action.name as string));
138 function selectActionsByKind(currentResourceKinds: Array<string>, filterSet: TMultiselectActionsFilters) {
139 const rawResult: Set<ContextMenuAction> = new Set();
140 const resultNames = new Set();
141 const allFiltersArray: ContextMenuAction[][] = [];
142 currentResourceKinds.forEach(kind => {
143 if (filterSet[kind]) {
144 const actions = filterActions(...filterSet[kind]);
145 allFiltersArray.push(actions);
146 actions.forEach(action => {
147 if (!resultNames.has(action.name)) {
148 rawResult.add(action);
149 resultNames.add(action.name);
155 const filteredNameSet = allFiltersArray.map(filterArray => {
156 const resultSet = new Set();
157 filterArray.forEach(action => resultSet.add(action.name || ""));
161 const filteredResult = Array.from(rawResult).filter(action => {
162 for (let i = 0; i < filteredNameSet.length; i++) {
163 if (!filteredNameSet[i].has(action.name)) return false;
168 return filteredResult.sort((a, b) => {
169 const nameA = a.name || "";
170 const nameB = b.name || "";
181 //--------------------------------------------------//
183 function mapStateToProps(state: RootState) {
185 checkedList: state.multiselect.checkedList as TCheckedList,
186 resources: state.resources,
190 function mapDispatchToProps(dispatch: Dispatch) {
192 executeMulti: (selectedAction: ContextMenuAction, checkedList: TCheckedList, resources: ResourcesState): void => {
193 const kindGroups = groupByKind(checkedList, resources);
194 switch (selectedAction.name) {
195 case contextMenuActionConsts.MOVE_TO:
196 case contextMenuActionConsts.REMOVE:
197 const firstResource = getResource(selectedToArray(checkedList)[0])(resources) as ContainerRequestResource | Resource;
198 const action = findActionByName(selectedAction.name as string, kindToActionSet[firstResource.kind]);
199 if (action) action.execute(dispatch, kindGroups[firstResource.kind]);
201 case contextMenuActionConsts.COPY_TO_CLIPBOARD:
202 const selectedResources = selectedToArray(checkedList).map(uuid => getResource(uuid)(resources));
203 dispatch<any>(copyToClipboardAction(selectedResources));
206 for (const kind in kindGroups) {
207 const action = findActionByName(selectedAction.name as string, kindToActionSet[kind]);
208 if (action) action.execute(dispatch, kindGroups[kind]);