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