17782: Fixes almost all tests (4 left) mostly by fixing namespace-type imports.
[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                     ownerUuid: collection.ownerUuid,
91                     isTrashed: collection.isTrashed,
92                     kind: collection.kind,
93                     menuKind
94                 }));
95             }
96         },
97     });
98
99 const CollectionVersionBrowser = withStyles(styles)(
100     connect(mapStateToProps, mapDispatchToProps)(
101         ({ currentCollection, versions, showVersion, handleContextMenu, classes }: CollectionVersionBrowserProps & CollectionVersionBrowserDispatchProps & WithStyles<CssRules>) => {
102             return <div data-cy="collection-version-browser">
103                 <Grid container>
104                     <Grid item xs={2}>
105                         <Typography variant="caption" className={classes.versionBrowserHeader}>
106                             Nr
107                         </Typography>
108                     </Grid>
109                     <Grid item xs={4}>
110                         <Typography variant="caption" className={classes.versionBrowserHeader}>
111                             Size
112                         </Typography>
113                     </Grid>
114                     <Grid item xs={6}>
115                         <Typography variant="caption" className={classes.versionBrowserHeader}>
116                             Date
117                         </Typography>
118                     </Grid>
119                 { versions.map(item => {
120                     const isSelectedVersion = !!(currentCollection && currentCollection.uuid === item.uuid);
121                     return (
122                         <ListItem button style={{padding: '4px'}}
123                             data-cy={`collection-version-browser-select-${item.version}`}
124                             key={item.version}
125                             onClick={e => showVersion(item)}
126                             onContextMenu={event => handleContextMenu(event, item)}
127                             selected={isSelectedVersion}>
128                             <Grid item xs={2}>
129                                 <Typography variant="caption" className={classes.versionBrowserItem}>
130                                     {item.version}
131                                 </Typography>
132                             </Grid>
133                             <Grid item xs={4}>
134                                 <Typography variant="caption" className={classes.versionBrowserItem}>
135                                     {formatFileSize(item.fileSizeTotal)}
136                                 </Typography>
137                             </Grid>
138                             <Grid item xs={6}>
139                                 <Typography variant="caption" className={classes.versionBrowserItem}>
140                                     {formatDate(item.modifiedAt)}
141                                 </Typography>
142                             </Grid>
143                         </ListItem>
144                     );
145                 })}
146                 </Grid>
147             </div>;
148         }));