1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as 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';
19 export type CssRules = 'versionBrowserHeader' | 'versionBrowserItem';
21 const styles: StyleRulesCallback<CssRules> = theme => ({
22 versionBrowserHeader: {
31 export class CollectionDetails extends DetailsData<CollectionResource> {
33 getIcon(className?: string) {
34 return <CollectionIcon className={className} />;
38 return ['Details', 'Versions'];
41 getDetails(tabNumber: number) {
44 return this.getCollectionInfo();
46 return this.getVersionBrowser();
52 private getCollectionInfo() {
53 return <CollectionDetailsAttributes twoCol={false} item={this.item} />;
56 private getVersionBrowser() {
57 return <CollectionVersionBrowser />;
61 interface CollectionVersionBrowserProps {
62 currentCollection: CollectionResource | undefined;
63 versions: CollectionResource[];
66 interface CollectionVersionBrowserDispatchProps {
67 showVersion: (c: CollectionResource) => void;
68 handleContextMenu: (event: React.MouseEvent<HTMLElement>, collection: CollectionResource) => void;
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[]
78 return { currentCollection, versions };
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,
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">
105 <Typography variant="caption" className={classes.versionBrowserHeader}>
110 <Typography variant="caption" className={classes.versionBrowserHeader}>
115 <Typography variant="caption" className={classes.versionBrowserHeader}>
119 { versions.map(item => {
120 const isSelectedVersion = !!(currentCollection && currentCollection.uuid === item.uuid);
122 <ListItem button style={{padding: '4px'}}
123 data-cy={`collection-version-browser-select-${item.version}`}
125 onClick={e => showVersion(item)}
126 onContextMenu={event => handleContextMenu(event, item)}
127 selected={isSelectedVersion}>
129 <Typography variant="caption" className={classes.versionBrowserItem}>
134 <Typography variant="caption" className={classes.versionBrowserItem}>
135 {formatFileSize(item.fileSizeTotal)}
139 <Typography variant="caption" className={classes.versionBrowserItem}>
140 {formatDate(item.modifiedAt)}