8db551e8b48a75b300325a2e969e1ddd2e6e2406
[arvados-workbench2.git] / 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 } from "components/data-table/data-table";
13 import { DataTableFilters } from 'components/data-table-filters/data-table-filters-tree';
14
15 interface Props {
16     id: string;
17     onRowClick: (item: any) => void;
18     onContextMenu?: (event: React.MouseEvent<HTMLElement>, item: any, isAdmin?: boolean) => void;
19     onRowDoubleClick: (item: any) => void;
20     extractKey?: (item: any) => React.Key;
21     working?: boolean;
22 }
23
24 let prevRoute = '';
25 let routeChanged = false;
26 let isWorking = false;
27
28 const mapStateToProps = (state: RootState, { id }: Props) => {
29     const progress = state.progressIndicator.find(p => p.id === id);
30     const dataExplorerState = getDataExplorer(state.dataExplorer, id);
31     const currentRoute = state.router.location ? state.router.location.pathname : '';
32     const currentItemUuid = currentRoute === '/workflows' ? state.properties.workflowPanelDetailsUuid : state.detailsPanel.resourceUuid;
33
34     if (currentRoute !== prevRoute) {
35         routeChanged = true;
36         prevRoute = currentRoute;
37     }
38     if (progress?.working) {
39         isWorking = true;
40     }
41
42     const working = routeChanged && isWorking;
43
44     if (working && !progress?.working) {
45         routeChanged = false;
46         isWorking = false;
47     }
48
49     return { ...dataExplorerState, working, paperKey: currentRoute, currentItemUuid };
50 };
51
52 const mapDispatchToProps = () => {
53     return (dispatch: Dispatch, { id, onRowClick, onRowDoubleClick, onContextMenu }: Props) => ({
54         onSetColumns: (columns: DataColumns<any>) => {
55             dispatch(dataExplorerActions.SET_COLUMNS({ id, columns }));
56         },
57
58         onSearch: (searchValue: string) => {
59             dispatch(dataExplorerActions.SET_EXPLORER_SEARCH_VALUE({ id, searchValue }));
60         },
61
62         onColumnToggle: (column: DataColumn<any>) => {
63             dispatch(dataExplorerActions.TOGGLE_COLUMN({ id, columnName: column.name }));
64         },
65
66         onSortToggle: (column: DataColumn<any>) => {
67             dispatch(dataExplorerActions.TOGGLE_SORT({ id, columnName: column.name }));
68         },
69
70         onFiltersChange: (filters: DataTableFilters, column: DataColumn<any>) => {
71             dispatch(dataExplorerActions.SET_FILTERS({ id, columnName: column.name, filters }));
72         },
73
74         onChangePage: (page: number) => {
75             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
76         },
77
78         onChangeRowsPerPage: (rowsPerPage: number) => {
79             dispatch(dataExplorerActions.SET_ROWS_PER_PAGE({ id, rowsPerPage }));
80         },
81
82         onLoadMore: (page: number) => {
83             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
84         },
85
86         onRowClick,
87
88         onRowDoubleClick,
89
90         onContextMenu,
91     });
92 };
93
94 export const DataExplorer = connect(mapStateToProps, mapDispatchToProps)(DataExplorerComponent);
95