22141: Picker details display WIP
[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     hideEdit?: boolean;
63 }
64
65 interface ProjectDetailsComponentActionProps {
66     onClick: (prj: ProjectUpdateFormDialogData) => () => void;
67 }
68
69 const mapStateToProps = (state: RootState): { resources: ResourcesState } => {
70     return {
71         resources: state.resources
72     };
73 };
74
75 const mapDispatchToProps = (dispatch: Dispatch) => ({
76     onClick: (prj: ProjectUpdateFormDialogData) =>
77         () => dispatch<any>(openProjectUpdateDialog(prj)),
78 });
79
80 type ProjectDetailsComponentProps = ProjectDetailsComponentDataProps & ProjectDetailsComponentActionProps & WithStyles<CssRules>;
81
82 export const ProjectDetailsComponent = connect(mapStateToProps, mapDispatchToProps)(
83     withStyles(styles)(
84         ({ classes, project, resources, onClick, hideEdit }: ProjectDetailsComponentProps & { resources: ResourcesState }) => <div>
85             {project.groupClass !== GroupClass.FILTER && !hideEdit ?
86              <Button onClick={onClick({
87                  uuid: project.uuid,
88                  name: project.name,
89                  description: project.description,
90                  properties: project.properties,
91              })}
92                      disabled={resourceIsFrozen(project, resources)}
93                      className={classes.editButton} variant='contained'
94                      data-cy='details-panel-edit-btn' color='primary' size='small'>
95                  <RenameIcon className={classes.editIcon} /> Edit
96              </Button>
97             : ''
98             }
99             <DetailsAttribute label='Type' value={project.groupClass === GroupClass.FILTER ? 'Filter group' : resourceLabel(ResourceKind.PROJECT)} />
100             <DetailsAttribute label='UUID' linkToUuid={project.uuid} value={project.uuid} />
101             <DetailsAttribute label='Owner' linkToUuid={project.ownerUuid}
102                               uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
103             <DetailsAttribute label='Created at' value={formatDate(project.createdAt)} />
104             <DetailsAttribute label='Last modified' value={formatDate(project.modifiedAt)} />
105             <DetailsAttribute label='Last modified by' linkToUuid={project.modifiedByUserUuid}
106                               uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
107             <DetailsAttribute label='Description'>
108                 {project.description ?
109                  <RichTextEditorLink
110                      title={`Description of ${project.name}`}
111                      content={project.description}
112                      label='Show full description' />
113                 : '---'
114                 }
115             </DetailsAttribute>
116             <DetailsAttribute label='Properties' />
117             {
118                 Object.keys(project.properties).map(k =>
119                     Array.isArray(project.properties[k])
120                     ? project.properties[k].map((v: string) =>
121                         getPropertyChip(k, v, undefined, classes.tag))
122                     : getPropertyChip(k, project.properties[k], undefined, classes.tag)
123                 )
124             }
125         </div>
126 ));