20251: Fix flaky collection file browser by using race-free state update callback
[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 const mapStateToProps = (state: RootState, { id }: Props) => {
25     const progress = state.progressIndicator.find(p => p.id === id);
26     const dataExplorerState = getDataExplorer(state.dataExplorer, id);
27     const currentRoute = state.router.location ? state.router.location.pathname : '';
28     const currentRefresh = localStorage.getItem(LAST_REFRESH_TIMESTAMP) || '';
29     const currentItemUuid = currentRoute === '/workflows' ? state.properties.workflowPanelDetailsUuid : state.detailsPanel.resourceUuid;
30     return {
31         ...dataExplorerState,
32         working: !!progress?.working,
33         currentRefresh: currentRefresh,
34         currentRoute: currentRoute,
35         paperKey: currentRoute,
36         currentItemUuid,
37     };
38 };
39
40 const mapDispatchToProps = () => {
41     return (dispatch: Dispatch, { id, onRowClick, onRowDoubleClick, onContextMenu }: Props) => ({
42         onSetColumns: (columns: DataColumns<any, any>) => {
43             dispatch(dataExplorerActions.SET_COLUMNS({ id, columns }));
44         },
45
46         onSearch: (searchValue: string) => {
47             dispatch(dataExplorerActions.SET_EXPLORER_SEARCH_VALUE({ id, searchValue }));
48         },
49
50         onColumnToggle: (column: DataColumn<any, any>) => {
51             dispatch(dataExplorerActions.TOGGLE_COLUMN({ id, columnName: column.name }));
52         },
53
54         onSortToggle: (column: DataColumn<any, any>) => {
55             dispatch(dataExplorerActions.TOGGLE_SORT({ id, columnName: column.name }));
56         },
57
58         onFiltersChange: (filters: DataTableFilters, column: DataColumn<any, any>) => {
59             dispatch(dataExplorerActions.SET_FILTERS({ id, columnName: column.name, filters }));
60         },
61
62         onChangePage: (page: number) => {
63             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
64         },
65
66         onChangeRowsPerPage: (rowsPerPage: number) => {
67             dispatch(dataExplorerActions.SET_ROWS_PER_PAGE({ id, rowsPerPage }));
68         },
69
70         onLoadMore: (page: number) => {
71             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
72         },
73
74         onRowClick,
75
76         onRowDoubleClick,
77
78         onContextMenu,
79     });
80 };
81
82 export const DataExplorer = connect(mapStateToProps, mapDispatchToProps)(DataExplorerComponent);