15067: Shows tags labels on project details. Adds copy-on-click feature.
[arvados-workbench2.git] / src / views-components / details-panel / project-details.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { connect } from 'react-redux';
7 import { RootState } from '~/store/store';
8 import { openProjectPropertiesDialog } from '~/store/details-panel/details-panel-action';
9 import { ProjectIcon, RenameIcon } from '~/components/icon/icon';
10 import { ProjectResource } from '~/models/project';
11 import { formatDate } from '~/common/formatters';
12 import { ResourceKind } from '~/models/resource';
13 import { resourceLabel } from '~/common/labels';
14 import { DetailsData } from "./details-data";
15 import { DetailsAttribute } from "~/components/details-attribute/details-attribute";
16 import { RichTextEditorLink } from '~/components/rich-text-editor-link/rich-text-editor-link';
17 import { withStyles, StyleRulesCallback, Chip, WithStyles } from '@material-ui/core';
18 import { ArvadosTheme } from '~/common/custom-theme';
19 import * as CopyToClipboard from 'react-copy-to-clipboard';
20 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
21 import { getTagValueLabel, getTagKeyLabel, Vocabulary } from '~/models/vocabulary';
22 import { getVocabulary } from "~/store/vocabulary/vocabulary-selectors";
23 import { Dispatch } from 'redux';
24
25 export class ProjectDetails extends DetailsData<ProjectResource> {
26     getIcon(className?: string) {
27         return <ProjectIcon className={className} />;
28     }
29
30     getDetails() {
31         return <ProjectDetailsComponent project={this.item} />;
32     }
33 }
34
35 type CssRules = 'tag' | 'editIcon';
36
37 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
38     tag: {
39         marginRight: theme.spacing.unit,
40         marginBottom: theme.spacing.unit
41     },
42     editIcon: {
43         fontSize: '1.125rem',
44         cursor: 'pointer'
45     }
46 });
47
48
49 interface ProjectDetailsComponentDataProps {
50     project: ProjectResource;
51     vocabulary: Vocabulary;
52 }
53
54 interface ProjectDetailsComponentActionProps {
55     onClick: () => void;
56     onCopy: (message: string) => void;
57 }
58
59 const mapStateToProps = ({ properties }: RootState) => ({
60     vocabulary: getVocabulary(properties),
61 });
62
63 const mapDispatchToProps = (dispatch: Dispatch) => ({
64     onClick: () => dispatch<any>(openProjectPropertiesDialog()),
65     onCopy: (message: string) => dispatch(snackbarActions.OPEN_SNACKBAR({
66         message,
67         hideDuration: 2000,
68         kind: SnackbarKind.SUCCESS
69     }))
70 });
71
72 type ProjectDetailsComponentProps = ProjectDetailsComponentDataProps & ProjectDetailsComponentActionProps & WithStyles<CssRules>;
73
74 const ProjectDetailsComponent = connect(mapStateToProps, mapDispatchToProps)(
75     withStyles(styles)(
76         ({ classes, project, onClick, vocabulary, onCopy }: ProjectDetailsComponentProps) => <div>
77             <DetailsAttribute label='Type' value={resourceLabel(ResourceKind.PROJECT)} />
78             {/* Missing attr */}
79             <DetailsAttribute label='Size' value='---' />
80             <DetailsAttribute label='Owner' linkToUuid={project.ownerUuid} lowercaseValue={true} />
81             <DetailsAttribute label='Last modified' value={formatDate(project.modifiedAt)} />
82             <DetailsAttribute label='Created at' value={formatDate(project.createdAt)} />
83             <DetailsAttribute label='Project UUID' linkToUuid={project.uuid} value={project.uuid} />
84             {/* Missing attr */}
85             {/*<DetailsAttribute label='File size' value='1.4 GB' />*/}
86             <DetailsAttribute label='Description'>
87                 {project.description ?
88                     <RichTextEditorLink
89                         title={`Description of ${project.name}`}
90                         content={project.description}
91                         label='Show full description' />
92                     : '---'
93                 }
94             </DetailsAttribute>
95             <DetailsAttribute label='Properties'>
96                 <div onClick={onClick}>
97                     <RenameIcon className={classes.editIcon} />
98                 </div>
99             </DetailsAttribute>
100             {
101                 Object.keys(project.properties).map(k => {
102                     const label = `${getTagKeyLabel(k, vocabulary)}: ${getTagValueLabel(k, project.properties[k], vocabulary)}`;
103                     return (
104                         <CopyToClipboard key={k} text={label} onCopy={() => onCopy("Copied")}>
105                             <Chip key={k} className={classes.tag} label={label} />
106                         </CopyToClipboard>
107                     );
108                 })
109             }
110         </div>
111     ));