Merge branch '13776-update-process-status-calculation'
[arvados-workbench2.git] / src / views / project-panel / project-panel.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 { Button, StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
7 import { DataExplorer } from "~/views-components/data-explorer/data-explorer";
8 import { DispatchProp, connect } from 'react-redux';
9 import { DataColumns } from '~/components/data-table/data-table';
10 import { RouteComponentProps } from 'react-router';
11 import { RootState } from '~/store/store';
12 import { DataTableFilterItem } from '~/components/data-table-filters/data-table-filters';
13 import { ContainerRequestState } from '~/models/container-request';
14 import { SortDirection } from '~/components/data-table/data-column';
15 import { ResourceKind } from '~/models/resource';
16 import { resourceLabel } from '~/common/labels';
17 import { ArvadosTheme } from '~/common/custom-theme';
18 import { ResourceFileSize, ResourceLastModifiedDate, ProcessStatus, ResourceType, ResourceOwner } from '~/views-components/data-explorer/renderers';
19 import { ProjectIcon } from '~/components/icon/icon';
20 import { ResourceName } from '~/views-components/data-explorer/renderers';
21 import { ResourcesState, getResource } from '~/store/resources/resources';
22 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
23 import { resourceKindToContextMenuKind, openContextMenu } from '~/store/context-menu/context-menu-actions';
24 import { ProjectResource } from '~/models/project';
25 import { navigateTo } from '~/store/navigation/navigation-action';
26 import { getProperty } from '~/store/properties/properties';
27 import { PROJECT_PANEL_CURRENT_UUID } from '~/store/project-panel/project-panel-action';
28 import { openCollectionCreateDialog } from '../../store/collections/collection-create-actions';
29 import { openProjectCreateDialog } from '~/store/projects/project-create-actions';
30
31 type CssRules = 'root' | "toolbar" | "button";
32
33 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
34     root: {
35         position: 'relative',
36         width: '100%',
37         height: '100%'
38     },
39     toolbar: {
40         paddingBottom: theme.spacing.unit * 3,
41         textAlign: "right"
42     },
43     button: {
44         marginLeft: theme.spacing.unit
45     },
46 });
47
48 export enum ProjectPanelColumnNames {
49     NAME = "Name",
50     STATUS = "Status",
51     TYPE = "Type",
52     OWNER = "Owner",
53     FILE_SIZE = "File size",
54     LAST_MODIFIED = "Last modified"
55 }
56
57 export interface ProjectPanelFilter extends DataTableFilterItem {
58     type: ResourceKind | ContainerRequestState;
59 }
60
61 export const projectPanelColumns: DataColumns<string, ProjectPanelFilter> = [
62     {
63         name: ProjectPanelColumnNames.NAME,
64         selected: true,
65         configurable: true,
66         sortDirection: SortDirection.ASC,
67         filters: [],
68         render: uuid => <ResourceName uuid={uuid} />,
69         width: "450px"
70     },
71     {
72         name: "Status",
73         selected: true,
74         configurable: true,
75         filters: [],
76         render: uuid => <ProcessStatus uuid={uuid} />,
77         width: "75px"
78     },
79     {
80         name: ProjectPanelColumnNames.TYPE,
81         selected: true,
82         configurable: true,
83         filters: [
84             {
85                 name: resourceLabel(ResourceKind.COLLECTION),
86                 selected: true,
87                 type: ResourceKind.COLLECTION
88             },
89             {
90                 name: resourceLabel(ResourceKind.PROCESS),
91                 selected: true,
92                 type: ResourceKind.PROCESS
93             },
94             {
95                 name: resourceLabel(ResourceKind.PROJECT),
96                 selected: true,
97                 type: ResourceKind.PROJECT
98             }
99         ],
100         render: uuid => <ResourceType uuid={uuid} />,
101         width: "125px"
102     },
103     {
104         name: ProjectPanelColumnNames.OWNER,
105         selected: true,
106         configurable: true,
107         filters: [],
108         render: uuid => <ResourceOwner uuid={uuid} />,
109         width: "200px"
110     },
111     {
112         name: ProjectPanelColumnNames.FILE_SIZE,
113         selected: true,
114         configurable: true,
115         filters: [],
116         render: uuid => <ResourceFileSize uuid={uuid} />,
117         width: "50px"
118     },
119     {
120         name: ProjectPanelColumnNames.LAST_MODIFIED,
121         selected: true,
122         configurable: true,
123         sortDirection: SortDirection.NONE,
124         filters: [],
125         render: uuid => <ResourceLastModifiedDate uuid={uuid} />,
126         width: "150px"
127     }
128 ];
129
130 export const PROJECT_PANEL_ID = "projectPanel";
131
132 interface ProjectPanelDataProps {
133     currentItemId: string;
134     resources: ResourcesState;
135 }
136
137 type ProjectPanelProps = ProjectPanelDataProps & DispatchProp
138     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
139
140 export const ProjectPanel = withStyles(styles)(
141     connect((state: RootState) => ({
142         currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
143         resources: state.resources
144     }))(
145         class extends React.Component<ProjectPanelProps> {
146             render() {
147                 const { classes } = this.props;
148                 return <div className={classes.root}>
149                     <div className={classes.toolbar}>
150                         <Button color="primary" onClick={this.handleNewCollectionClick} variant="raised" className={classes.button}>
151                             Create a collection
152                         </Button>
153                         <Button color="primary" variant="raised" className={classes.button}>
154                             Run a process
155                         </Button>
156                         <Button color="primary" onClick={this.handleNewProjectClick} variant="raised" className={classes.button}>
157                             New project
158                         </Button>
159                     </div>
160                     <DataExplorer
161                         id={PROJECT_PANEL_ID}
162                         onRowClick={this.handleRowClick}
163                         onRowDoubleClick={this.handleRowDoubleClick}
164                         onContextMenu={this.handleContextMenu}
165                         defaultIcon={ProjectIcon}
166                         defaultMessages={['Your project is empty.', 'Please create a project or create a collection and upload a data.']}
167                         contextMenuColumn={true}/>
168                 </div>;
169             }
170
171             handleNewProjectClick = () => {
172                 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
173             }
174
175             handleNewCollectionClick = () => {
176                 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
177             }
178
179             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
180                 const menuKind = resourceKindToContextMenuKind(resourceUuid);
181                 const resource = getResource<ProjectResource>(resourceUuid)(this.props.resources);
182                 if (menuKind && resource) {
183                     this.props.dispatch<any>(openContextMenu(event, {
184                         name: resource.name,
185                         uuid: resource.uuid,
186                         ownerUuid: resource.ownerUuid,
187                         isTrashed: resource.isTrashed,
188                         kind: resource.kind,
189                         menuKind
190                     }));
191                 }
192             }
193
194             handleRowDoubleClick = (uuid: string) => {
195                 this.props.dispatch<any>(navigateTo(uuid));
196             }
197
198             handleRowClick = (uuid: string) => {
199                 this.props.dispatch(loadDetailsPanel(uuid));
200             }
201
202         }
203     )
204 );