1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { CollectionIcon, RenameIcon } 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, ResourcesState } from 'store/resources/resources';
12 import { connect } from 'react-redux';
13 import { Button, 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 import { openCollectionUpdateDialog } from 'store/collections/collection-update-actions';
20 import { resourceIsFrozen } from 'common/frozen-resources';
22 export type CssRules = 'versionBrowserHeader'
23 | 'versionBrowserItem'
24 | 'versionBrowserField'
29 const styles: StyleRulesCallback<CssRules> = theme => ({
30 versionBrowserHeader: {
37 versionBrowserField: {
41 paddingRight: theme.spacing.unit/2,
46 padding: '2px 10px 2px 5px',
50 marginRight: theme.spacing.unit / 2,
51 marginBottom: theme.spacing.unit / 2
55 export class CollectionDetails extends DetailsData<CollectionResource> {
57 getIcon(className?: string) {
58 return <CollectionIcon className={className} />;
62 return ['Details', 'Versions'];
68 return this.getCollectionInfo();
70 return this.getVersionBrowser();
76 private getCollectionInfo() {
77 return <CollectionInfo />;
80 private getVersionBrowser() {
81 return <CollectionVersionBrowser />;
85 interface CollectionInfoDataProps {
86 resources: ResourcesState;
87 currentCollection: CollectionResource | undefined;
90 interface CollectionInfoDispatchProps {
91 editCollection: (collection: CollectionResource | undefined) => void;
94 const ciMapStateToProps = (state: RootState): CollectionInfoDataProps => {
96 resources: state.resources,
97 currentCollection: getResource<CollectionResource>(state.detailsPanel.resourceUuid)(state.resources),
101 const ciMapDispatchToProps = (dispatch: Dispatch): CollectionInfoDispatchProps => ({
102 editCollection: (collection: CollectionResource) =>
103 dispatch<any>(openCollectionUpdateDialog({
104 uuid: collection.uuid,
105 name: collection.name,
106 description: collection.description,
107 properties: collection.properties,
108 storageClassesDesired: collection.storageClassesDesired,
112 type CollectionInfoProps = CollectionInfoDataProps & CollectionInfoDispatchProps & WithStyles<CssRules>;
114 const CollectionInfo = withStyles(styles)(
115 connect(ciMapStateToProps, ciMapDispatchToProps)(
116 ({ currentCollection, resources, editCollection, classes }: CollectionInfoProps) =>
117 currentCollection !== undefined
120 disabled={resourceIsFrozen(currentCollection, resources)}
121 className={classes.editButton} variant='contained'
122 data-cy='details-panel-edit-btn' color='primary' size='small'
123 onClick={() => editCollection(currentCollection)}>
124 <RenameIcon className={classes.editIcon} /> Edit
126 <CollectionDetailsAttributes classes={classes} twoCol={false} item={currentCollection} />
132 interface CollectionVersionBrowserProps {
133 currentCollection: CollectionResource | undefined;
134 versions: CollectionResource[];
137 interface CollectionVersionBrowserDispatchProps {
138 showVersion: (c: CollectionResource) => void;
139 handleContextMenu: (event: React.MouseEvent<HTMLElement>, collection: CollectionResource) => void;
142 const vbMapStateToProps = (state: RootState): CollectionVersionBrowserProps => {
143 const currentCollection = getResource<CollectionResource>(state.detailsPanel.resourceUuid)(state.resources);
144 const versions = (currentCollection
145 && filterResources(rsc =>
146 (rsc as CollectionResource).currentVersionUuid === currentCollection.currentVersionUuid)(state.resources)
147 .sort((a: CollectionResource, b: CollectionResource) => b.version - a.version) as CollectionResource[])
149 return { currentCollection, versions };
152 const vbMapDispatchToProps = () =>
153 (dispatch: Dispatch): CollectionVersionBrowserDispatchProps => ({
154 showVersion: (collection) => dispatch<any>(navigateTo(collection.uuid)),
155 handleContextMenu: (event: React.MouseEvent<HTMLElement>, collection: CollectionResource) => {
156 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(collection.uuid));
157 if (collection && menuKind) {
158 dispatch<any>(openContextMenu(event, {
159 name: collection.name,
160 uuid: collection.uuid,
161 description: collection.description,
162 storageClassesDesired: collection.storageClassesDesired,
163 ownerUuid: collection.ownerUuid,
164 isTrashed: collection.isTrashed,
165 kind: collection.kind,
172 const CollectionVersionBrowser = withStyles(styles)(
173 connect(vbMapStateToProps, vbMapDispatchToProps)(
174 ({ currentCollection, versions, showVersion, handleContextMenu, classes }: CollectionVersionBrowserProps & CollectionVersionBrowserDispatchProps & WithStyles<CssRules>) => {
175 return <div data-cy="collection-version-browser">
178 <Typography variant="caption" className={classes.versionBrowserHeader}>
183 <Typography variant="caption" className={classes.versionBrowserHeader}>
188 <Typography variant="caption" className={classes.versionBrowserHeader}>
192 { versions.map(item => {
193 const isSelectedVersion = !!(currentCollection && currentCollection.uuid === item.uuid);
195 <ListItem button style={{padding: '4px'}}
196 data-cy={`collection-version-browser-select-${item.version}`}
198 onClick={e => showVersion(item)}
199 onContextMenu={event => handleContextMenu(event, item)}
200 selected={isSelectedVersion}
201 className={classes.versionBrowserItem}>
203 <Typography variant="caption" className={classes.versionBrowserField}>
208 <Typography variant="caption" className={classes.versionBrowserField}>
209 {formatFileSize(item.fileSizeTotal)}
213 <Typography variant="caption" className={classes.versionBrowserField}>
214 {formatDate(item.modifiedAt)}
218 <Typography variant="caption" className={classes.versionBrowserField}>
219 Modified by: <UserNameFromID uuid={item.modifiedByUserUuid} />