X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/00dd0a8f134729754f01f24f642fe82c08873dae..b5b75b89e923864389f814af97974a55b69a343f:/src/components/collection-panel-files/collection-panel-files.tsx?ds=sidebyside

diff --git a/src/components/collection-panel-files/collection-panel-files.tsx b/src/components/collection-panel-files/collection-panel-files.tsx
index 4e2e08cd..2ba29d44 100644
--- a/src/components/collection-panel-files/collection-panel-files.tsx
+++ b/src/components/collection-panel-files/collection-panel-files.tsx
@@ -8,24 +8,39 @@ import { connect } from 'react-redux';
 import { FixedSizeList } from "react-window";
 import AutoSizer from "react-virtualized-auto-sizer";
 import servicesProvider from 'common/service-provider';
-import { CustomizeTableIcon, DownloadIcon } from 'components/icon/icon';
+import { CustomizeTableIcon, DownloadIcon, MoreOptionsIcon } from 'components/icon/icon';
 import { SearchInput } from 'components/search-input/search-input';
-import { ListItemIcon, StyleRulesCallback, Theme, WithStyles, withStyles, Tooltip, IconButton, Checkbox, CircularProgress, Button } from '@material-ui/core';
+import {
+    ListItemIcon,
+    StyleRulesCallback,
+    Theme,
+    WithStyles,
+    withStyles,
+    Tooltip,
+    IconButton,
+    Checkbox,
+    CircularProgress,
+    Button,
+} from '@material-ui/core';
 import { FileTreeData } from '../file-tree/file-tree-data';
 import { TreeItem, TreeItemStatus } from '../tree/tree';
 import { RootState } from 'store/store';
 import { WebDAV, WebDAVRequestConfig } from 'common/webdav';
 import { AuthState } from 'store/auth/auth-reducer';
 import { extractFilesData } from 'services/collection-service/collection-service-files-response';
-import { DefaultIcon, DirectoryIcon, FileIcon, BackIcon, SidePanelRightArrowIcon } from 'components/icon/icon';
+import {
+    DefaultIcon,
+    DirectoryIcon,
+    FileIcon,
+    BackIcon,
+    SidePanelRightArrowIcon
+} from 'components/icon/icon';
 import { setCollectionFiles } from 'store/collection-panel/collection-panel-files/collection-panel-files-actions';
 import { sortBy } from 'lodash';
 import { formatFileSize } from 'common/formatters';
 import { getInlineFileUrl, sanitizeToken } from 'views-components/context-menu/actions/helpers';
-import _ from 'lodash';
 
 export interface CollectionPanelFilesProps {
-    items: any;
     isWritable: boolean;
     onUploadDataClick: (targetLocation?: string) => void;
     onSearchChange: (searchValue: string) => void;
@@ -40,13 +55,37 @@ export interface CollectionPanelFilesProps {
     collectionPanel: any;
 }
 
-type CssRules = "backButton" | "backButtonHidden" | "pathPanelPathWrapper" | "uploadButton" | "uploadIcon" | "loader" | "wrapper" | "dataWrapper" | "row" | "rowEmpty" | "leftPanel" | "rightPanel" | "pathPanel" | "pathPanelItem" | "rowName" | "listItemIcon" | "rowActive" | "pathPanelMenu" | "rowSelection" | "leftPanelHidden" | "leftPanelVisible" | "searchWrapper" | "searchWrapperHidden";
+type CssRules = "backButton"
+    | "backButtonHidden"
+    | "pathPanelPathWrapper"
+    | "uploadButton"
+    | "uploadIcon"
+    | "moreOptionsButton"
+    | "moreOptions"
+    | "loader"
+    | "wrapper"
+    | "dataWrapper"
+    | "row"
+    | "rowEmpty"
+    | "leftPanel"
+    | "rightPanel"
+    | "pathPanel"
+    | "pathPanelItem"
+    | "rowName"
+    | "listItemIcon"
+    | "rowActive"
+    | "pathPanelMenu"
+    | "rowSelection"
+    | "leftPanelHidden"
+    | "leftPanelVisible"
+    | "searchWrapper"
+    | "searchWrapperHidden";
 
 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
     wrapper: {
         display: 'flex',
         minHeight: '600px',
-        color: 'rgba(0, 0, 0, 0.87)',
+        color: 'rgba(0,0,0,0.87)',
         fontSize: '0.875rem',
         fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
         fontWeight: 400,
@@ -115,8 +154,8 @@ const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
         marginTop: '-15px',
     },
     pathPanel: {
-        padding: '1rem',
-        marginBottom: '1rem',
+        padding: '0.5rem',
+        marginBottom: '0.5rem',
         backgroundColor: '#fff',
         boxShadow: '0px 1px 3px 0px rgb(0 0 0 / 20%), 0px 1px 1px 0px rgb(0 0 0 / 14%), 0px 2px 1px -1px rgb(0 0 0 / 12%)',
     },
@@ -125,7 +164,7 @@ const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
     },
     leftPanel: {
         flex: 0,
-        padding: '1rem',
+        padding: '0 1rem 1rem',
         marginRight: '1rem',
         whiteSpace: 'nowrap',
         position: 'relative',
@@ -156,8 +195,8 @@ const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
     rightPanel: {
         flex: '50%',
         padding: '1rem',
-        paddingTop: '2rem',
-        marginTop: '-1rem',
+        paddingTop: '0.5rem',
+        marginTop: '-0.5rem',
         position: 'relative',
         backgroundColor: '#fff',
         boxShadow: '0px 3px 3px 0px rgb(0 0 0 / 20%), 0px 3px 1px 0px rgb(0 0 0 / 14%), 0px 3px 1px -1px rgb(0 0 0 / 12%)',
@@ -170,30 +209,27 @@ const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
     },
     uploadButton: {
         float: 'right',
-    }
+    },
+    moreOptionsButton: {
+        width: theme.spacing.unit * 3,
+        height: theme.spacing.unit * 3,
+        marginRight: theme.spacing.unit,
+        marginTop: 'auto',
+        marginBottom: 'auto',
+        justifyContent: 'center',
+    },
+    moreOptions: {
+        position: 'absolute'
+    },
 });
 
 const pathPromise = {};
 
