Merge branch '15317-metrics'
[arvados.git] / services / workbench2 / src / views-components / data-explorer / data-explorer.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from "react-redux";
6 import { RootState } from "store/store";
7 import { DataExplorer as DataExplorerComponent } from "components/data-explorer/data-explorer";
8 import { getDataExplorer } from "store/data-explorer/data-explorer-reducer";
9 import { Dispatch } from "redux";
10 import { dataExplorerActions } from "store/data-explorer/data-explorer-action";
11 import { DataColumn } from "components/data-table/data-column";
12 import { DataColumns, TCheckedList } from "components/data-table/data-table";
13 import { DataTableFilters } from "components/data-table-filters/data-table-filters-tree";
14 import { toggleMSToolbar, setCheckedListOnStore } from "store/multiselect/multiselect-actions";
15
16 interface Props {
17     id: string;
18     onRowClick: (item: any) => void;
19     onContextMenu?: (event: React.MouseEvent<HTMLElement>, item: any, isAdmin?: boolean) => void;
20     onRowDoubleClick: (item: any) => void;
21     extractKey?: (item: any) => React.Key;
22     working?: boolean;
23 }
24
25 const mapStateToProps = ({ progressIndicator, dataExplorer, router, multiselect, detailsPanel, properties}: RootState, { id }: Props) => {
26     const working = !!progressIndicator.some(p => p.id === id && p.working);
27     const dataExplorerState = getDataExplorer(dataExplorer, id);
28     const currentRoute = router.location ? router.location.pathname : "";
29     const isDetailsResourceChecked = multiselect.checkedList[detailsPanel.resourceUuid]
30     const isOnlyOneSelected = Object.values(multiselect.checkedList).filter(x => x === true).length === 1;
31     const currentItemUuid =
32         currentRoute === '/workflows' ? properties.workflowPanelDetailsUuid : isDetailsResourceChecked && isOnlyOneSelected ? detailsPanel.resourceUuid : multiselect.selectedUuid;
33     const isMSToolbarVisible = multiselect.isVisible;
34     return {
35         ...dataExplorerState,
36         currentRoute: currentRoute,
37         paperKey: currentRoute,
38         currentItemUuid,
39         isMSToolbarVisible,
40         checkedList: multiselect.checkedList,
41         working,
42     };
43 };
44
45 const mapDispatchToProps = () => {
46     return (dispatch: Dispatch, { id, onRowClick, onRowDoubleClick, onContextMenu }: Props) => ({
47         onSetColumns: (columns: DataColumns<any, any>) => {
48             dispatch(dataExplorerActions.SET_COLUMNS({ id, columns }));
49         },
50
51         onSearch: (searchValue: string) => {
52             dispatch(dataExplorerActions.SET_EXPLORER_SEARCH_VALUE({ id, searchValue }));
53         },
54
55         onColumnToggle: (column: DataColumn<any, any>) => {
56             dispatch(dataExplorerActions.TOGGLE_COLUMN({ id, columnName: column.name }));
57         },
58
59         onSortToggle: (column: DataColumn<any, any>) => {
60             dispatch(dataExplorerActions.TOGGLE_SORT({ id, columnName: column.name }));
61         },
62
63         onFiltersChange: (filters: DataTableFilters, column: DataColumn<any, any>) => {
64             dispatch(dataExplorerActions.SET_FILTERS({ id, columnName: column.name, filters }));
65         },
66
67         onChangePage: (page: number) => {
68             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
69         },
70
71         onChangeRowsPerPage: (rowsPerPage: number) => {
72             dispatch(dataExplorerActions.SET_ROWS_PER_PAGE({ id, rowsPerPage }));
73         },
74
75         onLoadMore: (page: number) => {
76             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
77         },
78
79         toggleMSToolbar: (isVisible: boolean) => {
80             dispatch<any>(toggleMSToolbar(isVisible));
81         },
82
83         setCheckedListOnStore: (checkedList: TCheckedList) => {
84             dispatch<any>(setCheckedListOnStore(checkedList));
85         },
86
87         onRowClick,
88
89         onRowDoubleClick,
90
91         onContextMenu,
92     });
93 };
94
95 export const DataExplorer = connect(mapStateToProps, mapDispatchToProps)(DataExplorerComponent);