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