-let prevState = {};
-function difference(object, base) {
-	function changes(object, base) {
-		return _.transform(object, function(result, value, key) {
-			if (!_.isEqual(value, base[key])) {
-				result[key] = (_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value;
-			}
-		});
-	}
-	return changes(object, base);
-}
 export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState) => ({
     auth: state.auth,
     collectionPanel: state.collectionPanel,
     collectionPanelFiles: state.collectionPanelFiles,
 }))((props: CollectionPanelFilesProps & WithStyles<CssRules> & { auth: AuthState }) => {
-    const diff = difference(props, prevState);
-    prevState = props;
-    console.log('---> render CollectionPanel <------', diff);
     const { classes, onItemMenuOpen, onUploadDataClick, isWritable, dispatch, collectionPanelFiles, collectionPanel } = props;
     const { apiToken, config } = props.auth;
 
@@ -210,8 +246,8 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
     };
 
     const parentRef = React.useRef(null);
-    const [path, setPath]: any = React.useState([]);
-    const [pathData, setPathData]: any = React.useState({});
+    const [path, setPath] = React.useState<string[]>([]);
+    const [pathData, setPathData] = React.useState({});
     const [isLoading, setIsLoading] = React.useState(false);
     const [leftSearch, setLeftSearch] = React.useState('');
     const [rightSearch, setRightSearch] = React.useState('');
@@ -224,14 +260,12 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
 
     React.useEffect(() => {
         if (props.currentItemUuid) {
-            console.log(' --> useEffect current UUID: ', props.currentItemUuid);
             setPathData({});
             setPath([props.currentItemUuid]);
         }
     }, [props.currentItemUuid]);
 
     const fetchData = (keys, ignoreCache = false) => {
-        console.log('---> fetchData', keys);
         const keyArray = Array.isArray(keys) ? keys : [keys];
 
         Promise.all(keyArray.filter(key => !!key)
@@ -239,14 +273,13 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
                 const dataExists = !!pathData[key];
                 const runningRequest = pathPromise[key];
 
-                if ((!dataExists || ignoreCache) && (!runningRequest || ignoreCache)) {
+                if (ignoreCache || (!dataExists && !runningRequest)) {
                     if (!isLoading) {
                         setIsLoading(true);
                     }
 
                     pathPromise[key] = true;
 
-                    console.log('>>> fetching data for key', key);
                     return webdavClient.propfind(`c=${key}`, webDAVRequestConfig);
                 }
 
@@ -257,7 +290,6 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
         .then((requests) => {
             const newState = requests.map((request, index) => {
                 if (request && request.responseXML != null) {
-                    console.log(">>> got data for key", keyArray[index]);
                     const key = keyArray[index];
                     const result: any = extractFilesData(request.responseXML);
                     const sortedResult = sortBy(result, (n) => n.name).sort((n1, n2) => {
@@ -287,7 +319,6 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
 
     React.useEffect(() => {
         if (rightKey) {
-            console.log('---> useEffect rightKey:', rightKey);
             fetchData(rightKey);
             setLeftSearch('');
             setRightSearch('');
@@ -297,19 +328,12 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
     const currentPDH = (collectionPanel.item || {}).portableDataHash;
     React.useEffect(() => {
         if (currentPDH) {
-            console.log('---> useEffect PDH change:', currentPDH);
-            // Avoid fetching the same content level twice
-            if (leftKey !== rightKey) {
-                fetchData([leftKey, rightKey], true);
-            } else {
-                fetchData(rightKey, true);
-            }
+            fetchData([leftKey, rightKey], true);
         }
     }, [currentPDH]); // eslint-disable-line react-hooks/exhaustive-deps
 
     React.useEffect(() => {
         if (rightData) {
-            console.log('---> useEffect rightData:', rightData, 'search:', rightSearch);
             const filtered = rightData.filter(({ name }) => name.indexOf(rightSearch) > -1);
             setCollectionFiles(filtered, false)(dispatch);
         }
@@ -339,14 +363,12 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
                 onItemMenuOpen(event, item, isWritable);
             }
         },
-        [onItemMenuOpen, isWritable, rightData] // eslint-disable-line react-hooks/exhaustive-deps
-    );
+        [onItemMenuOpen, isWritable, rightData]);
 
     React.useEffect(() => {
         let node = null;
 
-        if (parentRef && parentRef.current) {
-            console.log('---> useEffect parentRef:', parentRef);
+        if (parentRef?.current) {
             node = parentRef.current;
             (node as any).addEventListener('contextmenu', handleRightClick);
         }
@@ -361,17 +383,23 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
     const handleClick = React.useCallback(
         (event: any) => {
             let isCheckbox = false;
+            let isMoreButton = false;
             let elem = event.target;
 
             if (elem.type === 'checkbox') {
                 isCheckbox = true;
             }
+            // The "More options" button click event could be triggered on its
+            // internal graphic element.
+            else if ((elem.dataset && elem.dataset.id === 'moreOptions') || (elem.parentNode && elem.parentNode.dataset && elem.parentNode.dataset.id === 'moreOptions')) {
+                isMoreButton = true;
+            }
 
             while (elem && elem.dataset && !elem.dataset.item) {
                 elem = elem.parentNode;
             }
 
-            if (elem && elem.dataset && !isCheckbox) {
+            if (elem && elem.dataset && !isCheckbox && !isMoreButton) {
                 const { parentPath, subfolderPath, breadcrumbPath, type } = elem.dataset;
 
                 if (breadcrumbPath) {
@@ -404,6 +432,14 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
                 const item = collectionPanelFiles[id];
                 props.onSelectionToggle(event, item);
             }
+            if (isMoreButton) {
+                const { id } = elem.dataset;
+                const item: any = {
+                    id,
+                    data: rightData.find((elem) => elem.id === id),
+                };
+                onItemMenuOpen(event, item, isWritable);
+            }
         },
         [path, setPath, collectionPanelFiles] // eslint-disable-line react-hooks/exhaustive-deps
     );
@@ -447,18 +483,15 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
     return <div data-cy="collection-files-panel" onClick={handleClick} ref={parentRef}>
         <div className={classes.pathPanel}>
             <div className={classes.pathPanelPathWrapper}>
-                { path.map((p: string, index: number) => <span
-                    key={`${index}-${p}`}
-                    data-item="true"
-                    className={classes.pathPanelItem}
-                    data-breadcrumb-path={p}
-                >
+            { path.map( (p: string, index: number) =>
+                <span key={`${index}-${p}`} data-item="true"
+                className={classes.pathPanelItem} data-breadcrumb-path={p}>
                     <span className={classes.rowActive}>{index === 0 ? 'Home' : p}</span> <b>/</b>&nbsp;
-                </span>) }
+                </span>)
+            }
             </div>
             <Tooltip className={classes.pathPanelMenu} title="More options" disableFocusListener>
-                <IconButton
-                    data-cy='collection-files-panel-options-btn'
+                <IconButton data-cy='collection-files-panel-options-btn'
                     onClick={(ev) => {
                         onOptionsMenuOpen(ev, isWritable);
                     }}>
@@ -467,128 +500,51 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
             </Tooltip>
         </div>
         <div className={classes.wrapper}>
-            <div className={classNames(classes.leftPanel, path.length > 1 ? classes.leftPanelVisible : classes.leftPanelHidden)}  data-cy="collection-files-left-panel">
+            <div className={classNames(classes.leftPanel, path.length > 1 ? classes.leftPanelVisible : classes.leftPanelHidden)} data-cy="collection-files-left-panel">
                 <Tooltip title="Go back" className={path.length > 1 ? classes.backButton : classes.backButtonHidden}>
                     <IconButton onClick={() => setPath([...path.slice(0, path.length -1)])}>
                         <BackIcon />
                     </IconButton>
                 </Tooltip>
-            </div>
-            <div className={classes.wrapper}>
-                <div className={classNames(classes.leftPanel, path.length > 1 ? classes.leftPanelVisible : classes.leftPanelHidden)}  data-cy="collection-files-left-panel">
-                    <Tooltip title="Go back" className={path.length > 1 ? classes.backButton : classes.backButtonHidden}>
-                        <IconButton onClick={() => setPath([...path.slice(0, path.length -1)])}>
-                            <BackIcon />
-                        </IconButton>
-                    </Tooltip>
-                    <div className={path.length > 1 ? classes.searchWrapper : classes.searchWrapperHidden}>
-                        <SearchInput selfClearProp={leftKey} label="Search" value={leftSearch} onSearch={setLeftSearch} />
-                    </div>
-                    <div className={classes.dataWrapper}>
-                        {
-                            leftData ?
-                                <AutoSizer defaultWidth={0}>
-                                    {({ height, width }) => {
-                                        const filtered = leftData.filter(({ name }) => name.indexOf(leftSearch) > -1);
-
-                                        return !!filtered.length ? <FixedSizeList
-                                            height={height}
-                                            itemCount={filtered.length}
-                                            itemSize={35}
-                                            width={width}
-                                        >
-                                            {
-                                                ({ index, style }) => {
-                                                    const { id, type, name } = filtered[index];
-
-                                                    return <div
-                                                        data-id={id}
-                                                        style={style}
-                                                        data-item="true"
-                                                        data-type={type}
-                                                        data-parent-path={name}
-                                                        className={classNames(classes.row, getActiveClass(name))}
-                                                        key={id}>
-                                                            {getItemIcon(type, getActiveClass(name))}
-                                                            <div className={classes.rowName}>
-                                                                {name}
-                                                            </div>
-                                                            {
-                                                                getActiveClass(name) ? <SidePanelRightArrowIcon
-                                                                    style={{ display: 'inline', marginTop: '5px', marginLeft: '5px' }} /> : null
-                                                            }
-                                                    </div>;
-                                                }
-                                            }
-                                        </FixedSizeList> : <div className={classes.rowEmpty}>No directories available</div>
-                                    }}
-                                </AutoSizer> : <div className={classes.row}><CircularProgress className={classes.loader} size={30} /></div>
-                        }
-
-                    </div>
+                <div className={path.length > 1 ? classes.searchWrapper : classes.searchWrapperHidden}>
+                    <SearchInput selfClearProp={leftKey} label="Search" value={leftSearch} onSearch={setLeftSearch} />
                 </div>
-                <div className={classes.rightPanel} data-cy="collection-files-right-panel">
-                    <div className={classes.searchWrapper}>
-                        <SearchInput selfClearProp={rightKey} label="Search" value={rightSearch} onSearch={setRightSearch} />
-                    </div>
-                    {
-                        isWritable &&
-                        <Button
-                            className={classes.uploadButton}
-                            data-cy='upload-button'
-                            onClick={() => {
-                                onUploadDataClick(rightKey === leftKey ? undefined : rightKey);
-                            }}
-                            variant='contained'
-                            color='primary'
-                            size='small'>
-                            <DownloadIcon className={classes.uploadIcon} />
-                            Upload data
-                        </Button>
-                    }
-                    <div className={classes.dataWrapper}>
-                        {
-                            rightData && !isLoading ?
-                                <AutoSizer defaultHeight={500}>
-                                    {({ height, width }) => {
-                                        const filtered = rightData.filter(({ name }) => name.indexOf(rightSearch) > -1);
-
-                        return !!filtered.length
-                        ? <FixedSizeList height={height} itemCount={filtered.length}
-                            itemSize={35} width={width}>{ ({ index, style }) => {
-                            console.log("Left Data ROW: ", filtered[index]);
-                            const { id, type, name } = filtered[index];
-
-                            return <div data-id={id} style={style} data-item="true"
-                                data-type={type} data-parent-path={name} key={id}
-                                className={classNames(classes.row, getActiveClass(name))}>
-                                    { getItemIcon(type, getActiveClass(name)) }
-                                    <div className={classes.rowName}>
-                                        {name}
-                                    </div>
-                                    { getActiveClass(name)
-                                    ? <SidePanelRightArrowIcon style={{
-                                        display: 'inline',
-                                        marginTop: '5px',
-                                        marginLeft: '5px' }} />
-                                    : null }
-                            </div>;
-                        } }</FixedSizeList>
-                        : <div className={classes.rowEmpty}>No directories available</div>
-                    } }</AutoSizer>
-                    : <div className={classes.row}>
-                        <CircularProgress className={classes.loader} size={30} />
-                    </div> }
+                <div className={classes.dataWrapper}>{ leftData
+                ? <AutoSizer defaultWidth={0}>{({ height, width }) => {
+                    const filtered = leftData.filter(({ name }) => name.indexOf(leftSearch) > -1);
+                    return !!filtered.length
+                    ? <FixedSizeList height={height} itemCount={filtered.length}
+                        itemSize={35} width={width}>{ ({ index, style }) => {
+                        const { id, type, name } = filtered[index];
+                        return <div data-id={id} style={style} data-item="true"
+                            data-type={type} data-parent-path={name}
+                            className={classNames(classes.row, getActiveClass(name))}
+                            key={id}>
+                                { getItemIcon(type, getActiveClass(name)) }
+                                <div className={classes.rowName}>
+                                    {name}
+                                </div>
+                                { getActiveClass(name)
+                                ? <SidePanelRightArrowIcon
+                                    style={{ display: 'inline', marginTop: '5px', marginLeft: '5px' }} />
+                                : null
+                                }
+                        </div>;
+                    }}</FixedSizeList>
+                    : <div className={classes.rowEmpty}>No directories available</div>
+                    }}
+                </AutoSizer>
+                : <div data-cy="collection-loader" className={classes.row}><CircularProgress className={classes.loader} size={30} /></div> }
                 </div>
             </div>
-            <div className={classes.rightPanel}>
+            <div className={classes.rightPanel} data-cy="collection-files-right-panel">
                 <div className={classes.searchWrapper}>
                     <SearchInput selfClearProp={rightKey} label="Search" value={rightSearch} onSearch={setRightSearch} />
                 </div>
                 { isWritable &&
                 <Button className={classes.uploadButton} data-cy='upload-button'
                     onClick={() => {
-                        onUploadDataClick();
+                        onUploadDataClick(rightKey === leftKey ? undefined : rightKey);
                     }}
                     variant='contained' color='primary' size='small'>
                     <DownloadIcon className={classes.uploadIcon} />
@@ -597,12 +553,9 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
                 <div className={classes.dataWrapper}>{ rightData && !isLoading
                     ? <AutoSizer defaultHeight={500}>{({ height, width }) => {
                         const filtered = rightData.filter(({ name }) => name.indexOf(rightSearch) > -1);
-                        console.log("Right Data: ", filtered);
-
                         return !!filtered.length
                         ? <FixedSizeList height={height} itemCount={filtered.length}
                             itemSize={35} width={width}>{ ({ index, style }) => {
-                                console.log("Right Data ROW: ", filtered[index]);
                                 const { id, type, name, size } = filtered[index];
 
                                 return <div style={style} data-id={id} data-item="true"
@@ -620,6 +573,15 @@ export const CollectionPanelFiles = withStyles(styles)(connect((state: RootState
                                         marginLeft: 'auto', marginRight: '1rem' }}>
                                         { formatFileSize(size) }
                                     </span>
+                                    <Tooltip title="More options" disableFocusListener>
+                                        <IconButton data-id='moreOptions'
+                                            data-cy='file-item-options-btn'
+                                            className={classes.moreOptionsButton}>
+                                            <MoreOptionsIcon
+                                                data-id='moreOptions'
+                                                className={classes.moreOptions} />
+                                        </IconButton>
+                                    </Tooltip>
                                 </div>
                             } }</FixedSizeList>
                         : <div className={classes.rowEmpty}>This collection is empty</div>
