21128: fixed unwanted rowselects on other operational clicks Arvados-DCO-1.1-Signed...
[arvados.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 import { toggleOne } from 'store/multiselect/multiselect-actions';
42
43 type CssRules = "toolbar" | "button" | "root";
44
45 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
46     toolbar: {
47         paddingBottom: theme.spacing.unit * 3,
48         textAlign: "right"
49     },
50     button: {
51         marginLeft: theme.spacing.unit
52     },
53     root: {
54         width: '100%',
55     },
56 });
57
58 export enum FavoritePanelColumnNames {
59     NAME = "Name",
60     STATUS = "Status",
61     TYPE = "Type",
62     OWNER = "Owner",
63     FILE_SIZE = "File size",
64     LAST_MODIFIED = "Last modified"
65 }
66
67 export interface FavoritePanelFilter extends DataTableFilterItem {
68     type: ResourceKind | ContainerRequestState;
69 }
70
71 export const favoritePanelColumns: DataColumns<string, GroupContentsResource> = [
72     {
73         name: FavoritePanelColumnNames.NAME,
74         selected: true,
75         configurable: true,
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         filters: createTree(),
112         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
113     }
114 ];
115
116 interface FavoritePanelDataProps {
117     currentItemId: any;
118     favorites: FavoritesState;
119     resources: ResourcesState;
120     userUuid: string;
121 }
122
123 interface FavoritePanelActionProps {
124     onItemClick: (item: string) => void;
125     onDialogOpen: (ownerUuid: string) => void;
126     onItemDoubleClick: (item: string) => void;
127 }
128 const mapStateToProps = (state : RootState): FavoritePanelDataProps => ({
129     favorites: state.favorites,
130     resources: state.resources,
131     userUuid: state.auth.user!.uuid,
132     currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
133 });
134
135 type FavoritePanelProps = FavoritePanelDataProps & FavoritePanelActionProps & DispatchProp
136     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
137
138 export const FavoritePanel = withStyles(styles)(
139     connect(mapStateToProps)(
140         class extends React.Component<FavoritePanelProps> {
141
142             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
143                 const { resources } = this.props;
144                 const resource = getResource<GroupContentsResource>(resourceUuid)(resources);
145
146                 let readonly = false;
147                 const project = getResource<GroupResource>(this.props.currentItemId)(resources);
148
149                 if (project && project.groupClass === GroupClass.FILTER) {
150                     readonly = true;
151                 }
152
153                 const menuKind = this.props.dispatch<any>(resourceUuidToContextMenuKind(resourceUuid, readonly));
154
155                 if (menuKind && resource) {
156                     this.props.dispatch<any>(openContextMenu(event, {
157                         name: resource.name,
158                         uuid: resource.uuid,
159                         ownerUuid: resource.ownerUuid,
160                         isTrashed: ('isTrashed' in resource) ? resource.isTrashed: false,
161                         kind: resource.kind,
162                         menuKind,
163                         description: resource.description,
164                         storageClassesDesired: (resource as CollectionResource).storageClassesDesired,
165                     }));
166                 }
167                 this.props.dispatch<any>(loadDetailsPanel(resourceUuid));
168             }
169
170             handleRowDoubleClick = (uuid: string) => {
171                 this.props.dispatch<any>(navigateTo(uuid));
172             }
173
174             handleRowClick = (uuid: string) => {
175                 this.props.dispatch<any>(toggleOne(uuid))
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 );