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