Merge branch 'master' into #15165-running-a-process-from-workflow-section-doesnt...
[arvados-workbench2.git] / src / 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 * as React from 'react';
6 import { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, WithStyles, TablePagination, IconButton, Tooltip, Button } from '@material-ui/core';
7 import { ColumnSelector } from "~/components/column-selector/column-selector";
8 import { DataTable, DataColumns, DataTableFetchMode } from "~/components/data-table/data-table";
9 import { DataColumn } from "~/components/data-table/data-column";
10 import { SearchInput } from '~/components/search-input/search-input';
11 import { ArvadosTheme } from "~/common/custom-theme";
12 import { createTree } from '~/models/tree';
13 import { DataTableFilters } from '~/components/data-table-filters/data-table-filters-tree';
14 import { MoreOptionsIcon } from '~/components/icon/icon';
15 import { PaperProps } from '@material-ui/core/Paper';
16
17 type CssRules = 'searchBox' | "toolbar" | "toolbarUnderTitle" | "footer" | "root" | 'moreOptionsButton' | 'title';
18
19 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
20     searchBox: {
21         paddingBottom: theme.spacing.unit * 2
22     },
23     toolbar: {
24         paddingTop: theme.spacing.unit * 2
25     },
26     toolbarUnderTitle: {
27         paddingTop: 0
28     },
29     footer: {
30         overflow: 'auto'
31     },
32     root: {
33         height: '100%'
34     },
35     moreOptionsButton: {
36         padding: 0
37     },
38     title: {
39         paddingLeft: theme.spacing.unit * 3,
40         paddingTop: theme.spacing.unit * 3,
41         fontSize: '18px'
42     }
43 });
44
45 interface DataExplorerDataProps<T> {
46     fetchMode: DataTableFetchMode;
47     items: T[];
48     itemsAvailable: number;
49     columns: DataColumns<T>;
50     searchValue: string;
51     rowsPerPage: number;
52     rowsPerPageOptions: number[];
53     page: number;
54     contextMenuColumn: boolean;
55     dataTableDefaultView?: React.ReactNode;
56     working?: boolean;
57     hideColumnSelector?: boolean;
58     paperProps?: PaperProps;
59     actions?: React.ReactNode;
60     hideSearchInput?: boolean;
61     title?: React.ReactNode;
62     paperKey?: string;
63     currentItemUuid: string;
64 }
65
66 interface DataExplorerActionProps<T> {
67     onSetColumns: (columns: DataColumns<T>) => void;
68     onSearch: (value: string) => void;
69     onRowClick: (item: T) => void;
70     onRowDoubleClick: (item: T) => void;
71     onColumnToggle: (column: DataColumn<T>) => void;
72     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
73     onSortToggle: (column: DataColumn<T>) => void;
74     onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
75     onChangePage: (page: number) => void;
76     onChangeRowsPerPage: (rowsPerPage: number) => void;
77     onLoadMore: (page: number) => void;
78     extractKey?: (item: T) => React.Key;
79 }
80
81 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
82
83 export const DataExplorer = withStyles(styles)(
84     class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
85         componentDidMount() {
86             if (this.props.onSetColumns) {
87                 this.props.onSetColumns(this.props.columns);
88             }
89         }
90         render() {
91             const {
92                 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
93                 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
94                 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
95                 dataTableDefaultView, hideColumnSelector, actions, paperProps, hideSearchInput,
96                 paperKey, fetchMode, currentItemUuid, title
97             } = this.props;
98             return <Paper className={classes.root} {...paperProps} key={paperKey}>
99                 {title && <div className={classes.title}>{title}</div>}
100                 {(!hideColumnSelector || !hideSearchInput) && <Toolbar className={title ? classes.toolbarUnderTitle : classes.toolbar}>
101                     <Grid container justify="space-between" wrap="nowrap" alignItems="center">
102                         <div className={classes.searchBox}>
103                             {!hideSearchInput && <SearchInput
104                                 value={searchValue}
105                                 onSearch={onSearch} />}
106                         </div>
107                         {actions}
108                         {!hideColumnSelector && <ColumnSelector
109                             columns={columns}
110                             onColumnToggle={onColumnToggle} />}
111                     </Grid>
112                 </Toolbar>}
113                 <DataTable
114                     columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
115                     items={items}
116                     onRowClick={(_, item: T) => onRowClick(item)}
117                     onContextMenu={onContextMenu}
118                     onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
119                     onFiltersChange={onFiltersChange}
120                     onSortToggle={onSortToggle}
121                     extractKey={extractKey}
122                     working={working}
123                     defaultView={dataTableDefaultView}
124                     currentItemUuid={currentItemUuid}
125                     currentRoute={paperKey} />
126                 <Toolbar className={classes.footer}>
127                     <Grid container justify="flex-end">
128                         {fetchMode === DataTableFetchMode.PAGINATED ? <TablePagination
129                             count={itemsAvailable}
130                             rowsPerPage={rowsPerPage}
131                             rowsPerPageOptions={rowsPerPageOptions}
132                             page={this.props.page}
133                             onChangePage={this.changePage}
134                             onChangeRowsPerPage={this.changeRowsPerPage}
135                             component="div" /> : <Button
136                                 variant="text"
137                                 size="medium"
138                                 onClick={this.loadMore}
139                             >Load more</Button>}
140                     </Grid>
141                 </Toolbar>
142             </Paper>;
143         }
144
145         changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
146             this.props.onChangePage(page);
147         }
148
149         changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
150             this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
151         }
152
153         loadMore = () => {
154             this.props.onLoadMore(this.props.page + 1);
155         }
156
157         renderContextMenuTrigger = (item: T) =>
158             <Grid container justify="center">
159                 <Tooltip title="More options" disableFocusListener>
160                     <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
161                         <MoreOptionsIcon />
162                     </IconButton>
163                 </Tooltip>
164             </Grid>
165
166         contextMenuColumn: DataColumn<any> = {
167             name: "Actions",
168             selected: true,
169             configurable: false,
170             filters: createTree(),
171             key: "context-actions",
172             render: this.renderContextMenuTrigger
173         };
174     }
175 );