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