1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from "react";
6 import { CustomStyleRulesCallback } from 'common/custom-theme';
16 } from "@mui/material";
17 import { WithStyles } from '@mui/styles';
18 import withStyles from '@mui/styles/withStyles';
19 import classnames from "classnames";
20 import { DataColumn, SortDirection } from "./data-column";
21 import { DataTableDefaultView } from "../data-table-default-view/data-table-default-view";
22 import { DataTableFilters } from "../data-table-filters/data-table-filters-tree";
23 import { DataTableMultiselectPopover } from "../data-table-multiselect-popover/data-table-multiselect-popover";
24 import { DataTableFiltersPopover } from "../data-table-filters/data-table-filters-popover";
25 import { countNodes, getTreeDirty } from "models/tree";
26 import { IconType } from "components/icon/icon";
27 import { SvgIconProps } from "@mui/material/SvgIcon";
28 import ArrowDownwardIcon from "@mui/icons-material/ArrowDownward";
29 import { createTree } from "models/tree";
30 import { DataTableMultiselectOption } from "../data-table-multiselect-popover/data-table-multiselect-popover";
31 import { isExactlyOneSelected } from "store/multiselect/multiselect-actions";
32 import { PendingIcon } from "components/icon/icon";
33 import { CustomTheme, ArvadosTheme } from "common/custom-theme";
35 export type DataColumns<I, R> = Array<DataColumn<I, R>>;
37 export enum DataTableFetchMode {
42 export interface DataTableDataProps<I> {
44 columns: DataColumns<I, any>;
45 onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: I) => void;
46 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: I) => void;
47 onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: I) => void;
48 onSortToggle: (column: DataColumn<I, any>) => void;
49 onFiltersChange: (filters: DataTableFilters, column: DataColumn<I, any>) => void;
50 extractKey?: (item: I) => React.Key;
52 defaultViewIcon?: IconType;
53 defaultViewMessages?: string[];
54 toggleMSToolbar: (isVisible: boolean) => void;
55 setCheckedListOnStore: (checkedList: TCheckedList) => void;
56 currentRoute?: string;
57 currentRouteUuid: string;
58 checkedList: TCheckedList;
59 selectedResourceUuid: string;
60 setSelectedUuid: (uuid: string | null) => void;
62 detailsPanelResourceUuid?: string;
63 loadDetailsPanel: (uuid: string) => void;
83 | "tableCellWorkflows";
85 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
90 display: "inline-block",
94 background: theme.palette.background.paper,
99 padding: theme.spacing(1),
106 paddingLeft: '0.9rem',
107 marginRight: '0.5rem',
108 backgroundColor: theme.palette.background.paper,
112 backgroundColor: theme.palette.background.paper,
118 paddingLeft: '0.35rem',
119 paddingTop: '0.1rem',
120 marginLeft: '0.5rem',
127 wordWrap: "break-word",
128 paddingRight: "24px",
137 wordWrap: "break-word",
138 paddingRight: "24px",
140 fontSize: "0.8125rem",
141 backgroundColor: theme.palette.background.paper,
144 backgroundColor: `${CustomTheme.palette.grey['300']} !important`
147 backgroundColor: `${CustomTheme.palette.grey['100']} !important`
149 tableCellWorkflows: {
150 "&:nth-last-child(2)": {
156 paddingRight: "24px",
164 color: theme.palette.text.primary,
168 export type TCheckedList = Record<string, boolean>;
170 type DataTableState = {
173 hoveredIndex: number | null;
176 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
178 export const DataTable = withStyles(styles)(
179 class Component<T> extends React.Component<DataTableProps<T>> {
180 state: DataTableState = {
186 componentDidMount(): void {
187 this.initializeCheckedList([]);
188 if((this.props.items.length > 0) && !this.state.isLoaded) {
189 this.setState({ isLoaded: true });
193 componentDidUpdate(prevProps: Readonly<DataTableProps<T>>, prevState: DataTableState) {
194 const { items, currentRouteUuid, setCheckedListOnStore } = this.props;
195 const { isSelected } = this.state;
196 const singleSelected = isExactlyOneSelected(this.props.checkedList);
197 if (prevProps.items !== items) {
198 if (isSelected === true) this.setState({ isSelected: false });
199 if (items.length) this.initializeCheckedList(items);
200 else setCheckedListOnStore({});
202 if (prevProps.currentRoute !== this.props.currentRoute) {
203 this.initializeCheckedList([]);
205 if (singleSelected && singleSelected !== isExactlyOneSelected(prevProps.checkedList)) {
206 this.props.setSelectedUuid(singleSelected);
208 if (!singleSelected && !!currentRouteUuid && !this.isAnySelected()) {
209 this.props.setSelectedUuid(currentRouteUuid);
211 if (!singleSelected && this.isAnySelected()) {
212 this.props.setSelectedUuid(null);
214 if(prevProps.working === true && this.props.working === false) {
215 this.setState({ isLoaded: true });
217 if((this.props.items.length > 0) && !this.state.isLoaded) {
218 this.setState({ isLoaded: true });
220 if(this.props.detailsPanelResourceUuid !== this.props.selectedResourceUuid) {
221 this.props.loadDetailsPanel(this.props.selectedResourceUuid);
225 componentWillUnmount(): void {
226 this.initializeCheckedList([]);
229 checkBoxColumn: DataColumn<any, any> = {
230 name: "checkBoxColumn",
233 filters: createTree(),
235 const { classes, checkedList } = this.props;
238 className={classes.clickBox}
241 this.handleSelectOne(uuid)
243 onDoubleClick={(ev) => ev.stopPropagation()}
246 data-cy={`multiselect-checkbox-${uuid}`}
249 className={classes.checkBox}
250 checked={checkedList && checkedList[uuid] ? checkedList[uuid] : false}
251 onChange={() => this.handleSelectOne(uuid)}
252 onDoubleClick={(ev) => ev.stopPropagation()}
259 multiselectOptions: DataTableMultiselectOption[] = [
260 { name: "All", fn: list => this.handleSelectAll(list) },
261 { name: "None", fn: list => this.handleSelectNone(list) },
262 { name: "Invert", fn: list => this.handleInvertSelect(list) },
265 initializeCheckedList = (uuids: any[]): void => {
266 const newCheckedList = { ...this.props.checkedList };
268 if(Object.keys(newCheckedList).length === 0){
269 for(const uuid of uuids){
270 newCheckedList[uuid] = false
274 for (const key in newCheckedList) {
275 if (!uuids.includes(key)) {
276 delete newCheckedList[key];
279 this.props.setCheckedListOnStore(newCheckedList);
282 isAllSelected = (list: TCheckedList): boolean => {
283 for (const key in list) {
284 if (list[key] === false) return false;
289 isAnySelected = (): boolean => {
290 const { checkedList } = this.props;
291 if (!checkedList) return false;
292 if (!Object.keys(checkedList).length) return false;
293 for (const key in checkedList) {
294 if (checkedList[key] === true) return true;
299 handleSelectOne = (uuid: string): void => {
300 const { checkedList } = this.props;
301 const newCheckedList = { ...checkedList };
302 newCheckedList[uuid] = !checkedList[uuid];
303 this.setState({ isSelected: this.isAllSelected(newCheckedList) });
304 this.props.setCheckedListOnStore(newCheckedList);
307 handleSelectorSelect = (): void => {
308 const { checkedList } = this.props;
309 const { isSelected } = this.state;
310 isSelected ? this.handleSelectNone(checkedList) : this.handleSelectAll(checkedList);
313 handleSelectAll = (list: TCheckedList): void => {
314 if (Object.keys(list).length) {
315 const newCheckedList = { ...list };
316 for (const key in newCheckedList) {
317 newCheckedList[key] = true;
319 this.setState({ isSelected: true });
320 this.props.setCheckedListOnStore(newCheckedList);
324 handleSelectNone = (list: TCheckedList): void => {
325 const newCheckedList = { ...list };
326 for (const key in newCheckedList) {
327 newCheckedList[key] = false;
329 this.setState({ isSelected: false });
330 this.props.setCheckedListOnStore(newCheckedList);
333 handleInvertSelect = (list: TCheckedList): void => {
334 if (Object.keys(list).length) {
335 const newCheckedList = { ...list };
336 for (const key in newCheckedList) {
337 newCheckedList[key] = !list[key];
339 this.setState({ isSelected: this.isAllSelected(newCheckedList) });
340 this.props.setCheckedListOnStore(newCheckedList);
345 const { items, classes, columns, isNotFound } = this.props;
346 const { isLoaded } = this.state;
347 if (columns.length && columns[0].name === this.checkBoxColumn.name) columns.shift();
348 columns.unshift(this.checkBoxColumn);
350 <div className={classes.root}>
351 <div className={classes.content}>
352 <Table data-cy="data-table" stickyHeader>
354 <TableRow>{this.mapVisibleColumns(this.renderHeadCell)}</TableRow>
356 <TableBody className={classes.tableBody}>{(isLoaded && !isNotFound) && items.map(this.renderBodyRow)}</TableBody>
358 {(!isLoaded || isNotFound || items.length === 0) && this.renderNoItemsPlaceholder(this.props.columns)}
364 renderNoItemsPlaceholder = (columns: DataColumns<T, any>) => {
365 const { isLoaded } = this.state;
366 const { working, isNotFound } = this.props;
367 const dirty = columns.some(column => getTreeDirty("")(column.filters));
368 if (isNotFound && isLoaded) {
370 <DataTableDefaultView
371 icon={this.props.defaultViewIcon}
372 messages={["No items found"]}
376 if (isLoaded === false || working === true) {
378 <DataTableDefaultView
380 messages={["Loading data, please wait"]}
384 // isLoaded && !working && !isNotFound
386 <DataTableDefaultView
387 icon={this.props.defaultViewIcon}
388 messages={this.props.defaultViewMessages}
389 filtersApplied={dirty}
395 renderHeadCell = (column: DataColumn<T, any>, index: number) => {
396 const { name, key, renderHeader, filters, sort } = column;
397 const { onSortToggle, onFiltersChange, classes, checkedList } = this.props;
398 const { isSelected } = this.state;
399 return column.name === "checkBoxColumn" ? (
402 className={classes.checkBoxCell}>
403 <div className={classes.checkBoxHead}>
404 <Tooltip title={this.state.isSelected ? "Deselect all" : "Select all"}>
407 className={classes.checkBox}
409 disabled={!this.props.items.length}
410 onChange={this.handleSelectorSelect}></input>
412 <DataTableMultiselectPopover
414 disabled={!this.props.items.length}
415 options={this.multiselectOptions}
416 checkedList={checkedList}></DataTableMultiselectPopover>
421 className={classnames(classes.tableHead, index === 1 ? classes.firstTableHead : '')}
425 ) : countNodes(filters) > 0 ? (
426 <DataTableFiltersPopover
427 name={`${name} filters`}
428 mutuallyExclusive={column.mutuallyExclusiveFilters}
429 onChange={filters => onFiltersChange && onFiltersChange(filters, column)}
432 </DataTableFiltersPopover>
435 active={sort.direction !== SortDirection.NONE}
436 direction={sort.direction !== SortDirection.NONE ? sort.direction : undefined}
437 IconComponent={this.ArrowIcon}
439 onClick={() => onSortToggle && onSortToggle(column)}>
449 ArrowIcon = ({ className, ...props }: SvgIconProps) => (
451 data-cy="sort-button"
453 className={this.props.classes.arrowButton}
458 className={classnames(className, this.props.classes.arrow)}
463 renderBodyRow = (item: any, index: number) => {
464 const { onRowClick, onRowDoubleClick, extractKey, classes, selectedResourceUuid, currentRoute } = this.props;
465 const { hoveredIndex } = this.state;
466 const isRowSelected = item === selectedResourceUuid;
467 const getClassnames = (colIndex: number) => {
468 if(currentRoute === '/workflows') return classes.tableCellWorkflows;
469 if(colIndex === 0) return classnames(classes.checkBoxCell, isRowSelected ? classes.selected : index === hoveredIndex ? classes.hovered : "");
470 if(colIndex === 1) return classnames(classes.tableCell, classes.firstTableCell, isRowSelected ? classes.selected : "");
471 return classnames(classes.tableCell, isRowSelected ? classes.selected : "");
473 const handleHover = (index: number | null) => {
474 this.setState({ hoveredIndex: index });
479 data-cy={'data-table-row'}
481 key={extractKey ? extractKey(item) : index}
482 onClick={event => onRowClick && onRowClick(event, item)}
483 onContextMenu={this.handleRowContextMenu(item)}
484 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}
485 selected={isRowSelected}
486 className={isRowSelected ? classes.selected : ""}
487 onMouseEnter={()=>handleHover(index)}
488 onMouseLeave={()=>handleHover(null)}
490 {this.mapVisibleColumns((column, colIndex) => (
492 key={column.key || colIndex}
493 data-cy={column.key || colIndex}
494 className={getClassnames(colIndex)}>
495 {column.render(item)}
502 mapVisibleColumns = (fn: (column: DataColumn<T, any>, index: number) => React.ReactElement<any>) => {
503 return this.props.columns.filter(column => column.selected).map(fn);
506 handleRowContextMenu = (item: T) => (event: React.MouseEvent<HTMLElement>) => this.props.onContextMenu(event, item);