X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/59329caeae17903b97b90b167df5a8122a0c9d95..892d0e98eeb20624cf49ca408af6156b4d3b0f55:/src/views/collection-panel/collection-panel.tsx?ds=sidebyside

diff --git a/src/views/collection-panel/collection-panel.tsx b/src/views/collection-panel/collection-panel.tsx
index 41a685f3..bcf72edc 100644
--- a/src/views/collection-panel/collection-panel.tsx
+++ b/src/views/collection-panel/collection-panel.tsx
@@ -2,35 +2,62 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from 'react';
+import React from 'react';
 import {
-    StyleRulesCallback, WithStyles, withStyles, Card,
-    CardHeader, IconButton, CardContent, Grid, Chip, Tooltip
+    StyleRulesCallback, WithStyles, withStyles,
+    IconButton, Grid, Tooltip, Typography, ExpansionPanel,
+    ExpansionPanelSummary, ExpansionPanelDetails
 } from '@material-ui/core';
 import { connect, DispatchProp } from "react-redux";
 import { RouteComponentProps } from 'react-router';
-import { ArvadosTheme } from '~/common/custom-theme';
-import { RootState } from '~/store/store';
-import { MoreOptionsIcon, CollectionIcon, CopyIcon } from '~/components/icon/icon';
-import { DetailsAttribute } from '~/components/details-attribute/details-attribute';
-import { CollectionResource } from '~/models/collection';
-import { CollectionPanelFiles } from '~/views-components/collection-panel-files/collection-panel-files';
-import * as CopyToClipboard from 'react-copy-to-clipboard';
+import { ArvadosTheme } from 'common/custom-theme';
+import { RootState } from 'store/store';
+import { MoreOptionsIcon, CollectionIcon, ReadOnlyIcon, ExpandIcon, CollectionOldVersionIcon } from 'components/icon/icon';
+import { DetailsAttribute } from 'components/details-attribute/details-attribute';
+import { CollectionResource, getCollectionUrl } from 'models/collection';
+import { CollectionPanelFiles } from 'views-components/collection-panel-files/collection-panel-files';
 import { CollectionTagForm } from './collection-tag-form';
-import { deleteCollectionTag, navigateToProcess } from '~/store/collection-panel/collection-panel-action';
-import { snackbarActions } from '~/store/snackbar/snackbar-actions';
-import { getResource } from '~/store/resources/resources';
-import { openContextMenu } from '~/store/context-menu/context-menu-actions';
-import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
-import { formatFileSize } from "~/common/formatters";
-import { getResourceData } from "~/store/resources-data/resources-data";
-import { ResourceData } from "~/store/resources-data/resources-data-reducer";
+import { deleteCollectionTag, navigateToProcess, collectionPanelActions } from 'store/collection-panel/collection-panel-action';
+import { getResource } from 'store/resources/resources';
+import { openContextMenu, resourceUuidToContextMenuKind } from 'store/context-menu/context-menu-actions';
+import { formatDate, formatFileSize } from "common/formatters";
+import { openDetailsPanel } from 'store/details-panel/details-panel-action';
+import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
+import { getPropertyChip } from 'views-components/resource-properties-form/property-chip';
+import { IllegalNamingWarning } from 'components/warning/warning';
+import { GroupResource } from 'models/group';
+import { UserResource } from 'models/user';
+import { getUserUuid } from 'common/getuser';
+import { getProgressIndicator } from 'store/progress-indicator/progress-indicator-reducer';
+import { COLLECTION_PANEL_LOAD_FILES, loadCollectionFiles, COLLECTION_PANEL_LOAD_FILES_THRESHOLD } from 'store/collection-panel/collection-panel-files/collection-panel-files-actions';
+import { Link } from 'react-router-dom';
+import { Link as ButtonLink } from '@material-ui/core';
+import { ResourceOwnerWithName, ResponsiblePerson } from 'views-components/data-explorer/renderers';
+import { MPVContainer, MPVPanelContent, MPVPanelState } from 'components/multi-panel-view/multi-panel-view';
 
