X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/e25469d58a038391fa16184da4f7078a0eae805a..900081d75c21b924f4da42a94c1ca4ef7084a3df:/src/components/data-explorer/data-explorer.tsx

diff --git a/src/components/data-explorer/data-explorer.tsx b/src/components/data-explorer/data-explorer.tsx
index f2fe3be27e..af14db9c9e 100644
--- a/src/components/data-explorer/data-explorer.tsx
+++ b/src/components/data-explorer/data-explorer.tsx
@@ -3,217 +3,154 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { DataTable, DataTableProps, Column, ColumnsConfigurator } from "../../components/data-table";
-import { Typography, Grid, ListItem, Divider, List, ListItemIcon, ListItemText } from '@material-ui/core';
-import IconButton, { IconButtonProps } from '@material-ui/core/IconButton';
+import { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, WithStyles, TablePagination, IconButton } from '@material-ui/core';
 import MoreVertIcon from "@material-ui/icons/MoreVert";
-import Popover from '../popover/popover';
+import { ColumnSelector } from "../column-selector/column-selector";
+import { DataTable, DataColumns } from "../data-table/data-table";
+import { DataColumn, SortDirection } from "../data-table/data-column";
+import { DataTableFilterItem } from '../data-table-filters/data-table-filters';
+import { SearchInput } from '../search-input/search-input';
+import { ArvadosTheme } from "~/common/custom-theme";
+import { DefaultView } from '../default-view/default-view';
+import { IconType } from '../icon/icon';
 
-export interface DataItem {
-    name: string;
-    type: string;
-    owner: string;
-    lastModified: string;
-    fileSize?: number;
-    status?: string;
-}
+type CssRules = 'searchBox' | "toolbar" | 'defaultRoot' | 'defaultMessage' | 'defaultIcon';
+
+const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
+    searchBox: {
+        paddingBottom: theme.spacing.unit * 2
+    },
+    toolbar: {
+        paddingTop: theme.spacing.unit * 2
+    },
+    defaultRoot: {
+        position: 'absolute',
+        width: '80%',
+        left: '50%',
+        top: '50%',
+        transform: 'translate(-50%, -50%)'
+    },
+    defaultMessage: {
+        fontSize: '1.75rem',
+    },
+    defaultIcon: {
+        fontSize: '6rem'
+    }
+});
 
