Reuse DataColumns type
[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 } 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> {
19
20     contextMenuActions = [[{
21         icon: "fas fa-users fa-fw",
22         name: "Share"
23     }, {
24         icon: "fas fa-sign-out-alt fa-fw",
25         name: "Move to"
26     }, {
27         icon: "fas fa-star fa-fw",
28         name: "Add to favourite"
29     }, {
30         icon: "fas fa-edit fa-fw",
31         name: "Rename"
32     }, {
33         icon: "fas fa-copy fa-fw",
34         name: "Make a copy"
35     }, {
36         icon: "fas fa-download fa-fw",
37         name: "Download"
38     }], [{
39         icon: "fas fa-trash-alt fa-fw",
40         name: "Remove"
41     }
42     ]];
43
44     render() {
45         return <DataExplorer
46             id={PROJECT_EXPLORER_ID}
47             contextActions={this.contextMenuActions}
48             onColumnToggle={this.toggleColumn}
49             onFiltersChange={this.changeFilters}
50             onRowClick={console.log}
51             onSortToggle={this.toggleSort}
52             onSearch={this.search}
53             onContextAction={this.executeAction}
54             onChangePage={this.changePage}
55             onChangeRowsPerPage={this.changeRowsPerPage} />;
56     }
57
58     componentDidMount() {
59         this.props.dispatch(actions.SET_COLUMNS({ id: PROJECT_EXPLORER_ID, columns }));
60     }
61
62     toggleColumn = (toggledColumn: DataColumn<ProjectExplorerItem>) => {
63         this.props.dispatch(actions.TOGGLE_COLUMN({ id: PROJECT_EXPLORER_ID, columnName: toggledColumn.name }));
64     }
65
66     toggleSort = (toggledColumn: DataColumn<ProjectExplorerItem>) => {
67         this.props.dispatch(actions.TOGGLE_SORT({ id: PROJECT_EXPLORER_ID, columnName: toggledColumn.name }));
68     }
69
70     changeFilters = (filters: DataTableFilterItem[], updatedColumn: DataColumn<ProjectExplorerItem>) => {
71         this.props.dispatch(actions.SET_FILTERS({ id: PROJECT_EXPLORER_ID, columnName: updatedColumn.name, filters }));
72     }
73
74     executeAction = (action: ContextMenuAction, item: ProjectExplorerItem) => {
75         alert(`Executing ${action.name} on ${item.name}`);
76     }
77
78     search = (searchValue: string) => {
79         this.props.dispatch(actions.SET_SEARCH_VALUE({ id: PROJECT_EXPLORER_ID, searchValue }));
80     }
81
82     changePage = (page: number) => {
83         this.props.dispatch(actions.SET_PAGE({ id: PROJECT_EXPLORER_ID, page }));
84     }
85
86     changeRowsPerPage = (rowsPerPage: number) => {
87         this.props.dispatch(actions.SET_ROWS_PER_PAGE({ id: PROJECT_EXPLORER_ID, rowsPerPage }));
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 export default connect()(ProjectExplorer);