Refactor to apply global navigation actions
[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 { ContextMenuKind } from '~/views-components/context-menu/context-menu';
24 import { contextMenuActions } from '~/store/context-menu/context-menu-actions';
25 import { CollectionResource } from '~/models/collection';
26 import { ProjectResource } from '~/models/project';
27 import { openProjectCreator } from '~/store/project/project-action';
28 import { reset } from 'redux-form';
29 import { COLLECTION_CREATE_DIALOG } from '~/views-components/dialog-create/dialog-collection-create';
30 import { collectionCreateActions } from '~/store/collections/creator/collection-creator-action';
31 import { navigateToResource } from '~/store/navigation/navigation-action';
32 import { getProperty } from '~/store/properties/properties';
33 import { PROJECT_PANEL_CURRENT_UUID } from '~/store/project-panel/project-panel-action';
34
35 type CssRules = 'root' | "toolbar" | "button";
36
37 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
38     root: {
39         position: 'relative',
40         width: '100%',
41         height: '100%'
42     },
43     toolbar: {
44         paddingBottom: theme.spacing.unit * 3,
45         textAlign: "right"
46     },
47     button: {
48         marginLeft: theme.spacing.unit
49     },
50 });
51
52 export enum ProjectPanelColumnNames {
53     NAME = "Name",
54     STATUS = "Status",
55     TYPE = "Type",
56     OWNER = "Owner",
57     FILE_SIZE = "File size",
58     LAST_MODIFIED = "Last modified"
59 }
60
61 export interface ProjectPanelFilter extends DataTableFilterItem {
62     type: ResourceKind | ContainerRequestState;
63 }
64
65 export const projectPanelColumns: DataColumns<string, ProjectPanelFilter> = [
66     {
67         name: ProjectPanelColumnNames.NAME,
68         selected: true,
69         configurable: true,
70         sortDirection: SortDirection.ASC,
71         filters: [],
72         render: uuid => <ResourceName uuid={uuid} />,
73         width: "450px"
74     },
75     {
76         name: "Status",
77         selected: true,
78         configurable: true,
79         sortDirection: SortDirection.NONE,
80         filters: [
81             {
82                 name: ContainerRequestState.COMMITTED,
83                 selected: true,
84                 type: ContainerRequestState.COMMITTED
85             },
86             {
87                 name: ContainerRequestState.FINAL,
88                 selected: true,
89                 type: ContainerRequestState.FINAL
90             },
91             {
92                 name: ContainerRequestState.UNCOMMITTED,
93                 selected: true,
94                 type: ContainerRequestState.UNCOMMITTED
95             }
96         ],
97         render: uuid => <ProcessStatus uuid={uuid} />,
98         width: "75px"
99     },
100     {
101         name: ProjectPanelColumnNames.TYPE,
102         selected: true,
103         configurable: true,
104         sortDirection: SortDirection.NONE,
105         filters: [
106             {
107                 name: resourceLabel(ResourceKind.COLLECTION),
108                 selected: true,
109                 type: ResourceKind.COLLECTION
110             },
111             {
112                 name: resourceLabel(ResourceKind.PROCESS),
113                 selected: true,
114                 type: ResourceKind.PROCESS
115             },
116             {
117                 name: resourceLabel(ResourceKind.PROJECT),
118                 selected: true,
119                 type: ResourceKind.PROJECT
120             }
121         ],
122         render: uuid => <ResourceType uuid={uuid} />,
123         width: "125px"
124     },
125     {
126         name: ProjectPanelColumnNames.OWNER,
127         selected: true,
128         configurable: true,
129         sortDirection: SortDirection.NONE,
130         filters: [],
131         render: uuid => <ResourceOwner uuid={uuid} />,
132         width: "200px"
133     },
134     {
135         name: ProjectPanelColumnNames.FILE_SIZE,
136         selected: true,
137         configurable: true,
138         sortDirection: SortDirection.NONE,
139         filters: [],
140         render: uuid => <ResourceFileSize uuid={uuid} />,
141         width: "50px"
142     },
143     {
144         name: ProjectPanelColumnNames.LAST_MODIFIED,
145         selected: true,
146         configurable: true,
147         sortDirection: SortDirection.NONE,
148         filters: [],
149         render: uuid => <ResourceLastModifiedDate uuid={uuid} />,
150         width: "150px"
151     }
152 ];
153
154 export const PROJECT_PANEL_ID = "projectPanel";
155
156 interface ProjectPanelDataProps {
157     currentItemId: string;
158     resources: ResourcesState;
159 }
160
161 type ProjectPanelProps = ProjectPanelDataProps & DispatchProp
162     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
163
164 export const ProjectPanel = withStyles(styles)(
165     connect((state: RootState) => ({
166         currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
167         resources: state.resources
168     }))(
169         class extends React.Component<ProjectPanelProps> {
170             render() {
171                 const { classes } = this.props;
172                 return <div className={classes.root}>
173                     <div className={classes.toolbar}>
174                         <Button color="primary" onClick={this.handleNewCollectionClick} variant="raised" className={classes.button}>
175                             Create a collection
176                         </Button>
177                         <Button color="primary" variant="raised" className={classes.button}>
178                             Run a process
179                         </Button>
180                         <Button color="primary" onClick={this.handleNewProjectClick} variant="raised" className={classes.button}>
181                             New project
182                         </Button>
183                     </div>
184                     <DataExplorer
185                         id={PROJECT_PANEL_ID}
186                         onRowClick={this.handleRowClick}
187                         onRowDoubleClick={this.handleRowDoubleClick}
188                         onContextMenu={this.handleContextMenu}
189                         defaultIcon={ProjectIcon}
190                         defaultMessages={['Your project is empty.', 'Please create a project or create a collection and upload a data.']} />
191                 </div>;
192             }
193
194             handleNewProjectClick = () => {
195                 this.props.dispatch<any>(openProjectCreator(this.props.currentItemId));
196             }
197
198             handleNewCollectionClick = () => {
199                 this.props.dispatch(reset(COLLECTION_CREATE_DIALOG));
200                 this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: this.props.currentItemId }));
201             }
202
203             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
204                 event.preventDefault();
205                 const resource = getResource(resourceUuid)(this.props.resources) as CollectionResource | ProjectResource | undefined;
206                 if (resource) {
207                     let kind: ContextMenuKind;
208
209                     if (resource.kind === ResourceKind.PROJECT) {
210                         kind = ContextMenuKind.PROJECT;
211                     } else if (resource.kind === ResourceKind.COLLECTION) {
212                         kind = ContextMenuKind.COLLECTION_RESOURCE;
213                     } else {
214                         kind = ContextMenuKind.RESOURCE;
215                     }
216                     if (kind !== ContextMenuKind.RESOURCE) {
217                         this.props.dispatch(
218                             contextMenuActions.OPEN_CONTEXT_MENU({
219                                 position: { x: event.clientX, y: event.clientY },
220                                 resource: {
221                                     uuid: resource.uuid,
222                                     name: resource.name || '',
223                                     description: resource.description,
224                                     kind,
225                                 }
226                             })
227                         );
228                     }
229                 }
230             }
231
232             handleRowDoubleClick = (uuid: string) => {
233                 this.props.dispatch<any>(navigateToResource(uuid));
234             }
235
236             handleRowClick = (uuid: string) => {
237                 this.props.dispatch(loadDetailsPanel(uuid));
238             }
239
240         }
241     )
242 );