-interface DataExplorerProps {
-    items: DataItem[];
-    onItemClick: (item: DataItem) => void;
+interface DataExplorerDataProps<T> {
+    items: T[];
+    itemsAvailable: number;
+    columns: DataColumns<T>;
+    searchValue: string;
+    rowsPerPage: number;
+    rowsPerPageOptions: number[];
+    page: number;
+    defaultIcon: IconType;
+    defaultMessages: string[];
 }
 
-type DataExplorerState = Pick<DataTableProps<DataItem>, "columns">;
+interface DataExplorerActionProps<T> {
+    onSetColumns: (columns: DataColumns<T>) => void;
+    onSearch: (value: string) => void;
+    onRowClick: (item: T) => void;
+    onRowDoubleClick: (item: T) => void;
+    onColumnToggle: (column: DataColumn<T>) => void;
+    onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
+    onSortToggle: (column: DataColumn<T>) => void;
+    onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
+    onChangePage: (page: number) => void;
+    onChangeRowsPerPage: (rowsPerPage: number) => void;
+    extractKey?: (item: T) => React.Key;
+}
 
-class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState> {
+type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
 
-    state: DataExplorerState = {
-        columns: [
-            {
-                header: "Name",
-                selected: true,
-                render: item => (
-                    <Grid
-                        container
-                        alignItems="center"
-                        wrap="nowrap"
-                        spacing={16}
-                        onClick={() => this.props.onItemClick(item)}
-                    >
-                        <Grid item>
-                            {renderIcon(item)}
-                        </Grid>
-                        <Grid item>
-                            <Typography color="primary">
-                                {item.name}
-                            </Typography>
-                        </Grid>
-                    </Grid>
-                )
-            },
-            {
-                header: "Status",
-                selected: true,
-                render: item => (
-                    <Typography noWrap align="center">
-                        {item.status || "-"}
-                    </Typography>
-                )
-            },
-            {
-                header: "Type",
-                selected: true,
-                render: item => (
-                    <Typography noWrap>
-                        {item.type}
-                    </Typography>
-                )
-            },
-            {
-                header: "Owner",
-                selected: true,
-                render: item => (
-                    <Typography noWrap color="primary">
-                        {item.owner}
-                    </Typography>
-                )
-            },
-            {
-                header: "File size",
-                selected: true,
-                render: ({ fileSize }) => (
-                    <Typography noWrap>
-                        {typeof fileSize === "number" ? formatFileSize(fileSize) : "-"}
-                    </Typography>
-                )
-            },
-            {
-                header: "Last modified",
-                selected: true,
-                render: item => (
-                    <Typography noWrap>
-                        {formatDate(item.lastModified)}
-                    </Typography>
-                )
-            },
-            {
-                header: "Actions",
-                key: "Actions",
-                selected: true,
-                configurable: false,
-                renderHeader: () => (
-                    <Grid container justify="flex-end">
-                        <ColumnsConfigurator
-                            columns={this.state.columns}
-                            onColumnToggle={this.toggleColumn}
-                        />
-                    </Grid>
-                ),
-                render: item => (
-                    <Grid container justify="flex-end">
-                        <Popover triggerComponent={ItemActionsTrigger}>
-                            <List dense>
-                                {[
-                                    {
-                                        icon: "fas fa-users",
-                                        label: "Share"
-                                    },
-                                    {
-                                        icon: "fas fa-sign-out-alt",
-                                        label: "Move to"
-                                    },
-                                    {
-                                        icon: "fas fa-star",
-                                        label: "Add to favourite"
-                                    },
-                                    {
-                                        icon: "fas fa-edit",
-                                        label: "Rename"
-                                    },
-                                    {
-                                        icon: "fas fa-copy",
-                                        label: "Make a copy"
-                                    },
-                                    {
-                                        icon: "fas fa-download",
-                                        label: "Download"
-                                    }].map(renderAction)
-                                }
-                                < Divider />
-                                {
-                                    renderAction({ icon: "fas fa-trash-alt", label: "Remove" })
-                                }
-                            </List>
-                        </Popover>
-                    </Grid>
-                )
+export const DataExplorer = withStyles(styles)(
+    class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
+        componentDidMount() {
+            if (this.props.onSetColumns) {
+                this.props.onSetColumns(this.props.columns);
             }
-        ]
-    };
+        }
+        render() {
+            const {
+                columns, onContextMenu, onFiltersChange, onSortToggle, extractKey,
+                rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
+                items, itemsAvailable, onRowClick, onRowDoubleClick, defaultIcon, defaultMessages, classes
+            } = this.props;
+            return <div>
+                { items.length > 0 ? (
+                    <Paper>
+                        <Toolbar className={classes.toolbar}>
+                            <Grid container justify="space-between" wrap="nowrap" alignItems="center">
+                                <div className={classes.searchBox}>
+                                    <SearchInput
+                                        value={searchValue}
+                                        onSearch={onSearch}/>
+                                </div>
+                                <ColumnSelector
+                                    columns={columns}
+                                    onColumnToggle={onColumnToggle}/>
+                            </Grid>
+                        </Toolbar>
+                        <DataTable
+                            columns={[...columns, this.contextMenuColumn]}
+                            items={items}
+                            onRowClick={(_, item: T) => onRowClick(item)}
+                            onContextMenu={onContextMenu}
+                            onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
+                            onFiltersChange={onFiltersChange}
+                            onSortToggle={onSortToggle}
+                            extractKey={extractKey}/>
+                        <Toolbar>
+                            <Grid container justify="flex-end">
+                                <TablePagination
+                                    count={itemsAvailable}
+                                    rowsPerPage={rowsPerPage}
+                                    rowsPerPageOptions={rowsPerPageOptions}
+                                    page={this.props.page}
+                                    onChangePage={this.changePage}
+                                    onChangeRowsPerPage={this.changeRowsPerPage}
+                                    component="div" />
+                            </Grid>
+                        </Toolbar>
+                    </Paper>
+                ) : (
+                    <DefaultView
+                        classRoot={classes.defaultRoot}
+                        icon={defaultIcon}
+                        classIcon={classes.defaultIcon}
+                        messages={defaultMessages}
+                        classMessage={classes.defaultMessage} />
+                )}
+            </div>;
+        }
 
-    render() {
-        return (
-            <DataTable
-                columns={this.state.columns}
-                items={this.props.items}
-            />
-        );
-    }
+        changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
+            this.props.onChangePage(page);
+        }
 
-    toggleColumn = (column: Column<DataItem>) => {
-        const index = this.state.columns.indexOf(column);
-        const columns = this.state.columns.slice(0);
-        columns.splice(index, 1, { ...column, selected: !column.selected });
-        this.setState({ columns });
-    }
-}
+        changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
+            this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
+        }
 
-const formatDate = (isoDate: string) => {
-    const date = new Date(isoDate);
-    return date.toLocaleString();
-};
+        renderContextMenuTrigger = (item: T) =>
+            <Grid container justify="flex-end">
+                <IconButton onClick={event => this.props.onContextMenu(event, item)}>
+                    <MoreVertIcon/>
+                </IconButton>
+            </Grid>
 
-const formatFileSize = (size: number) => {
-    switch (true) {
-        case size > 1000000000000:
-            return `${size / 1000000000000} TB`;
-        case size > 1000000000:
-            return `${size / 1000000000} GB`;
-        case size > 1000000:
-            return `${size / 1000000} MB`;
-        case size > 1000:
-            return `${size / 1000} KB`;
-        default:
-            return `${size} B`;
+        contextMenuColumn = {
+            name: "Actions",
+            selected: true,
+            configurable: false,
+            sortDirection: SortDirection.NONE,
+            filters: [],
+            key: "context-actions",
+            render: this.renderContextMenuTrigger,
+            width: "auto"
+        };
     }
-};
-
-const renderIcon = (DataItem: DataItem) => {
-    switch (DataItem.type) {
-        case "arvados#group":
-            return <i className="fas fa-folder fa-lg" />;
-        case "arvados#groupList":
-            return <i className="fas fa-th fa-lg" />;
-        default:
-            return <i />;
-    }
-};
-
-const renderAction = (action: { label: string, icon: string }, index?: number) => (
-    <ListItem button key={index}>
-        <ListItemIcon>
-            <i className={action.icon} />
-        </ListItemIcon>
-        <ListItemText>
-            {action.label}
-        </ListItemText>
-    </ListItem>
 );
-
-const ItemActionsTrigger: React.SFC<IconButtonProps> = (props) => (
-    <IconButton {...props}>
-        <MoreVertIcon />
-    </IconButton>
-);
-
-export default DataExplorer;
