Add toolbar with project explorer actions
[arvados-workbench2.git] / src / views-components / project-explorer / project-explorer.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 { ProjectExplorerItem } from './project-explorer-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 '../data-explorer/data-explorer';
10 import { DataColumn } from '../../components/data-table/data-column';
11 import { DataTableFilterItem } from '../../components/data-table-filters/data-table-filters';
12 import { ContextMenuAction } from '../../components/context-menu/context-menu';
13 import { DispatchProp, connect } from 'react-redux';
14 import actions from "../../store/data-explorer/data-explorer-action";
15 import { DataColumns } from '../../components/data-table/data-table';
16
17 export const PROJECT_EXPLORER_ID = "projectExplorer";
18 class ProjectExplorer extends React.Component<DispatchProp & WithStyles<CssRules>> {
19     render() {
20         return <div>
21             <div className={this.props.classes.toolbar}>
22                 <Button color="primary" variant="raised" className={this.props.classes.button}>
23                     Create a collection
24                 </Button>
25                 <Button color="primary" variant="raised" className={this.props.classes.button}>
26                     Run a process
27                 </Button>
28                 <Button color="primary" variant="raised" className={this.props.classes.button}>
29                     Create a project
30                 </Button>
31             </div>
32             <DataExplorer
33                 id={PROJECT_EXPLORER_ID}
34                 contextActions={contextMenuActions}
35                 onColumnToggle={this.toggleColumn}
36                 onFiltersChange={this.changeFilters}
37                 onRowClick={console.log}
38                 onSortToggle={this.toggleSort}
39                 onSearch={this.search}
40                 onContextAction={this.executeAction}
41                 onChangePage={this.changePage}
42                 onChangeRowsPerPage={this.changeRowsPerPage} />;
43         </div>;
44     }
45
46     componentDidMount() {
47         this.props.dispatch(actions.SET_COLUMNS({ id: PROJECT_EXPLORER_ID, columns }));
48     }
49
50     toggleColumn = (toggledColumn: DataColumn<ProjectExplorerItem>) => {
51         this.props.dispatch(actions.TOGGLE_COLUMN({ id: PROJECT_EXPLORER_ID, columnName: toggledColumn.name }));
52     }
53
54     toggleSort = (toggledColumn: DataColumn<ProjectExplorerItem>) => {
55         this.props.dispatch(actions.TOGGLE_SORT({ id: PROJECT_EXPLORER_ID, columnName: toggledColumn.name }));
56     }
57
58     changeFilters = (filters: DataTableFilterItem[], updatedColumn: DataColumn<ProjectExplorerItem>) => {
59         this.props.dispatch(actions.SET_FILTERS({ id: PROJECT_EXPLORER_ID, columnName: updatedColumn.name, filters }));
60     }
61
62     executeAction = (action: ContextMenuAction, item: ProjectExplorerItem) => {
63         alert(`Executing ${action.name} on ${item.name}`);
64     }
65
66     search = (searchValue: string) => {
67         this.props.dispatch(actions.SET_SEARCH_VALUE({ id: PROJECT_EXPLORER_ID, searchValue }));
68     }
69
70     changePage = (page: number) => {
71         this.props.dispatch(actions.SET_PAGE({ id: PROJECT_EXPLORER_ID, page }));
72     }
73
74     changeRowsPerPage = (rowsPerPage: number) => {
75         this.props.dispatch(actions.SET_ROWS_PER_PAGE({ id: PROJECT_EXPLORER_ID, rowsPerPage }));
76     }
77 }
78
79 type CssRules = "toolbar" | "button";
80
81 const styles: StyleRulesCallback<CssRules> = theme => ({
82     toolbar: {
83         paddingBottom: theme.spacing.unit * 3,
84         textAlign: "right"
85     },
86     button: {
87         marginLeft: theme.spacing.unit
88     }
89 });
90
91 const renderName = (item: ProjectExplorerItem) =>
92     <Grid
93         container
94         alignItems="center"
95         wrap="nowrap"
96         spacing={16}>
97         <Grid item>
98             {renderIcon(item)}
99         </Grid>
100         <Grid item>
101             <Typography color="primary">
102                 {item.name}
103             </Typography>
104         </Grid>
105     </Grid>;
106
107 const renderIcon = (item: ProjectExplorerItem) => {
108     switch (item.type) {
109         case "arvados#group":
110             return <i className="fas fa-folder fa-lg" />;
111         case "arvados#groupList":
112             return <i className="fas fa-th fa-lg" />;
113         default:
114             return <i />;
115     }
116 };
117
118 const renderDate = (date: string) =>
119     <Typography noWrap>
120         {formatDate(date)}
121     </Typography>;
122
123 const renderFileSize = (fileSize?: number) =>
124     <Typography noWrap>
125         {formatFileSize(fileSize)}
126     </Typography>;
127
128 const renderOwner = (owner: string) =>
129     <Typography noWrap color="primary">
130         {owner}
131     </Typography>;
132
133 const renderType = (type: string) =>
134     <Typography noWrap>
135         {type}
136     </Typography>;
137
138 const renderStatus = (item: ProjectExplorerItem) =>
139     <Typography noWrap align="center">
140         {item.status || "-"}
141     </Typography>;
142
143 const columns: DataColumns<ProjectExplorerItem> = [{
144     name: "Name",
145     selected: true,
146     sortDirection: "asc",
147     render: renderName
148 }, {
149     name: "Status",
150     selected: true,
151     filters: [{
152         name: "In progress",
153         selected: true
154     }, {
155         name: "Complete",
156         selected: true
157     }],
158     render: renderStatus
159 }, {
160     name: "Type",
161     selected: true,
162     filters: [{
163         name: "Collection",
164         selected: true
165     }, {
166         name: "Group",
167         selected: true
168     }],
169     render: item => renderType(item.type)
170 }, {
171     name: "Owner",
172     selected: true,
173     render: item => renderOwner(item.owner)
174 }, {
175     name: "File size",
176     selected: true,
177     sortDirection: "none",
178     render: item => renderFileSize(item.fileSize)
179 }, {
180     name: "Last modified",
181     selected: true,
182     render: item => renderDate(item.lastModified)
183 }];
184
185 const contextMenuActions = [[{
186     icon: "fas fa-users fa-fw",
187     name: "Share"
188 }, {
189     icon: "fas fa-sign-out-alt fa-fw",
190     name: "Move to"
191 }, {
192     icon: "fas fa-star fa-fw",
193     name: "Add to favourite"
194 }, {
195     icon: "fas fa-edit fa-fw",
196     name: "Rename"
197 }, {
198     icon: "fas fa-copy fa-fw",
199     name: "Make a copy"
200 }, {
201     icon: "fas fa-download fa-fw",
202     name: "Download"
203 }], [{
204     icon: "fas fa-trash-alt fa-fw",
205     name: "Remove"
206 }
207 ]];
208
209 export default withStyles(styles)(connect()(ProjectExplorer));