Merge branch '13775-custom-theme-test-errors'
[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 { ProjectPanelItem } from './project-panel-item';
7 import { Grid, Typography, Button, Toolbar, StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
8 import { formatDate, formatFileSize } from '../../common/formatters';
9 import DataExplorer from "../../views-components/data-explorer/data-explorer";
10 import { ContextMenuAction } from '../../components/context-menu/context-menu';
11 import { DispatchProp, connect } from 'react-redux';
12 import { DataColumns } from '../../components/data-table/data-table';
13 import { RouteComponentProps } from 'react-router';
14 import { RootState } from '../../store/store';
15 import { ResourceKind } from '../../models/kinds';
16 import { DataTableFilterItem } from '../../components/data-table-filters/data-table-filters';
17 import { ContainerRequestState } from '../../models/container-request';
18 import { SortDirection } from '../../components/data-table/data-column';
19
20 export const PROJECT_PANEL_ID = "projectPanel";
21
22 export interface ProjectPanelFilter extends DataTableFilterItem {
23     type: ResourceKind | ContainerRequestState;
24 }
25
26 type ProjectPanelProps = {
27     currentItemId: string,
28     onItemClick: (item: ProjectPanelItem) => void,
29     onItemRouteChange: (itemId: string) => void
30 }
31     & DispatchProp
32     & WithStyles<CssRules>
33     & RouteComponentProps<{ id: string }>;
34 class ProjectPanel extends React.Component<ProjectPanelProps> {
35     render() {
36         return <div>
37             <div className={this.props.classes.toolbar}>
38                 <Button color="primary" variant="raised" className={this.props.classes.button}>
39                     Create a collection
40                 </Button>
41                 <Button color="primary" variant="raised" className={this.props.classes.button}>
42                     Run a process
43                 </Button>
44                 <Button color="primary" variant="raised" className={this.props.classes.button}>
45                     Create a project
46                 </Button>
47             </div>
48             <DataExplorer
49                 id={PROJECT_PANEL_ID}
50                 contextActions={contextMenuActions}
51                 onRowClick={this.props.onItemClick}
52                 onContextAction={this.executeAction} />;
53         </div>;
54     }
55
56     componentWillReceiveProps({ match, currentItemId }: ProjectPanelProps) {
57         if (match.params.id !== currentItemId) {
58             this.props.onItemRouteChange(match.params.id);
59         }
60     }
61
62     executeAction = (action: ContextMenuAction, item: ProjectPanelItem) => {
63         alert(`Executing ${action.name} on ${item.name}`);
64     }
65
66 }
67
68 type CssRules = "toolbar" | "button";
69
70 const styles: StyleRulesCallback<CssRules> = theme => ({
71     toolbar: {
72         paddingBottom: theme.spacing.unit * 3,
73         textAlign: "right"
74     },
75     button: {
76         marginLeft: theme.spacing.unit
77     }
78 });
79
80 const renderName = (item: ProjectPanelItem) =>
81     <Grid
82         container
83         alignItems="center"
84         wrap="nowrap"
85         spacing={16}>
86         <Grid item>
87             {renderIcon(item)}
88         </Grid>
89         <Grid item>
90             <Typography color="primary">
91                 {item.name}
92             </Typography>
93         </Grid>
94     </Grid>;
95
96
97 const renderIcon = (item: ProjectPanelItem) => {
98     switch (item.kind) {
99         case ResourceKind.Project:
100             return <i className="fas fa-folder fa-lg" />;
101         case ResourceKind.Collection:
102             return <i className="fas fa-archive fa-lg" />;
103         case ResourceKind.Process:
104             return <i className="fas fa-cogs fa-lg" />;
105         default:
106             return <i />;
107     }
108 };
109
110 const renderDate = (date: string) =>
111     <Typography noWrap>
112         {formatDate(date)}
113     </Typography>;
114
115 const renderFileSize = (fileSize?: number) =>
116     <Typography noWrap>
117         {formatFileSize(fileSize)}
118     </Typography>;
119
120 const renderOwner = (owner: string) =>
121     <Typography noWrap color="primary">
122         {owner}
123     </Typography>;
124
125
126
127 const typeToLabel = (type: string) => {
128     switch (type) {
129         case ResourceKind.Collection:
130             return "Data collection";
131         case ResourceKind.Project:
132             return "Project";
133         case ResourceKind.Process:
134             return "Process";
135         default:
136             return "Unknown";
137     }
138 };
139
140 const renderType = (type: string) => {
141     return <Typography noWrap>
142         {typeToLabel(type)}
143     </Typography>;
144 };
145
146 const renderStatus = (item: ProjectPanelItem) =>
147     <Typography noWrap align="center">
148         {item.status || "-"}
149     </Typography>;
150
151 export enum ProjectPanelColumnNames {
152     Name = "Name",
153     Status = "Status",
154     Type = "Type",
155     Owner = "Owner",
156     FileSize = "File size",
157     LastModified = "Last modified"
158
159 }
160
161 export const columns: DataColumns<ProjectPanelItem, ProjectPanelFilter> = [{
162     name: ProjectPanelColumnNames.Name,
163     selected: true,
164     sortDirection: SortDirection.Asc,
165     render: renderName,
166     width: "450px"
167 }, {
168     name: "Status",
169     selected: true,
170     filters: [{
171         name: ContainerRequestState.Committed,
172         selected: true,
173         type: ContainerRequestState.Committed
174     }, {
175         name: ContainerRequestState.Final,
176         selected: true,
177         type: ContainerRequestState.Final
178     }, {
179         name: ContainerRequestState.Uncommitted,
180         selected: true,
181         type: ContainerRequestState.Uncommitted
182     }],
183     render: renderStatus,
184     width: "75px"
185 }, {
186     name: ProjectPanelColumnNames.Type,
187     selected: true,
188     filters: [{
189         name: typeToLabel(ResourceKind.Collection),
190         selected: true,
191         type: ResourceKind.Collection
192     }, {
193         name: typeToLabel(ResourceKind.Process),
194         selected: true,
195         type: ResourceKind.Process
196     }, {
197         name: typeToLabel(ResourceKind.Project),
198         selected: true,
199         type: ResourceKind.Project
200     }],
201     render: item => renderType(item.kind),
202     width: "125px"
203 }, {
204     name: ProjectPanelColumnNames.Owner,
205     selected: true,
206     render: item => renderOwner(item.owner),
207     width: "200px"
208 }, {
209     name: ProjectPanelColumnNames.FileSize,
210     selected: true,
211     render: item => renderFileSize(item.fileSize),
212     width: "50px"
213 }, {
214     name: ProjectPanelColumnNames.LastModified,
215     selected: true,
216     sortDirection: SortDirection.None,
217     render: item => renderDate(item.lastModified),
218     width: "150px"
219 }];
220
221 const contextMenuActions = [[{
222     icon: "fas fa-users fa-fw",
223     name: "Share"
224 }, {
225     icon: "fas fa-sign-out-alt fa-fw",
226     name: "Move to"
227 }, {
228     icon: "fas fa-star fa-fw",
229     name: "Add to favourite"
230 }, {
231     icon: "fas fa-edit fa-fw",
232     name: "Rename"
233 }, {
234     icon: "fas fa-copy fa-fw",
235     name: "Make a copy"
236 }, {
237     icon: "fas fa-download fa-fw",
238     name: "Download"
239 }], [{
240     icon: "fas fa-trash-alt fa-fw",
241     name: "Remove"
242 }
243 ]];
244
245 export default withStyles(styles)(
246     connect((state: RootState) => ({ currentItemId: state.projects.currentItemId }))(
247         ProjectPanel));