21026: <msg here> Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa.knox@curii.com>
[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 React from 'react';
6 import { connect } from 'react-redux';
7 import { ProjectIcon, RenameIcon, FilterGroupIcon } from 'components/icon/icon';
8 import { ProjectResource } from 'models/project';
9 import { formatDate } from 'common/formatters';
10 import { ResourceKind } from 'models/resource';
11 import { resourceLabel } from 'common/labels';
12 import { DetailsData } from "./details-data";
13 import { DetailsAttribute } from "components/details-attribute/details-attribute";
14 import { RichTextEditorLink } from 'components/rich-text-editor-link/rich-text-editor-link';
15 import { withStyles, StyleRulesCallback, WithStyles, Button } from '@material-ui/core';
16 import { ArvadosTheme } from 'common/custom-theme';
17 import { Dispatch } from 'redux';
18 import { getPropertyChip } from '../resource-properties-form/property-chip';
19 import { ResourceWithName } from '../data-explorer/renderers';
20 import { GroupClass } from "models/group";
21 import { openProjectUpdateDialog, ProjectUpdateFormDialogData } from 'store/projects/project-update-actions';
22 import { RootState } from 'store/store';
23 import { ResourcesState } from 'store/resources/resources';
24 import { resourceIsFrozen } from 'common/frozen-resources';
25 import { sanitizeHTML } from 'common/html-sanitize';
26
27 export class ProjectDetails extends DetailsData<ProjectResource> {
28     getIcon(className?: string) {
29         if (this.item.groupClass === GroupClass.FILTER) {
30             return <FilterGroupIcon className={className} />;
31         }
32         return <ProjectIcon className={className} />;
33     }
34
35     getDetails() {
36         return <ProjectDetailsComponent project={this.item} />;
37     }
38 }
39
40 type CssRules = 'tag' | 'editIcon' | 'editButton';
41
42 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
43     tag: {
44         marginRight: theme.spacing.unit / 2,
45         marginBottom: theme.spacing.unit / 2,
46     },
47     editIcon: {
48         paddingRight: theme.spacing.unit / 2,
49         fontSize: '1.125rem',
50     },
51     editButton: {
52         boxShadow: 'none',
53         padding: '2px 10px 2px 5px',
54         fontSize: '0.75rem'
55     },
56 });
57
58 interface ProjectDetailsComponentDataProps {
59     project: ProjectResource;
60 }
61
62 interface ProjectDetailsComponentActionProps {
63     onClick: (prj: ProjectUpdateFormDialogData) => () => void;
64 }
65
66 const mapStateToProps = (state: RootState): { resources: ResourcesState } => {
67     return {
68         resources: state.resources
69     };
70 };
71
72 const mapDispatchToProps = (dispatch: Dispatch) => ({
73     onClick: (prj: ProjectUpdateFormDialogData) =>
74         () => dispatch<any>(openProjectUpdateDialog(prj)),
75 });
76
77 type ProjectDetailsComponentProps = ProjectDetailsComponentDataProps & ProjectDetailsComponentActionProps & WithStyles<CssRules>;
78
79 const ProjectDetailsComponent = connect(mapStateToProps, mapDispatchToProps)(
80     withStyles(styles)(
81         ({ classes, project, resources, onClick }: ProjectDetailsComponentProps & { resources: ResourcesState }) => <div>
82             {project.groupClass !== GroupClass.FILTER ?
83                 <Button onClick={onClick({
84                     uuid: project.uuid,
85                     name: project.name,
86                     description: project.description,
87                     properties: project.properties,
88                 })}
89                     disabled={resourceIsFrozen(project, resources)}
90                     className={classes.editButton} variant='contained'
91                     data-cy='details-panel-edit-btn' color='primary' size='small'>
92                     <RenameIcon className={classes.editIcon} /> Edit
93                 </Button>
94                 : ''
95             }
96             <DetailsAttribute label='Type' value={project.groupClass === GroupClass.FILTER ? 'Filter group' : resourceLabel(ResourceKind.PROJECT)} />
97             <DetailsAttribute label='Owner' linkToUuid={project.ownerUuid}
98                 uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
99             <DetailsAttribute label='Last modified' value={formatDate(project.modifiedAt)} />
100             <DetailsAttribute label='Created at' value={formatDate(project.createdAt)} />
101             <DetailsAttribute label='UUID' linkToUuid={project.uuid} value={project.uuid} />
102             <DetailsAttribute label='Description'>
103                 {project.description ?
104                     <RichTextEditorLink
105                         title={`Description of ${project.name}`}
106                         content={sanitizeHTML(project.description)}
107                         label='Show full description' />
108                     : '---'
109                 }
110             </DetailsAttribute>
111             <DetailsAttribute label='Properties' />
112             {
113                 Object.keys(project.properties).map(k =>
114                     Array.isArray(project.properties[k])
115                         ? project.properties[k].map((v: string) =>
116                             getPropertyChip(k, v, undefined, classes.tag))
117                         : getPropertyChip(k, project.properties[k], undefined, classes.tag)
118                 )
119             }
120         </div>
121     ));