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