-type CssRules = 'card' | 'iconHeader' | 'tag' | 'copyIcon' | 'label' | 'value' | 'link';
+type CssRules = 'root'
+    | 'button'
+    | 'filesCard'
+    | 'iconHeader'
+    | 'tag'
+    | 'label'
+    | 'value'
+    | 'link'
+    | 'centeredLabel'
+    | 'warningLabel'
+    | 'collectionName'
+    | 'readOnlyIcon';
 
 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
-    card: {
-        marginBottom: theme.spacing.unit * 2
+    root: {
+        width: '100%',
+    },
+    button: {
+        cursor: 'pointer'
+    },
+    filesCard: {
+        marginBottom: theme.spacing.unit * 2,
+        flex: 1,
     },
     iconHeader: {
         fontSize: '1.875rem',
@@ -40,15 +67,19 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
         marginRight: theme.spacing.unit,
         marginBottom: theme.spacing.unit
     },
-    copyIcon: {
-        marginLeft: theme.spacing.unit,
-        fontSize: '1.125rem',
-        color: theme.palette.grey["500"],
-        cursor: 'pointer'
-    },
     label: {
         fontSize: '0.875rem'
     },
+    centeredLabel: {
+        fontSize: '0.875rem',
+        textAlign: 'center'
+    },
+    warningLabel: {
+        fontStyle: 'italic'
+    },
+    collectionName: {
+        flexDirection: 'column',
+    },
     value: {
         textTransform: 'none',
         fontSize: '0.875rem'
@@ -59,12 +90,19 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
         '&:hover': {
             cursor: 'pointer'
         }
+    },
+    readOnlyIcon: {
+        marginLeft: theme.spacing.unit,
+        fontSize: 'small',
     }
 });
 
 interface CollectionPanelDataProps {
     item: CollectionResource;
-    data: ResourceData;
+    isWritable: boolean;
+    isOldVersion: boolean;
+    isLoadingFiles: boolean;
+    tooManyFiles: boolean;
 }
 
 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
