1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
20 export type CssRules = 'versionBrowserHeader' | 'versionBrowserItem' | 'versionBrowserField';
22 const styles: StyleRulesCallback<CssRules> = theme => ({
23 versionBrowserHeader: {
30 versionBrowserField: {
35 export class CollectionDetails extends DetailsData<CollectionResource> {
37 getIcon(className?: string) {
38 return <CollectionIcon className={className} />;
42 return ['Details', 'Versions'];
48 return this.getCollectionInfo();
50 return this.getVersionBrowser();
56 private getCollectionInfo() {
57 return <CollectionDetailsAttributes twoCol={false} item={this.item} />;
60 private getVersionBrowser() {
61 return <CollectionVersionBrowser />;
65 interface CollectionVersionBrowserProps {
66 currentCollection: CollectionResource | undefined;
67 versions: CollectionResource[];
70 interface CollectionVersionBrowserDispatchProps {
71 showVersion: (c: CollectionResource) => void;
72 handleContextMenu: (event: React.MouseEvent<HTMLElement>, collection: CollectionResource) => void;
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[])
82 return { currentCollection, versions };
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,
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">
111 <Typography variant="caption" className={classes.versionBrowserHeader}>
116 <Typography variant="caption" className={classes.versionBrowserHeader}>
121 <Typography variant="caption" className={classes.versionBrowserHeader}>
125 { versions.map(item => {
126 const isSelectedVersion = !!(currentCollection && currentCollection.uuid === item.uuid);
128 <ListItem button style={{padding: '4px'}}
129 data-cy={`collection-version-browser-select-${item.version}`}
131 onClick={e => showVersion(item)}
132 onContextMenu={event => handleContextMenu(event, item)}
133 selected={isSelectedVersion}
134 className={classes.versionBrowserItem}>
136 <Typography variant="caption" className={classes.versionBrowserField}>
141 <Typography variant="caption" className={classes.versionBrowserField}>
142 {formatFileSize(item.fileSizeTotal)}
146 <Typography variant="caption" className={classes.versionBrowserField}>
147 {formatDate(item.modifiedAt)}
151 <Typography variant="caption" className={classes.versionBrowserField}>
152 Modified by: <UserNameFromID uuid={item.modifiedByUserUuid} />