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