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