21386: project 404 vs empty working Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[arvados.git] / services / workbench2 / src / components / tree / virtual-tree.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import classnames from "classnames";
7 import { StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core/styles';
8 import { ReactElement } from "react";
9 import { FixedSizeList, ListChildComponentProps } from "react-window";
10 import AutoSizer from "react-virtualized-auto-sizer";
11
12 import { ArvadosTheme } from 'common/custom-theme';
13 import { TreeItem, TreeProps, TreeItemStatus } from './tree';
14 import { ListItem, Radio, Checkbox, CircularProgress, ListItemIcon } from '@material-ui/core';
15 import { SidePanelRightArrowIcon } from '../icon/icon';
16
17 type CssRules = 'list'
18     | 'listItem'
19     | 'active'
20     | 'loader'
21     | 'toggableIconContainer'
22     | 'iconClose'
23     | 'renderContainer'
24     | 'iconOpen'
25     | 'toggableIcon'
26     | 'checkbox'
27     | 'virtualFileTree'
28     | 'virtualizedList';
29
30 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
31     list: {
32         padding: '3px 0px',
33     },
34     virtualFileTree: {
35         "&:last-child": {
36             paddingBottom: 20
37           }
38     },
39     virtualizedList: {
40         height: '200px',
41     },
42     listItem: {
43         padding: '3px 0px',
44     },
45     loader: {
46         position: 'absolute',
47         transform: 'translate(0px)',
48         top: '3px'
49     },
50     toggableIconContainer: {
51         color: theme.palette.grey["700"],
52         height: '14px',
53         width: '14px',
54     },
55     toggableIcon: {
56         fontSize: '14px'
57     },
58     renderContainer: {
59         flex: 1
60     },
61     active: {
62         color: theme.palette.primary.main,
63     },
64     iconClose: {
65         transition: 'all 0.1s ease',
66     },
67     iconOpen: {
68         transition: 'all 0.1s ease',
69         transform: 'rotate(90deg)',
70     },
71     checkbox: {
72         width: theme.spacing.unit * 3,
73         height: theme.spacing.unit * 3,
74         margin: `0 ${theme.spacing.unit}px`,
75         padding: 0,
76         color: theme.palette.grey["500"],
77     }
78 });
79
80 export interface VirtualTreeItem<T> extends TreeItem<T> {
81     itemCount?: number;
82     level?: number;
83 }
84
85 // For some reason, on TSX files it isn't accepted just one generic param, so
86 // I'm using <T, _> as a workaround.
87 // eslint-disable-next-line
88 export const Row =  <T, _>(itemList: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) => withStyles(styles)(
89     (props: React.PropsWithChildren<ListChildComponentProps> & WithStyles<CssRules>) => {
90         const { index, style, classes } = props;
91         const it = itemList[index];
92         const level = it.level || 0;
93         const { toggleItemActive, disableRipple, currentItemUuid, useRadioButtons } = treeProps;
94         const { listItem, loader, toggableIconContainer, renderContainer, virtualFileTree } = classes;
95         const { levelIndentation = 20, itemRightPadding = 20 } = treeProps;
96
97         const showSelection = typeof treeProps.showSelection === 'function'
98             ? treeProps.showSelection
99             : () => treeProps.showSelection ? true : false;
100
101         const handleRowContextMenu = (item: VirtualTreeItem<T>) =>
102             (event: React.MouseEvent<HTMLElement>) => {
103                 treeProps.onContextMenu(event, item);
104             };
105
106         const handleToggleItemOpen = (item: VirtualTreeItem<T>) =>
107             (event: React.MouseEvent<HTMLElement>) => {
108                 event.stopPropagation();
109                 treeProps.toggleItemOpen(event, item);
110             };
111
112         const getToggableIconClassNames = (isOpen?: boolean, isActive?: boolean) => {
113             const { iconOpen, iconClose, active, toggableIcon } = props.classes;
114             return classnames(toggableIcon, {
115                 [iconOpen]: isOpen,
116                 [iconClose]: !isOpen,
117                 [active]: isActive
118             });
119         };
120
121         const isSidePanelIconNotNeeded = (status: string, itemCount: number) => {
122             return status === TreeItemStatus.PENDING ||
123                 (status === TreeItemStatus.LOADED && itemCount === 0);
124         };
125
126         const getProperArrowAnimation = (status: string, itemCount: number) => {
127             return isSidePanelIconNotNeeded(status, itemCount) ? <span /> : <SidePanelRightArrowIcon style={{ fontSize: '14px' }} />;
128         };
129
130         const handleCheckboxChange = (item: VirtualTreeItem<T>) => {
131             const { toggleItemSelection } = treeProps;
132             return toggleItemSelection
133                 ? (event: React.MouseEvent<HTMLElement>) => {
134                     event.stopPropagation();
135                     toggleItemSelection(event, item);
136                 }
137                 : undefined;
138         };
139
140         return <div className={virtualFileTree} data-cy='virtual-file-tree' style={style}>
141             <ListItem button className={listItem}
142                 style={{
143                     paddingLeft: (level + 1) * levelIndentation,
144                     paddingRight: itemRightPadding,
145                 }}
146                 disableRipple={disableRipple}
147                 onClick={event => toggleItemActive(event, it)}
148                 selected={showSelection(it) && it.id === currentItemUuid}
149                 onContextMenu={handleRowContextMenu(it)}>
150                 {it.status === TreeItemStatus.PENDING ?
151                     <CircularProgress size={10} className={loader} /> : null}
152                 <i onClick={handleToggleItemOpen(it)}
153                     className={toggableIconContainer}>
154                     <ListItemIcon className={getToggableIconClassNames(it.open, it.active)}>
155                         {getProperArrowAnimation(it.status, it.itemCount!)}
156                     </ListItemIcon>
157                 </i>
158                 {showSelection(it) && !useRadioButtons &&
159                     <Checkbox
160                         checked={it.selected}
161                         className={classes.checkbox}
162                         color="primary"
163                         onClick={handleCheckboxChange(it)} />}
164                 {showSelection(it) && useRadioButtons &&
165                     <Radio
166                         checked={it.selected}
167                         className={classes.checkbox}
168                         color="primary" />}
169                 <div className={renderContainer}>
170                     {render(it, level)}
171                 </div>
172             </ListItem>
173         </div>;
174     });
175
176 const itemSize = 30;
177
178 // eslint-disable-next-line
179 export const VirtualList = <T, _>(height: number, width: number, items: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) =>
180     <FixedSizeList
181         height={height}
182         itemCount={items.length}
183         itemSize={itemSize}
184         width={width}
185     >
186         {Row(items, render, treeProps)}
187     </FixedSizeList>;
188
189 export const VirtualTree = withStyles(styles)(
190     class Component<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
191         render(): ReactElement<any> {
192             const { items, render } = this.props;
193             return <AutoSizer>
194                 {({ height, width }) => {
195                     return VirtualList(height, width, items || [], render, this.props);
196                 }}
197             </AutoSizer>;
198         }
199     }
200 );