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