21720: implemented CustomStyleRulesCallback and replaced everywhere
[arvados.git] / services / workbench2 / 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 { CustomStyleRulesCallback } from 'common/custom-theme';
16 import { withStyles, WithStyles, Button } from '@material-ui/core';
17 import { ArvadosTheme } from 'common/custom-theme';
18 import { Dispatch } from 'redux';
19 import { getPropertyChip } from '../resource-properties-form/property-chip';
20 import { ResourceWithName } from '../data-explorer/renderers';
21 import { GroupClass } from "models/group";
22 import { openProjectUpdateDialog, ProjectUpdateFormDialogData } from 'store/projects/project-update-actions';
23 import { RootState } from 'store/store';
24 import { ResourcesState } from 'store/resources/resources';
25 import { resourceIsFrozen } from 'common/frozen-resources';
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: CustomStyleRulesCallback<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='UUID' linkToUuid={project.uuid} value={project.uuid} />
98             <DetailsAttribute label='Owner' linkToUuid={project.ownerUuid}
99                 uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
100             <DetailsAttribute label='Created at' value={formatDate(project.createdAt)} />
101             <DetailsAttribute label='Last modified' value={formatDate(project.modifiedAt)} />
102             <DetailsAttribute label='Last modified by' linkToUuid={project.modifiedByUserUuid}
103                 uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
104             <DetailsAttribute label='Description'>
105                 {project.description ?
106                     <RichTextEditorLink
107                         title={`Description of ${project.name}`}
108                         content={project.description}
109                         label='Show full description' />
110                     : '---'
111                 }
112             </DetailsAttribute>
113             <DetailsAttribute label='Properties' />
114             {
115                 Object.keys(project.properties).map(k =>
116                     Array.isArray(project.properties[k])
117                         ? project.properties[k].map((v: string) =>
118                             getPropertyChip(k, v, undefined, classes.tag))
119                         : getPropertyChip(k, project.properties[k], undefined, classes.tag)
120                 )
121             }
122         </div>
123     ));