19988: Add resource type param to DataColumn to enable type-checked arbitrary field...
[arvados-workbench2.git] / src / views / favorite-panel / favorite-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 { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
7 import { DataExplorer } from "views-components/data-explorer/data-explorer";
8 import { connect, DispatchProp } from 'react-redux';
9 import { DataColumns } from 'components/data-table/data-table';
10 import { RouteComponentProps } from 'react-router';
11 import { DataTableFilterItem } from 'components/data-table-filters/data-table-filters';
12 import { ResourceKind } from 'models/resource';
13 import { ArvadosTheme } from 'common/custom-theme';
14 import { FAVORITE_PANEL_ID } from "store/favorite-panel/favorite-panel-action";
15 import {
16     ProcessStatus,
17     ResourceFileSize,
18     ResourceLastModifiedDate,
19     ResourceName,
20     ResourceOwnerWithName,
21     ResourceType
22 } from 'views-components/data-explorer/renderers';
23 import { FavoriteIcon } from 'components/icon/icon';
24 import {
25     openContextMenu,
26     resourceUuidToContextMenuKind
27 } from 'store/context-menu/context-menu-actions';
28 import { loadDetailsPanel } from 'store/details-panel/details-panel-action';
29 import { navigateTo } from 'store/navigation/navigation-action';
30 import { ContainerRequestState } from "models/container-request";
31 import { FavoritesState } from 'store/favorites/favorites-reducer';
32 import { RootState } from 'store/store';
33 import { createTree } from 'models/tree';
34 import { getSimpleObjectTypeFilters } from 'store/resource-type-filters/resource-type-filters';
35 import { getResource, ResourcesState } from 'store/resources/resources';
36 import { GroupContentsResource } from 'services/groups-service/groups-service';
37 import { GroupClass, GroupResource } from 'models/group';
38 import { getProperty } from 'store/properties/properties';
39 import { PROJECT_PANEL_CURRENT_UUID } from 'store/project-panel/project-panel-action';
40 import { CollectionResource } from 'models/collection';
41
42 type CssRules = "toolbar" | "button" | "root";
43
44 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
45     toolbar: {
46         paddingBottom: theme.spacing.unit * 3,
47         textAlign: "right"
48     },
49     button: {
50         marginLeft: theme.spacing.unit
51     },
52     root: {
53         width: '100%',
54     },
55 });
56
57 export enum FavoritePanelColumnNames {
58     NAME = "Name",
59     STATUS = "Status",
60     TYPE = "Type",
61     OWNER = "Owner",
62     FILE_SIZE = "File size",
63     LAST_MODIFIED = "Last modified"
64 }
65
66 export interface FavoritePanelFilter extends DataTableFilterItem {
67     type: ResourceKind | ContainerRequestState;
68 }
69
70 export const favoritePanelColumns: DataColumns<string, GroupContentsResource> = [
71     {
72         name: FavoritePanelColumnNames.NAME,
73         selected: true,
74         configurable: true,
75         // sort: {direction: SortDirection.NONE, field: "name"},
76         filters: createTree(),
77         render: uuid => <ResourceName uuid={uuid} />
78     },
79     {
80         name: "Status",
81         selected: true,
82         configurable: true,
83         filters: createTree(),
84         render: uuid => <ProcessStatus uuid={uuid} />
85     },
86     {
87         name: FavoritePanelColumnNames.TYPE,
88         selected: true,
89         configurable: true,
90         filters: getSimpleObjectTypeFilters(),
91         render: uuid => <ResourceType uuid={uuid} />
92     },
93     {
94         name: FavoritePanelColumnNames.OWNER,
95         selected: false,
96         configurable: true,
97         filters: createTree(),
98         render: uuid => <ResourceOwnerWithName uuid={uuid} />
99     },
100     {
101         name: FavoritePanelColumnNames.FILE_SIZE,
102         selected: true,
103         configurable: true,
104         filters: createTree(),
105         render: uuid => <ResourceFileSize uuid={uuid} />
106     },
107     {
108         name: FavoritePanelColumnNames.LAST_MODIFIED,
109         selected: true,
110         configurable: true,
111         // sort: {direction: SortDirection.DESC, field: "modifiedAt"},
112         filters: createTree(),
113         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
114     }
115 ];
116
117 interface FavoritePanelDataProps {
118     currentItemId: any;
119     favorites: FavoritesState;
120     resources: ResourcesState;
121     userUuid: string;
122 }
123
124 interface FavoritePanelActionProps {
125     onItemClick: (item: string) => void;
126     onDialogOpen: (ownerUuid: string) => void;
127     onItemDoubleClick: (item: string) => void;
128 }
129 const mapStateToProps = (state : RootState): FavoritePanelDataProps => ({
130     favorites: state.favorites,
131     resources: state.resources,
132     userUuid: state.auth.user!.uuid,
133     currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
134 });
135
136 type FavoritePanelProps = FavoritePanelDataProps & FavoritePanelActionProps & DispatchProp
137     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
138
139 export const FavoritePanel = withStyles(styles)(
140     connect(mapStateToProps)(
141         class extends React.Component<FavoritePanelProps> {
142
143             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
144                 const { resources } = this.props;
145                 const resource = getResource<GroupContentsResource>(resourceUuid)(resources);
146
147                 let readonly = false;
148                 const project = getResource<GroupResource>(this.props.currentItemId)(resources);
149
150                 if (project && project.groupClass === GroupClass.FILTER) {
151                     readonly = true;
152                 }
153
154                 const menuKind = this.props.dispatch<any>(resourceUuidToContextMenuKind(resourceUuid, readonly));
155
156                 if (menuKind && resource) {
157                     this.props.dispatch<any>(openContextMenu(event, {
158                         name: resource.name,
159                         uuid: resource.uuid,
160                         ownerUuid: resource.ownerUuid,
161                         isTrashed: ('isTrashed' in resource) ? resource.isTrashed: false,
162                         kind: resource.kind,
163                         menuKind,
164                         description: resource.description,
165                         storageClassesDesired: (resource as CollectionResource).storageClassesDesired,
166                     }));
167                 }
168                 this.props.dispatch<any>(loadDetailsPanel(resourceUuid));
169             }
170
171             handleRowDoubleClick = (uuid: string) => {
172                 this.props.dispatch<any>(navigateTo(uuid));
173             }
174
175             handleRowClick = (uuid: string) => {
176                 this.props.dispatch<any>(loadDetailsPanel(uuid));
177             }
178
179             render() {
180                 return <div className={this.props.classes.root}><DataExplorer
181                     id={FAVORITE_PANEL_ID}
182                     onRowClick={this.handleRowClick}
183                     onRowDoubleClick={this.handleRowDoubleClick}
184                     onContextMenu={this.handleContextMenu}
185                     contextMenuColumn={true}
186                     defaultViewIcon={FavoriteIcon}
187                     defaultViewMessages={['Your favorites list is empty.']} />
188                 </div>;
189             }
190         }
191     )
192 );