18692: Initial frozen project implementation with tests
[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 React from 'react';
6 import withStyles from "@material-ui/core/styles/withStyles";
7 import { DispatchProp, connect } from 'react-redux';
8 import { RouteComponentProps } from 'react-router';
9 import { StyleRulesCallback, WithStyles } from "@material-ui/core";
10
11 import { DataExplorer } from "views-components/data-explorer/data-explorer";
12 import { DataColumns } from 'components/data-table/data-table';
13 import { RootState } from 'store/store';
14 import { DataTableFilterItem } from 'components/data-table-filters/data-table-filters';
15 import { ContainerRequestState } from 'models/container-request';
16 import { SortDirection } from 'components/data-table/data-column';
17 import { ResourceKind, Resource } from 'models/resource';
18 import {
19     ResourceFileSize,
20     ResourceLastModifiedDate,
21     ProcessStatus,
22     ResourceType,
23     ResourceOwnerWithName
24 } from 'views-components/data-explorer/renderers';
25 import { ProjectIcon } from 'components/icon/icon';
26 import { ResourceName } from 'views-components/data-explorer/renderers';
27 import {
28     ResourcesState,
29     getResource
30 } from 'store/resources/resources';
31 import { loadDetailsPanel } from 'store/details-panel/details-panel-action';
32 import {
33     openContextMenu,
34     resourceUuidToContextMenuKind
35 } from 'store/context-menu/context-menu-actions';
36 import { navigateTo } from 'store/navigation/navigation-action';
37 import { getProperty } from 'store/properties/properties';
38 import { PROJECT_PANEL_CURRENT_UUID } from 'store/project-panel/project-panel-action';
39 import { DataTableDefaultView } from 'components/data-table-default-view/data-table-default-view';
40 import { ArvadosTheme } from "common/custom-theme";
41 import { createTree } from 'models/tree';
42 import {
43     getInitialResourceTypeFilters,
44     getInitialProcessStatusFilters
45 } from 'store/resource-type-filters/resource-type-filters';
46 import { GroupContentsResource } from 'services/groups-service/groups-service';
47 import { GroupClass, GroupResource } from 'models/group';
48 import { CollectionResource } from 'models/collection';
49 import { resourceIsFrozen } from 'common/frozen-resources';
50
51 type CssRules = 'root' | "button";
52
53 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
54     root: {
55         width: '100%',
56     },
57     button: {
58         marginLeft: theme.spacing.unit
59     },
60 });
61
62 export enum ProjectPanelColumnNames {
63     NAME = "Name",
64     STATUS = "Status",
65     TYPE = "Type",
66     OWNER = "Owner",
67     FILE_SIZE = "File size",
68     LAST_MODIFIED = "Last modified"
69 }
70
71 export interface ProjectPanelFilter extends DataTableFilterItem {
72     type: ResourceKind | ContainerRequestState;
73 }
74
75 export const projectPanelColumns: DataColumns<string> = [
76     {
77         name: ProjectPanelColumnNames.NAME,
78         selected: true,
79         configurable: true,
80         sortDirection: SortDirection.NONE,
81         filters: createTree(),
82         render: uuid => <ResourceName uuid={uuid} />
83     },
84     {
85         name: "Status",
86         selected: true,
87         configurable: true,
88         mutuallyExclusiveFilters: true,
89         filters: getInitialProcessStatusFilters(),
90         render: uuid => <ProcessStatus uuid={uuid} />,
91     },
92     {
93         name: ProjectPanelColumnNames.TYPE,
94         selected: true,
95         configurable: true,
96         filters: getInitialResourceTypeFilters(),
97         render: uuid => <ResourceType uuid={uuid} />
98     },
99     {
100         name: ProjectPanelColumnNames.OWNER,
101         selected: false,
102         configurable: true,
103         filters: createTree(),
104         render: uuid => <ResourceOwnerWithName uuid={uuid} />
105     },
106     {
107         name: ProjectPanelColumnNames.FILE_SIZE,
108         selected: true,
109         configurable: true,
110         filters: createTree(),
111         render: uuid => <ResourceFileSize uuid={uuid} />
112     },
113     {
114         name: ProjectPanelColumnNames.LAST_MODIFIED,
115         selected: true,
116         configurable: true,
117         sortDirection: SortDirection.DESC,
118         filters: createTree(),
119         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
120     }
121 ];
122
123 export const PROJECT_PANEL_ID = "projectPanel";
124
125 const DEFAULT_VIEW_MESSAGES = [
126     'Your project is empty.',
127     'Please create a project or create a collection and upload a data.',
128 ];
129
130 interface ProjectPanelDataProps {
131     currentItemId: string;
132     resources: ResourcesState;
133     isAdmin: boolean;
134     userUuid: string;
135     dataExplorerItems: any;
136 }
137
138 type ProjectPanelProps = ProjectPanelDataProps & DispatchProp
139     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
140
141
142 export const ProjectPanel = withStyles(styles)(
143     connect((state: RootState) => ({
144         currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
145         resources: state.resources,
146         userUuid: state.auth.user!.uuid
147     }))(
148         class extends React.Component<ProjectPanelProps> {
149             render() {
150                 const { classes } = this.props;
151
152                 return <div data-cy='project-panel' className={classes.root}>
153                     <DataExplorer
154                         id={PROJECT_PANEL_ID}
155                         onRowClick={this.handleRowClick}
156                         onRowDoubleClick={this.handleRowDoubleClick}
157                         onContextMenu={this.handleContextMenu}
158                         contextMenuColumn={true}
159                         dataTableDefaultView={
160                             <DataTableDefaultView
161                                 icon={ProjectIcon}
162                                 messages={DEFAULT_VIEW_MESSAGES} />
163                         } />
164                 </div>;
165             }
166
167             isCurrentItemChild = (resource: Resource) => {
168                 return resource.ownerUuid === this.props.currentItemId;
169             }
170
171             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
172                 const { resources, isAdmin } = this.props;
173                 const resource = getResource<GroupContentsResource>(resourceUuid)(resources);
174                 // When viewing the contents of a filter group, all contents should be treated as read only.
175                 let readonly = false;
176                 const project = getResource<GroupResource>(this.props.currentItemId)(resources);
177                 if (project && project.groupClass === GroupClass.FILTER) {
178                     readonly = true;
179                 }
180
181                 const menuKind = this.props.dispatch<any>(resourceUuidToContextMenuKind(resourceUuid, readonly));
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: ('isTrashed' in resource) ? resource.isTrashed: false,
188                         kind: resource.kind,
189                         menuKind,
190                         isAdmin,
191                         isFrozen: resourceIsFrozen(resource, resources),
192                         description: resource.description,
193                         storageClassesDesired: (resource as CollectionResource).storageClassesDesired,
194                         properties: ('properties' in resource) ? resource.properties : {},
195                     }));
196                 }
197                 this.props.dispatch<any>(loadDetailsPanel(resourceUuid));
198             }
199
200             handleRowDoubleClick = (uuid: string) => {
201                 this.props.dispatch<any>(navigateTo(uuid));
202             }
203
204             handleRowClick = (uuid: string) => {
205                 this.props.dispatch<any>(loadDetailsPanel(uuid));
206             }
207
208         }
209     )
210 );