@@ -72,106 +110,245 @@ type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
 
 export const CollectionPanel = withStyles(styles)(
     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
-        const item = getResource(props.match.params.id)(state.resources);
-        const data = getResourceData(props.match.params.id)(state.resourcesData);
-        return { item, data };
+        const currentUserUUID = getUserUuid(state);
+        const item = getResource<CollectionResource>(props.match.params.id)(state.resources);
+        let isWritable = false;
+        const isOldVersion = item && item.currentVersionUuid !== item.uuid;
+        if (item && !isOldVersion) {
+            if (item.ownerUuid === currentUserUUID) {
+                isWritable = true;
+            } else {
+                const itemOwner = getResource<GroupResource | UserResource>(item.ownerUuid)(state.resources);
+                if (itemOwner && itemOwner.writableBy) {
+                    isWritable = itemOwner.writableBy.indexOf(currentUserUUID || '') >= 0;
+                }
+            }
+        }
+        const loadingFilesIndicator = getProgressIndicator(COLLECTION_PANEL_LOAD_FILES)(state.progressIndicator);
+        const isLoadingFiles = (loadingFilesIndicator && loadingFilesIndicator!.working) || false;
+        const tooManyFiles = (!state.collectionPanel.loadBigCollections && item && item.fileCount > COLLECTION_PANEL_LOAD_FILES_THRESHOLD) || false;
+        return { item, isWritable, isOldVersion, isLoadingFiles, tooManyFiles };
     })(
         class extends React.Component<CollectionPanelProps> {
             render() {
-                const { classes, item, data, dispatch } = this.props;
+                const { classes, item, dispatch, isWritable, isOldVersion, isLoadingFiles, tooManyFiles } = this.props;
+                const panelsData: MPVPanelState[] = [
+                    {name: "Details"},
+                    {name: "Properties", visible: false},
+                    {name: "Files"},
+                ];
                 return item
-                    ? <>
-                        <Card className={classes.card}>
-                            <CardHeader
-                                avatar={<CollectionIcon className={classes.iconHeader} />}
-                                action={
-                                    <Tooltip title="More options" disableFocusListener>
-                                        <IconButton
-                                            aria-label="More options"
-                                            onClick={this.handleContextMenu}>
-                                            <MoreOptionsIcon />
+                    ? <MPVContainer className={classes.root} spacing={8} direction="column" justify-content="flex-start" wrap="nowrap" panelStates={panelsData}>
+                        <MPVPanelContent xs="auto"><ExpansionPanel data-cy='collection-info-panel' defaultExpanded>
+                            <ExpansionPanelSummary expandIcon={<ExpandIcon />}>
+                                <Grid container justify="space-between">
+                                    <Grid item xs={11}><span>
+                                        <IconButton onClick={this.openCollectionDetails}>
+                                            {isOldVersion
+                                                ? <CollectionOldVersionIcon className={classes.iconHeader} />
+                                                : <CollectionIcon className={classes.iconHeader} />}
                                         </IconButton>
-                                    </Tooltip>
-                                }
-                                title={item && item.name}
-                                subheader={item && item.description} />
-                            <CardContent>
-                                <Grid container direction="column">
-                                    <Grid item xs={6}>
-                                        <DetailsAttribute classLabel={classes.label} classValue={classes.value}
-                                            label='Collection UUID'
-                                            value={item && item.uuid}>
-                                            <Tooltip title="Copy uuid">
-                                                <CopyToClipboard text={item && item.uuid} onCopy={() => this.onCopy()}>
-                                                    <CopyIcon className={classes.copyIcon} />
-                                                </CopyToClipboard>
-                                            </Tooltip>
-                                        </DetailsAttribute>
-                                        <DetailsAttribute classLabel={classes.label} classValue={classes.value}
-                                            label='Number of files' value={data && data.fileCount} />
-                                        <DetailsAttribute classLabel={classes.label} classValue={classes.value}
-                                            label='Content size' value={data && formatFileSize(data.fileSize)} />
-                                        <DetailsAttribute classLabel={classes.label} classValue={classes.value}
-                                            label='Owner' value={item && item.ownerUuid} />
-                                        <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request  || item.properties.containerRequest))}>
-                                            <DetailsAttribute classLabel={classes.link} label='Link to process' />
+                                        <IllegalNamingWarning name={item.name} />
+                                        <span>
+                                            {item.name}
+                                            {isWritable ||
+                                                <Tooltip title="Read-only">
+                                                    <ReadOnlyIcon data-cy="read-only-icon" className={classes.readOnlyIcon} />
+                                                </Tooltip>
+                                            }
                                         </span>
+                                    </span></Grid>
+                                    <Grid item xs={1} style={{ textAlign: "right" }}>
+                                        <Tooltip title="Actions" disableFocusListener>
+                                            <IconButton
+                                                data-cy='collection-panel-options-btn'
+                                                aria-label="Actions"
+                                                onClick={this.handleContextMenu}>
+                                                <MoreOptionsIcon />
+                                            </IconButton>
+                                        </Tooltip>
                                     </Grid>
                                 </Grid>
-                            </CardContent>
-                        </Card>
-
-                        <Card className={classes.card}>
-                            <CardHeader title="Properties" />
-                            <CardContent>
-                                <Grid container direction="column">
+                            </ExpansionPanelSummary>
+                            <ExpansionPanelDetails>
+                                <Grid container justify="space-between">
                                     <Grid item xs={12}>
-                                        <CollectionTagForm />
+                                        <Typography variant="caption">
+                                            {item.description}
+                                        </Typography>
+                                        <CollectionDetailsAttributes item={item} classes={classes} twoCol={true} showVersionBrowser={() => dispatch<any>(openDetailsPanel(item.uuid, 1))} />
+                                        {(item.properties.container_request || item.properties.containerRequest) &&
+                                            <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
+                                                <DetailsAttribute classLabel={classes.link} label='Link to process' />
+                                            </span>
+                                        }
+                                        {isOldVersion &&
+                                            <Typography className={classes.warningLabel} variant="caption">
+                                                This is an old version. Make a copy to make changes. Go to the <Link to={getCollectionUrl(item.currentVersionUuid)}>head version</Link> for sharing options.
+                                          </Typography>
+                                        }
                                     </Grid>
