18972: Records the last refresh click on localStorage for others to act on.
[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 import { LAST_REFRESH_TIMESTAMP } from "components/refresh-button/refresh-button";
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 }
23
24 let prevRoute = '';
25 let prevRefresh = '';
26 let routeChanged = false;
27 let isWorking = false;
28
29 const mapStateToProps = (state: RootState, { id }: Props) => {
30     const progress = state.progressIndicator.find(p => p.id === id);
31     const dataExplorerState = getDataExplorer(state.dataExplorer, id);
32     const currentRoute = state.router.location ? state.router.location.pathname : '';
33     const currentRefresh = localStorage.getItem(LAST_REFRESH_TIMESTAMP) || '';
34     const currentItemUuid = currentRoute === '/workflows' ? state.properties.workflowPanelDetailsUuid : state.detailsPanel.resourceUuid;
35
36     if (currentRoute !== prevRoute || currentRefresh !== prevRefresh) {
37         routeChanged = true;
38         prevRoute = currentRoute;
39         prevRefresh = currentRefresh;
40     }
41     if (progress?.working) {
42         isWorking = true;
43     }
44
45     const working = routeChanged && isWorking;
46
47     if (working && !progress?.working) {
48         routeChanged = false;
49         isWorking = false;
50     }
51
52     return { ...dataExplorerState, working, paperKey: currentRoute, currentItemUuid };
53 };
54
55 const mapDispatchToProps = () => {
56     return (dispatch: Dispatch, { id, onRowClick, onRowDoubleClick, onContextMenu }: Props) => ({
57         onSetColumns: (columns: DataColumns<any>) => {
58             dispatch(dataExplorerActions.SET_COLUMNS({ id, columns }));
59         },
60
61         onSearch: (searchValue: string) => {
62             dispatch(dataExplorerActions.SET_EXPLORER_SEARCH_VALUE({ id, searchValue }));
63         },
64
65         onColumnToggle: (column: DataColumn<any>) => {
66             dispatch(dataExplorerActions.TOGGLE_COLUMN({ id, columnName: column.name }));
67         },
68
69         onSortToggle: (column: DataColumn<any>) => {
70             dispatch(dataExplorerActions.TOGGLE_SORT({ id, columnName: column.name }));
71         },
72
73         onFiltersChange: (filters: DataTableFilters, column: DataColumn<any>) => {
74             dispatch(dataExplorerActions.SET_FILTERS({ id, columnName: column.name, filters }));
75         },
76
77         onChangePage: (page: number) => {
78             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
79         },
80
81         onChangeRowsPerPage: (rowsPerPage: number) => {
82             dispatch(dataExplorerActions.SET_ROWS_PER_PAGE({ id, rowsPerPage }));
83         },
84
85         onLoadMore: (page: number) => {
86             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
87         },
88
89         onRowClick,
90
91         onRowDoubleClick,
92
93         onContextMenu,
94     });
95 };
96
97 export const DataExplorer = connect(mapStateToProps, mapDispatchToProps)(DataExplorerComponent);
98