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, DataColumns, 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";
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 enum DataTableFetchMode {
40 export interface DataTableDataProps<I> {
42 columns: DataColumns<I, any>;
43 onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: I) => void;
44 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: I) => void;
45 onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: I) => void;
46 onSortToggle: (column: DataColumn<I, any>) => void;
47 onFiltersChange: (filters: DataTableFilters, column: DataColumn<I, any>) => void;
48 extractKey?: (item: I) => React.Key;
50 defaultViewIcon?: IconType;
51 defaultViewMessages?: string[];
52 toggleMSToolbar: (isVisible: boolean) => void;
53 setCheckedListOnStore: (checkedList: TCheckedList) => void;
54 currentRoute?: string;
55 currentRouteUuid: string;
56 checkedList: TCheckedList;
57 selectedResourceUuid: string;
58 setSelectedUuid: (uuid: string | null) => void;
60 detailsPanelResourceUuid?: string;
61 loadDetailsPanel: (uuid: string) => void;
81 | "tableCellWorkflows";
83 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
88 display: "inline-block",
92 background: theme.palette.background.paper,
97 padding: theme.spacing(1),
104 paddingLeft: '0.9rem',
105 marginRight: '0.5rem',
106 backgroundColor: theme.palette.background.paper,
110 backgroundColor: theme.palette.background.paper,
116 paddingLeft: '0.35rem',
117 paddingTop: '0.1rem',
118 marginLeft: '0.5rem',
125 wordWrap: "break-word",
126 paddingRight: "24px",
135 wordWrap: "break-word",
136 paddingRight: "24px",
138 fontSize: "0.8125rem",
139 backgroundColor: theme.palette.background.paper,
142 backgroundColor: `${CustomTheme.palette.grey['300']} !important`
145 backgroundColor: `${CustomTheme.palette.grey['100']} !important`
147 tableCellWorkflows: {
148 "&:nth-last-child(2)": {
154 paddingRight: "24px",
162 color: theme.palette.text.primary,
166 export type TCheckedList = Record<string, boolean>;
168 type DataTableState = {
171 hoveredIndex: number | null;
174 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
176 export const DataTable = withStyles(styles)(
177 class Component<T> extends React.Component<DataTableProps<T>> {
178 state: DataTableState = {
184 componentDidMount(): void {
185 this.initializeCheckedList([]);
186 if(((this.props.items.length > 0) && !this.state.isLoaded) || !this.props.working) {
187 this.setState({ isLoaded: true });
189 if(this.props.detailsPanelResourceUuid !== this.props.selectedResourceUuid) {
190 this.props.loadDetailsPanel(this.props.selectedResourceUuid);
194 componentDidUpdate(prevProps: Readonly<DataTableProps<T>>, prevState: DataTableState) {
195 const { items, currentRouteUuid, setCheckedListOnStore } = this.props;
196 const { isSelected } = this.state;
197 const singleSelected = isExactlyOneSelected(this.props.checkedList);
198 if (prevProps.items !== items) {
199 if (isSelected === true) this.setState({ isSelected: false });
200 if (items.length) this.initializeCheckedList(items);
201 else setCheckedListOnStore({});
203 if (prevProps.currentRoute !== this.props.currentRoute) {
204 this.initializeCheckedList([]);
206 if (singleSelected && singleSelected !== isExactlyOneSelected(prevProps.checkedList)) {
207 this.props.setSelectedUuid(singleSelected);
209 if (!singleSelected && !!currentRouteUuid && !this.isAnySelected()) {
210 this.props.setSelectedUuid(currentRouteUuid);
212 if (!singleSelected && this.isAnySelected()) {
213 this.props.setSelectedUuid(null);
215 if(prevProps.working === false && this.props.working === true) {
216 this.setState({ isLoaded: false });
218 if(prevProps.working === true && this.props.working === false) {
219 this.setState({ isLoaded: true });
221 if((this.props.items.length > 0) && !this.state.isLoaded) {
222 this.setState({ isLoaded: true });
226 componentWillUnmount(): void {
227 this.initializeCheckedList([]);
230 checkBoxColumn: DataColumn<any, any> = {
231 name: "checkBoxColumn",
234 filters: createTree(),
236 const { classes, checkedList } = this.props;
239 className={classes.clickBox}
242 this.handleSelectOne(uuid)
244 onDoubleClick={(ev) => ev.stopPropagation()}
247 data-cy={`multiselect-checkbox-${uuid}`}
250 className={classes.checkBox}
251 checked={checkedList && checkedList[uuid] ? checkedList[uuid] : false}
252 onChange={() => this.handleSelectOne(uuid)}
253 onDoubleClick={(ev) => ev.stopPropagation()}
260 multiselectOptions: DataTableMultiselectOption[] = [
261 { name: "All", fn: list => this.handleSelectAll(list) },
262 { name: "None", fn: list => this.handleSelectNone(list) },
263 { name: "Invert", fn: list => this.handleInvertSelect(list) },
266 initializeCheckedList = (uuids: any[]): void => {
267 const newCheckedList = { ...this.props.checkedList };
269 if(Object.keys(newCheckedList).length === 0){
270 for(const uuid of uuids){
271 newCheckedList[uuid] = false
275 for (const key in newCheckedList) {
276 if (!uuids.includes(key)) {
277 delete newCheckedList[key];
280 this.props.setCheckedListOnStore(newCheckedList);
283 isAllSelected = (list: TCheckedList): boolean => {
284 for (const key in list) {
285 if (list[key] === false) return false;
290 isAnySelected = (): boolean => {
291 const { checkedList } = this.props;
292 if (!checkedList) return false;
293 if (!Object.keys(checkedList).length) return false;
294 for (const key in checkedList) {
295 if (checkedList[key] === true) return true;
300 handleSelectOne = (uuid: string): void => {
301 const { checkedList } = this.props;
302 const newCheckedList = { ...checkedList };
303 newCheckedList[uuid] = !checkedList[uuid];
304 this.setState({ isSelected: this.isAllSelected(newCheckedList) });
305 this.props.setCheckedListOnStore(newCheckedList);
308 handleSelectorSelect = (): void => {
309 const { checkedList } = this.props;
310 const { isSelected } = this.state;
311 isSelected ? this.handleSelectNone(checkedList) : this.handleSelectAll(checkedList);
314 handleSelectAll = (list: TCheckedList): void => {
315 if (Object.keys(list).length) {
316 const newCheckedList = { ...list };
317 for (const key in newCheckedList) {
318 newCheckedList[key] = true;
320 this.setState({ isSelected: true });
321 this.props.setCheckedListOnStore(newCheckedList);
325 handleSelectNone = (list: TCheckedList): void => {
326 const newCheckedList = { ...list };
327 for (const key in newCheckedList) {
328 newCheckedList[key] = false;
330 this.setState({ isSelected: false });
331 this.props.setCheckedListOnStore(newCheckedList);
334 handleInvertSelect = (list: TCheckedList): void => {
335 if (Object.keys(list).length) {
336 const newCheckedList = { ...list };
337 for (const key in newCheckedList) {
338 newCheckedList[key] = !list[key];
340 this.setState({ isSelected: this.isAllSelected(newCheckedList) });
341 this.props.setCheckedListOnStore(newCheckedList);
346 const { items, classes, columns, isNotFound } = this.props;
347 const { isLoaded } = this.state;
348 if (columns.length && columns[0].name === this.checkBoxColumn.name) columns.shift();
349 columns.unshift(this.checkBoxColumn);
351 <div className={classes.root}>
352 <div className={classes.content}>
353 <Table data-cy="data-table" stickyHeader>
355 <TableRow>{this.mapVisibleColumns(this.renderHeadCell)}</TableRow>
357 <TableBody className={classes.tableBody}>{(isLoaded && !isNotFound) && items.map(this.renderBodyRow)}</TableBody>
359 {(!isLoaded || isNotFound || items.length === 0) && this.renderNoItemsPlaceholder(this.props.columns)}
365 renderNoItemsPlaceholder = (columns: DataColumns<T, any>) => {
366 const { isLoaded } = this.state;
367 const { working, isNotFound } = this.props;
368 const dirty = columns.some(column => getTreeDirty("")(column.filters));
369 if (isNotFound && isLoaded) {
371 <DataTableDefaultView
372 icon={this.props.defaultViewIcon}
373 messages={["No items found"]}
377 if (isLoaded === false || working === true) {
379 <DataTableDefaultView
381 messages={["Loading data, please wait"]}
385 // isLoaded && !working && !isNotFound
387 <DataTableDefaultView
388 data-cy="data-table-default-view"
389 icon={this.props.defaultViewIcon}
390 messages={this.props.defaultViewMessages}
391 filtersApplied={dirty}
397 renderHeadCell = (column: DataColumn<T, any>, index: number) => {
398 const { name, key, renderHeader, filters, sort } = column;
399 const { onSortToggle, onFiltersChange, classes, checkedList } = this.props;
400 const { isSelected } = this.state;
401 return column.name === "checkBoxColumn" ? (
404 className={classes.checkBoxCell}>
405 <div className={classes.checkBoxHead}>
406 <Tooltip title={this.state.isSelected ? "Deselect all" : "Select all"}>
409 className={classes.checkBox}
411 disabled={!this.props.items.length}
412 onChange={this.handleSelectorSelect}></input>
414 <DataTableMultiselectPopover
416 disabled={!this.props.items.length}
417 options={this.multiselectOptions}
418 checkedList={checkedList}></DataTableMultiselectPopover>
423 className={classnames(classes.tableHead, index === 1 ? classes.firstTableHead : '')}
427 ) : countNodes(filters) > 0 ? (
428 <DataTableFiltersPopover
429 name={`${name} filters`}
430 mutuallyExclusive={column.mutuallyExclusiveFilters}
431 onChange={filters => onFiltersChange && onFiltersChange(filters, column)}
434 </DataTableFiltersPopover>
437 active={sort.direction !== SortDirection.NONE}
438 direction={sort.direction !== SortDirection.NONE ? sort.direction : undefined}
439 IconComponent={this.ArrowIcon}
441 onClick={() => onSortToggle && onSortToggle(column)}>
451 ArrowIcon = ({ className, ...props }: SvgIconProps) => (
453 data-cy="sort-button"
455 className={this.props.classes.arrowButton}
460 className={classnames(className, this.props.classes.arrow)}
465 renderBodyRow = (item: any, index: number) => {
466 const { onRowClick, onRowDoubleClick, extractKey, classes, selectedResourceUuid, currentRoute } = this.props;
467 const { hoveredIndex } = this.state;
468 const isRowSelected = item === selectedResourceUuid;
469 const getClassnames = (colIndex: number) => {
470 if(currentRoute === '/workflows') return classes.tableCellWorkflows;
471 if(colIndex === 0) return classnames(classes.checkBoxCell, isRowSelected ? classes.selected : index === hoveredIndex ? classes.hovered : "");
472 if(colIndex === 1) return classnames(classes.tableCell, classes.firstTableCell, isRowSelected ? classes.selected : "");
473 return classnames(classes.tableCell, isRowSelected ? classes.selected : "");
475 const handleHover = (index: number | null) => {
476 this.setState({ hoveredIndex: index });
481 data-cy={'data-table-row'}
483 key={extractKey ? extractKey(item) : index}
484 onClick={event => onRowClick && onRowClick(event, item)}
485 onContextMenu={this.handleRowContextMenu(item)}
486 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}
487 selected={isRowSelected}
488 className={isRowSelected ? classes.selected : ""}
489 onMouseEnter={()=>handleHover(index)}
490 onMouseLeave={()=>handleHover(null)}
492 {this.mapVisibleColumns((column, colIndex) => (
494 key={column.key || colIndex}
495 data-cy={column.key || colIndex}
496 className={getClassnames(colIndex)}>
497 {column.render(item)}
504 mapVisibleColumns = (fn: (column: DataColumn<T, any>, index: number) => React.ReactElement<any>) => {
505 return this.props.columns.filter(column => column.selected).map(fn);
508 handleRowContextMenu = (item: T) => (event: React.MouseEvent<HTMLElement>) => this.props.onContextMenu(event, item);