+                                </Grid>
+                            </ExpansionPanelDetails>
+                        </ExpansionPanel></MPVPanelContent>
+
+                        <MPVPanelContent xs="auto"><ExpansionPanel data-cy='collection-properties-panel' defaultExpanded>
+                            <ExpansionPanelSummary expandIcon={<ExpandIcon />}>
+                                {"Properties"}
+                            </ExpansionPanelSummary>
+                            <ExpansionPanelDetails>
+                                <Grid container>
+                                    {isWritable && <Grid item xs={12}>
+                                        <CollectionTagForm />
+                                    </Grid>}
                                     <Grid item xs={12}>
-                                        {
-                                            Object.keys(item.properties).map(k => {
-                                                return <Chip key={k} className={classes.tag}
-                                                    onDelete={this.handleDelete(k)}
-                                                    label={`${k}: ${item.properties[k]}`} />;
-                                            })
+                                        {Object.keys(item.properties).length > 0
+                                            ? Object.keys(item.properties).map(k =>
+                                                Array.isArray(item.properties[k])
+                                                    ? item.properties[k].map((v: string) =>
+                                                        getPropertyChip(
+                                                            k, v,
+                                                            isWritable
+                                                                ? this.handleDelete(k, v)
+                                                                : undefined,
+                                                            classes.tag))
+                                                    : getPropertyChip(
+                                                        k, item.properties[k],
+                                                        isWritable
+                                                            ? this.handleDelete(k, item.properties[k])
+                                                            : undefined,
+                                                        classes.tag)
+                                            )
+                                            : <div className={classes.centeredLabel}>No properties set on this collection.</div>
                                         }
                                     </Grid>
                                 </Grid>
-                            </CardContent>
-                        </Card>
-                        <div className={classes.card}>
-                            <CollectionPanelFiles />
-                        </div>
-                    </>
+                            </ExpansionPanelDetails>
+                        </ExpansionPanel></MPVPanelContent>
+                        <MPVPanelContent xs className={classes.filesCard}>
+                            <CollectionPanelFiles
+                                isWritable={isWritable}
+                                isLoading={isLoadingFiles}
+                                tooManyFiles={tooManyFiles}
+                                loadFilesFunc={() => {
+                                    dispatch(collectionPanelActions.LOAD_BIG_COLLECTIONS(true));
+                                    dispatch<any>(loadCollectionFiles(this.props.item.uuid));
+                                }
+                                } />
+                        </MPVPanelContent>
+                    </MPVContainer>
                     : null;
             }
 
             handleContextMenu = (event: React.MouseEvent<any>) => {
-                const { uuid, ownerUuid, name, description, kind, isTrashed } = this.props.item;
+                const { uuid, ownerUuid, name, description, kind, storageClassesDesired } = this.props.item;
+                const menuKind = this.props.dispatch<any>(resourceUuidToContextMenuKind(uuid));
                 const resource = {
                     uuid,
                     ownerUuid,
                     name,
                     description,
+                    storageClassesDesired,
                     kind,
-                    menuKind: isTrashed
-                        ? ContextMenuKind.TRASHED_COLLECTION
-                        : ContextMenuKind.COLLECTION
+                    menuKind,
                 };
+                // Avoid expanding/collapsing the panel
+                event.stopPropagation();
                 this.props.dispatch<any>(openContextMenu(event, resource));
             }
 
-            handleDelete = (key: string) => () => {
-                this.props.dispatch<any>(deleteCollectionTag(key));
+            onCopy = (message: string) =>
+                this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
+                    message,
+                    hideDuration: 2000,
+                    kind: SnackbarKind.SUCCESS
+                }))
+
+            handleDelete = (key: string, value: string) => () => {
+                this.props.dispatch<any>(deleteCollectionTag(key, value));
             }
 
