Merge remote-tracking branch 'origin/main' into 18207-Workbench2-is-not-clearing...
[arvados-workbench2.git] / src / views-components / details-panel / collection-details.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 { CollectionIcon } from 'components/icon/icon';
7 import { CollectionResource } from 'models/collection';
8 import { DetailsData } from "./details-data";
9 import { CollectionDetailsAttributes } from 'views/collection-panel/collection-panel';
10 import { RootState } from 'store/store';
11 import { filterResources, getResource } from 'store/resources/resources';
12 import { connect } from 'react-redux';
13 import { Grid, ListItem, StyleRulesCallback, Typography, withStyles, WithStyles } from '@material-ui/core';
14 import { formatDate, formatFileSize } from 'common/formatters';
15 import { UserNameFromID } from '../data-explorer/renderers';
16 import { Dispatch } from 'redux';
17 import { navigateTo } from 'store/navigation/navigation-action';
18 import { openContextMenu, resourceUuidToContextMenuKind } from 'store/context-menu/context-menu-actions';
19
20 export type CssRules = 'versionBrowserHeader' | 'versionBrowserItem' | 'versionBrowserField';
21
22 const styles: StyleRulesCallback<CssRules> = theme => ({
23     versionBrowserHeader: {
24         textAlign: 'center',
25         fontWeight: 'bold',
26     },
27     versionBrowserItem: {
28         flexWrap: 'wrap',
29     },
30     versionBrowserField: {
31         textAlign: 'center',
32     }
33 });
34
35 export class CollectionDetails extends DetailsData<CollectionResource> {
36
37     getIcon(className?: string) {
38         return <CollectionIcon className={className} />;
39     }
40
41     getTabLabels() {
42         return ['Details', 'Versions'];
43     }
44
45     getDetails({tabNr}) {
46         switch (tabNr) {
47             case 0:
48                 return this.getCollectionInfo();
49             case 1:
50                 return this.getVersionBrowser();
51             default:
52                 return <div />;
53         }
54     }
55
56     private getCollectionInfo() {
57         return <CollectionDetailsAttributes twoCol={false} item={this.item} />;
58     }
59
60     private getVersionBrowser() {
61         return <CollectionVersionBrowser />;
62     }
63 }
64
65 interface CollectionVersionBrowserProps {
66     currentCollection: CollectionResource | undefined;
67     versions: CollectionResource[];
68 }
69
70 interface CollectionVersionBrowserDispatchProps {
71     showVersion: (c: CollectionResource) => void;
72     handleContextMenu: (event: React.MouseEvent<HTMLElement>, collection: CollectionResource) => void;
73 }
74
75 const mapStateToProps = (state: RootState): CollectionVersionBrowserProps => {
76     const currentCollection = getResource<CollectionResource>(state.detailsPanel.resourceUuid)(state.resources);
77     const versions = (currentCollection
78         && filterResources(rsc =>
79             (rsc as CollectionResource).currentVersionUuid === currentCollection.currentVersionUuid)(state.resources)
80                 .sort((a: CollectionResource, b: CollectionResource) => b.version - a.version) as CollectionResource[])
81         || [];
82     return { currentCollection, versions };
83 };
84
85 const mapDispatchToProps = () =>
86     (dispatch: Dispatch): CollectionVersionBrowserDispatchProps => ({
87         showVersion: (collection) => dispatch<any>(navigateTo(collection.uuid)),
88         handleContextMenu: (event: React.MouseEvent<HTMLElement>, collection: CollectionResource) => {
89             const menuKind = dispatch<any>(resourceUuidToContextMenuKind(collection.uuid));
90             if (collection && menuKind) {
91                 dispatch<any>(openContextMenu(event, {
92                     name: collection.name,
93                     uuid: collection.uuid,
94                     description: collection.description,
95                     storageClassesDesired: collection.storageClassesDesired,
96                     ownerUuid: collection.ownerUuid,
97                     isTrashed: collection.isTrashed,
98                     kind: collection.kind,
99                     menuKind
100                 }));
101             }
102         },
103     });
104
105 const CollectionVersionBrowser = withStyles(styles)(
106     connect(mapStateToProps, mapDispatchToProps)(
107         ({ currentCollection, versions, showVersion, handleContextMenu, classes }: CollectionVersionBrowserProps & CollectionVersionBrowserDispatchProps & WithStyles<CssRules>) => {
108             return <div data-cy="collection-version-browser">
109                 <Grid container>
110                     <Grid item xs={2}>
111                         <Typography variant="caption" className={classes.versionBrowserHeader}>
112                             Nr
113                         </Typography>
114                     </Grid>
115                     <Grid item xs={4}>
116                         <Typography variant="caption" className={classes.versionBrowserHeader}>
117                             Size
118                         </Typography>
119                     </Grid>
120                     <Grid item xs={6}>
121                         <Typography variant="caption" className={classes.versionBrowserHeader}>
122                             Date
123                         </Typography>
124                     </Grid>
125                 { versions.map(item => {
126                     const isSelectedVersion = !!(currentCollection && currentCollection.uuid === item.uuid);
127                     return (
128                         <ListItem button style={{padding: '4px'}}
129                             data-cy={`collection-version-browser-select-${item.version}`}
130                             key={item.version}
131                             onClick={e => showVersion(item)}
132                             onContextMenu={event => handleContextMenu(event, item)}
133                             selected={isSelectedVersion}
134                             className={classes.versionBrowserItem}>
135                             <Grid item xs={2}>
136                                 <Typography variant="caption" className={classes.versionBrowserField}>
137                                     {item.version}
138                                 </Typography>
139                             </Grid>
140                             <Grid item xs={4}>
141                                 <Typography variant="caption" className={classes.versionBrowserField}>
142                                     {formatFileSize(item.fileSizeTotal)}
143                                 </Typography>
144                             </Grid>
145                             <Grid item xs={6}>
146                                 <Typography variant="caption" className={classes.versionBrowserField}>
147                                     {formatDate(item.modifiedAt)}
148                                 </Typography>
149                             </Grid>
150                             <Grid item xs={12}>
151                                 <Typography variant="caption" className={classes.versionBrowserField}>
152                                     Modified by: <UserNameFromID uuid={item.modifiedByUserUuid} />
153                                 </Typography>
154                             </Grid>
155                         </ListItem>
156                     );
157                 })}
158                 </Grid>
159             </div>;
160         }));