21224: created global selected resource state Arvados-DCO-1.1-Signed-off-by: Lisa...
[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 { LAST_REFRESH_TIMESTAMP } from "components/refresh-button/refresh-button";
15 import { toggleMSToolbar, setCheckedListOnStore, setSelectedUuid, isExactlyOneSelected } from "store/multiselect/multiselect-actions";
16 import { DetailsPanelState } from "store/details-panel/details-panel-reducer";
17
18 interface Props {
19     id: string;
20     onRowClick: (item: any) => void;
21     onContextMenu?: (event: React.MouseEvent<HTMLElement>, item: any, isAdmin?: boolean) => void;
22     onRowDoubleClick: (item: any) => void;
23     extractKey?: (item: any) => React.Key;
24 }
25
26 const getCurrentItemUuid = (
27     currentRoute: string,
28     workflowPanelDetailsUuid: string,
29     isDetailsResourceChecked: boolean,
30     isOnlyOneSelected: boolean,
31     detailsPanel: DetailsPanelState,
32     multiselectSelectedUuid: string
33 ) => {
34     if(currentRoute === '/workflows') {
35         return workflowPanelDetailsUuid;
36     }
37     if(isDetailsResourceChecked && isOnlyOneSelected) {
38         return detailsPanel.resourceUuid;
39     }
40     if(!detailsPanel.isOpened){
41         return multiselectSelectedUuid;
42     }
43     return detailsPanel.resourceUuid;
44 };
45
46 const mapStateToProps = ({ progressIndicator, dataExplorer, router, multiselect, detailsPanel, properties}: RootState, { id }: Props) => {
47     const progress = progressIndicator.find(p => p.id === id);
48     const dataExplorerState = getDataExplorer(dataExplorer, id);
49     const currentRoute = router.location ? router.location.pathname : "";
50     const currentRefresh = localStorage.getItem(LAST_REFRESH_TIMESTAMP) || "";
51     const isDetailsResourceChecked = multiselect.checkedList[detailsPanel.resourceUuid]
52     const isOnlyOneSelected = Object.values(multiselect.checkedList).filter(x => x === true).length === 1;
53     const currentItemUuid = getCurrentItemUuid(currentRoute, properties.workflowPanelDetailsUuid, isDetailsResourceChecked, isOnlyOneSelected, detailsPanel, multiselect.selectedUuid);
54     const isMSToolbarVisible = multiselect.isVisible;
55     return {
56         ...dataExplorerState,
57         working: !!progress?.working,
58         currentRefresh: currentRefresh,
59         currentRoute: currentRoute,
60         paperKey: currentRoute,
61         currentItemUuid,
62         isMSToolbarVisible,
63         checkedList: multiselect.checkedList,
64     };
65 };
66
67 const mapDispatchToProps = () => {
68     return (dispatch: Dispatch, { id, onRowClick, onRowDoubleClick, onContextMenu }: Props) => ({
69         onSetColumns: (columns: DataColumns<any, any>) => {
70             dispatch(dataExplorerActions.SET_COLUMNS({ id, columns }));
71         },
72
73         onSearch: (searchValue: string) => {
74             dispatch(dataExplorerActions.SET_EXPLORER_SEARCH_VALUE({ id, searchValue }));
75         },
76
77         onColumnToggle: (column: DataColumn<any, any>) => {
78             dispatch(dataExplorerActions.TOGGLE_COLUMN({ id, columnName: column.name }));
79         },
80
81         onSortToggle: (column: DataColumn<any, any>) => {
82             dispatch(dataExplorerActions.TOGGLE_SORT({ id, columnName: column.name }));
83         },
84
85         onFiltersChange: (filters: DataTableFilters, column: DataColumn<any, any>) => {
86             dispatch(dataExplorerActions.SET_FILTERS({ id, columnName: column.name, filters }));
87         },
88
89         onChangePage: (page: number) => {
90             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
91         },
92
93         onChangeRowsPerPage: (rowsPerPage: number) => {
94             dispatch(dataExplorerActions.SET_ROWS_PER_PAGE({ id, rowsPerPage }));
95         },
96
97         onLoadMore: (page: number) => {
98             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
99         },
100
101         toggleMSToolbar: (isVisible: boolean) => {
102             dispatch<any>(toggleMSToolbar(isVisible));
103         },
104
105         setCheckedListOnStore: (checkedList: TCheckedList) => {
106             dispatch<any>(setCheckedListOnStore(checkedList));
107         },
108
109         setSelectedUuid: (checkedList: TCheckedList) => {
110             dispatch<any>(setSelectedUuid(isExactlyOneSelected(checkedList)))
111         },
112         
113
114         onRowClick,
115
116         onRowDoubleClick,
117
118         onContextMenu,
119     });
120 };
121
122 export const DataExplorer = connect(mapStateToProps, mapDispatchToProps)(DataExplorerComponent);