Add toggleItemSelection callback
[arvados.git] / src / views / process-panel / process-panel.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { RootState } from '~/store/store';
6 import { connect } from 'react-redux';
7 import { getProcess, getSubprocesses, Process, getProcessStatus } from '~/store/processes/process';
8 import { Dispatch } from 'redux';
9 import { openProcessContextMenu } from '~/store/context-menu/context-menu-actions';
10 import { matchProcessRoute } from '~/routes/routes';
11 import { ProcessPanelRootDataProps, ProcessPanelRootActionProps, ProcessPanelRoot } from './process-panel-root';
12 import { ProcessPanel as ProcessPanelState} from '~/store/process-panel/process-panel';
13 import { groupBy } from 'lodash';
14 import { toggleProcessPanelFilter } from '~/store/process-panel/process-panel-actions';
15
16 const mapStateToProps = ({ router, resources, processPanel }: RootState): ProcessPanelRootDataProps => {
17     const pathname = router.location ? router.location.pathname : '';
18     const match = matchProcessRoute(pathname);
19     const uuid = match ? match.params.id : '';
20     const subprocesses = getSubprocesses(uuid)(resources);
21     return {
22         process: getProcess(uuid)(resources),
23         subprocesses: subprocesses.filter(subprocess => processPanel.filters[getProcessStatus(subprocess)]),
24         filters: getFilters(processPanel, subprocesses),
25         totalSubprocessesLength: subprocesses.length,
26     };
27 };
28
29 const mapDispatchToProps = (dispatch: Dispatch): ProcessPanelRootActionProps => ({
30     onContextMenu: (event, process) => {
31         dispatch<any>(openProcessContextMenu(event, process));
32     },
33     onToggle: status => {
34         dispatch<any>(toggleProcessPanelFilter(status));
35     }
36 });
37
38 export const ProcessPanel = connect(mapStateToProps, mapDispatchToProps)(ProcessPanelRoot);
39
40 export const getFilters = (processPanel: ProcessPanelState, processes: Process[]) => {
41     const grouppedProcesses = groupBy(processes, getProcessStatus);
42     return Object
43         .keys(processPanel.filters)
44         .map(filter => ({
45             label: filter,
46             value: (grouppedProcesses[filter] || []).length,
47             checked: processPanel.filters[filter],
48             key: filter,
49         }));
50     };