Merge remote-tracking branch 'origin/main' into 18207-Workbench2-is-not-clearing...
[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     ResourceOwner
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
50 type CssRules = 'root' | "button";
51
52 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
53     root: {
54         width: '100%',
55     },
56     button: {
57         marginLeft: theme.spacing.unit
58     },
59 });
60
61 export enum ProjectPanelColumnNames {
62     NAME = "Name",
63     STATUS = "Status",
64     TYPE = "Type",
65     OWNER = "Owner",
66     FILE_SIZE = "File size",
67     LAST_MODIFIED = "Last modified"
68 }
69
70 export interface ProjectPanelFilter extends DataTableFilterItem {
71     type: ResourceKind | ContainerRequestState;
72 }
73
74 export const projectPanelColumns: DataColumns<string> = [
75     {
76         name: ProjectPanelColumnNames.NAME,
77         selected: true,
78         configurable: true,
79         sortDirection: SortDirection.NONE,
80         filters: createTree(),
81         render: uuid => <ResourceName uuid={uuid} />
82     },
83     {
84         name: "Status",
85         selected: true,
86         configurable: true,
87         mutuallyExclusiveFilters: true,
88         filters: getInitialProcessStatusFilters(),
89         render: uuid => <ProcessStatus uuid={uuid} />,
90     },
91     {
92         name: ProjectPanelColumnNames.TYPE,
93         selected: true,
94         configurable: true,
95         filters: getInitialResourceTypeFilters(),
96         render: uuid => <ResourceType uuid={uuid} />
97     },
98     {
99         name: ProjectPanelColumnNames.OWNER,
100         selected: false,
101         configurable: true,
102         filters: createTree(),
103         render: uuid => <ResourceOwner uuid={uuid} />
104     },
105     {
106         name: ProjectPanelColumnNames.FILE_SIZE,
107         selected: true,
108         configurable: true,
109         filters: createTree(),
110         render: uuid => <ResourceFileSize uuid={uuid} />
111     },
112     {
113         name: ProjectPanelColumnNames.LAST_MODIFIED,
114         selected: true,
115         configurable: true,
116         sortDirection: SortDirection.DESC,
117         filters: createTree(),
118         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
119     }
120 ];
121
122 export const PROJECT_PANEL_ID = "projectPanel";
123
124 const DEFAULT_VIEW_MESSAGES = [
125     'Your project is empty.',
126     'Please create a project or create a collection and upload a data.',
127 ];
128
129 interface ProjectPanelDataProps {
130     currentItemId: string;
131     resources: ResourcesState;
132     isAdmin: boolean;
133     userUuid: string;
134     dataExplorerItems: any;
135 }
136
137 type ProjectPanelProps = ProjectPanelDataProps & DispatchProp
138     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
139
140 let data: any[] = [];
141 let href: string = '';
142
143 export const ProjectPanel = withStyles(styles)(
144     connect((state: RootState) => ({
145         currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
146         resources: state.resources,
147         userUuid: state.auth.user!.uuid,
148         dataExplorerItems: state.dataExplorer?.projectPanel.items,
149     }))(
150         class extends React.Component<ProjectPanelProps> {
151             render() {
152                 const { classes, dataExplorerItems } = this.props;
153                 let loading = false;
154
155                 if (dataExplorerItems.length > 0 && data === dataExplorerItems && href !== window.location.href) {
156                     loading = true
157                 } else {
158                     href = window.location.href;
159                     data = dataExplorerItems;
160                 }
161
162                 return <div data-cy='project-panel' className={classes.root}>
163                     <DataExplorer
164                         working={loading}
165                         id={PROJECT_PANEL_ID}
166                         onRowClick={this.handleRowClick}
167                         onRowDoubleClick={this.handleRowDoubleClick}
168                         onContextMenu={this.handleContextMenu}
169                         contextMenuColumn={true}
170                         dataTableDefaultView={
171                             <DataTableDefaultView
172                                 icon={ProjectIcon}
173                                 messages={DEFAULT_VIEW_MESSAGES} />
174                         } />
175                 </div>;
176             }
177
178             isCurrentItemChild = (resource: Resource) => {
179                 return resource.ownerUuid === this.props.currentItemId;
180             }
181
182             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
183                 const { resources } = this.props;
184                 const resource = getResource<GroupContentsResource>(resourceUuid)(resources);
185                 // When viewing the contents of a filter group, all contents should be treated as read only.
186                 let readonly = false;
187                 const project = getResource<GroupResource>(this.props.currentItemId)(resources);
188                 if (project && project.groupClass === GroupClass.FILTER) {
189                     readonly = true;
190                 }
191
192                 const menuKind = this.props.dispatch<any>(resourceUuidToContextMenuKind(resourceUuid, readonly));
193                 if (menuKind && resource) {
194                     this.props.dispatch<any>(openContextMenu(event, {
195                         name: resource.name,
196                         uuid: resource.uuid,
197                         ownerUuid: resource.ownerUuid,
198                         isTrashed: ('isTrashed' in resource) ? resource.isTrashed: false,
199                         kind: resource.kind,
200                         menuKind,
201                         description: resource.description,
202                         storageClassesDesired: (resource as CollectionResource).storageClassesDesired,
203                     }));
204                 }
205                 this.props.dispatch<any>(loadDetailsPanel(resourceUuid));
206             }
207
208             handleRowDoubleClick = (uuid: string) => {
209                 this.props.dispatch<any>(navigateTo(uuid));
210             }
211
212             handleRowClick = (uuid: string) => {
213                 this.props.dispatch<any>(loadDetailsPanel(uuid));
214             }
215
216         }
217     )
218 );