-            onCopy = () => {
-                this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
-                    message: "Uuid has been copied",
-                    hideDuration: 2000
-                }));
+            openCollectionDetails = (e: React.MouseEvent<HTMLElement>) => {
+                const { item } = this.props;
+                if (item) {
+                    e.stopPropagation();
+                    this.props.dispatch<any>(openDetailsPanel(item.uuid));
+                }
             }
+
+            titleProps = {
+                onClick: this.openCollectionDetails
+            };
+
         }
     )
 );
+
+export const CollectionDetailsAttributes = (props: { item: CollectionResource, twoCol: boolean, classes?: Record<CssRules, string>, showVersionBrowser?: () => void }) => {
+    const item = props.item;
+    const classes = props.classes || { label: '', value: '', button: '' };
+    const isOldVersion = item && item.currentVersionUuid !== item.uuid;
+    const mdSize = props.twoCol ? 6 : 12;
+    const showVersionBrowser = props.showVersionBrowser;
+    const responsiblePersonRef = React.useRef(null);
+    return <Grid container>
+        <Grid item xs={12} md={mdSize}>
+            <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                label={isOldVersion ? "This version's UUID" : "Collection UUID"}
+                linkToUuid={item.uuid} />
+        </Grid>
+        <Grid item xs={12} md={mdSize}>
+            <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                label={isOldVersion ? "This version's PDH" : "Portable data hash"}
+                linkToUuid={item.portableDataHash} />
+        </Grid>
+        <Grid item xs={12} md={mdSize}>
+            <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                label='Owner' linkToUuid={item.ownerUuid}
+                uuidEnhancer={(uuid: string) => <ResourceOwnerWithName uuid={uuid} />} />
+        </Grid>
+        <div data-cy="responsible-person-wrapper" ref={responsiblePersonRef}>
+            <Grid item xs={12} md={12}>
+                <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                    label='Responsible person' linkToUuid={item.ownerUuid}
+                    uuidEnhancer={(uuid: string) => <ResponsiblePerson uuid={item.uuid} parentRef={responsiblePersonRef.current} />} />
+            </Grid>
+        </div>
+        <Grid item xs={12} md={mdSize}>
+            <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                label='Head version'
+                value={isOldVersion ? undefined : 'this one'}
+                linkToUuid={isOldVersion ? item.currentVersionUuid : undefined} />
+        </Grid>
+        <Grid item xs={12} md={mdSize}>
+            <DetailsAttribute
+                classLabel={classes.label} classValue={classes.value}
+                label='Version number'
+                value={showVersionBrowser !== undefined
+                    ? <Tooltip title="Open version browser"><ButtonLink underline='none' className={classes.button} onClick={() => showVersionBrowser()}>
+                        {<span data-cy='collection-version-number'>{item.version}</span>}
+                    </ButtonLink></Tooltip>
+                    : item.version
+                }
+            />
+        </Grid>
+        <Grid item xs={12} md={mdSize}>
+            <DetailsAttribute label='Created at' value={formatDate(item.createdAt)} />
+        </Grid>
+        <Grid item xs={12} md={mdSize}>
+            <DetailsAttribute label='Last modified' value={formatDate(item.modifiedAt)} />
+        </Grid>
+        <Grid item xs={12} md={mdSize}>
+            <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                label='Number of files' value={item.fileCount} />
+        </Grid>
+        <Grid item xs={12} md={mdSize}>
+            <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                label='Content size' value={formatFileSize(item.fileSizeTotal)} />
+        </Grid>
+        <Grid item xs={12} md={mdSize}>
+            <DetailsAttribute classLabel={classes.label} classValue={classes.value}
+                label='Storage classes' value={item.storageClassesDesired.join(', ')} />
+        </Grid>
+    </Grid>;
+};
