18207: Added common icon and removed code duplication
[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 data: any[] = [];
25 let href: string = '';
26
27 const mapStateToProps = (state: RootState, { id }: Props) => {
28     const progress = state.progressIndicator.find(p => p.id === id);
29     const dataExplorerState = getDataExplorer(state.dataExplorer, id);
30     const currentRoute = state.router.location ? state.router.location.pathname : '';
31     const currentItemUuid = currentRoute === '/workflows' ? state.properties.workflowPanelDetailsUuid : state.detailsPanel.resourceUuid;
32
33     let loading = false;
34
35     if (dataExplorerState.items.length > 0 && data === dataExplorerState.items && href !== window.location.href) {
36         loading = true
37     } else {
38         href = window.location.href;
39         data = dataExplorerState.items;
40     }
41
42     const working = (progress && progress.working) || loading;
43
44     return { ...dataExplorerState, working, paperKey: currentRoute, currentItemUuid };
45 };
46
47 const mapDispatchToProps = () => {
48     return (dispatch: Dispatch, { id, onRowClick, onRowDoubleClick, onContextMenu }: Props) => ({
49         onSetColumns: (columns: DataColumns<any>) => {
50             dispatch(dataExplorerActions.SET_COLUMNS({ id, columns }));
51         },
52
53         onSearch: (searchValue: string) => {
54             dispatch(dataExplorerActions.SET_EXPLORER_SEARCH_VALUE({ id, searchValue }));
55         },
56
57         onColumnToggle: (column: DataColumn<any>) => {
58             dispatch(dataExplorerActions.TOGGLE_COLUMN({ id, columnName: column.name }));
59         },
60
61         onSortToggle: (column: DataColumn<any>) => {
62             dispatch(dataExplorerActions.TOGGLE_SORT({ id, columnName: column.name }));
63         },
64
65         onFiltersChange: (filters: DataTableFilters, column: DataColumn<any>) => {
66             dispatch(dataExplorerActions.SET_FILTERS({ id, columnName: column.name, filters }));
67         },
68
69         onChangePage: (page: number) => {
70             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
71         },
72
73         onChangeRowsPerPage: (rowsPerPage: number) => {
74             dispatch(dataExplorerActions.SET_ROWS_PER_PAGE({ id, rowsPerPage }));
75         },
76
77         onLoadMore: (page: number) => {
78             dispatch(dataExplorerActions.SET_PAGE({ id, page }));
79         },
80
81         onRowClick,
82
83         onRowDoubleClick,
84
85         onContextMenu,
86     });
87 };
88
89 export const DataExplorer = connect(mapStateToProps, mapDispatchToProps())(DataExplorerComponent);
90