Merge branch '22135-project-loading-indicator'
[arvados.git] / services / workbench2 / src / views / public-favorites-panel / public-favorites-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-column';
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 {
17     ProcessStatus,
18     ResourceFileSize,
19     ResourceLastModifiedDate,
20     ResourceType,
21     ResourceName,
22     ResourceOwnerWithName
23 } from 'views-components/data-explorer/renderers';
24 import { PublicFavoriteIcon } from 'components/icon/icon';
25 import { Dispatch } from 'redux';
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 { RootState } from 'store/store';
34 import { createTree } from 'models/tree';
35 import { getSimpleObjectTypeFilters } from 'store/resource-type-filters/resource-type-filters';
36 import { PUBLIC_FAVORITE_PANEL_ID } from 'store/public-favorites-panel/public-favorites-action';
37 import { PublicFavoritesState } from 'store/public-favorites/public-favorites-reducer';
38 import { getResource, ResourcesState } from 'store/resources/resources';
39 import { GroupContentsResource } from 'services/groups-service/groups-service';
40 import { CollectionResource } from 'models/collection';
41 import { toggleOne, deselectAllOthers } from 'store/multiselect/multiselect-actions';
42
43 type CssRules = "toolbar" | "button" | "root";
44
45 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
46     toolbar: {
47         paddingBottom: theme.spacing(3),
48         textAlign: "right"
49     },
50     button: {
51         marginLeft: theme.spacing(1)
52     },
53     root: {
54         width: '100%',
55     },
56 });
57
58 export enum PublicFavoritePanelColumnNames {
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 publicFavoritePanelColumns: DataColumns<string, GroupContentsResource> = [
72     {
73         name: PublicFavoritePanelColumnNames.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: PublicFavoritePanelColumnNames.TYPE,
88         selected: true,
89         configurable: true,
90         filters: getSimpleObjectTypeFilters(),
91         render: uuid => <ResourceType uuid={uuid} />
92     },
93     {
94         name: PublicFavoritePanelColumnNames.OWNER,
95         selected: false,
96         configurable: true,
97         filters: createTree(),
98         render: uuid => <ResourceOwnerWithName uuid={uuid} />
99     },
100     {
101         name: PublicFavoritePanelColumnNames.FILE_SIZE,
102         selected: true,
103         configurable: true,
104         filters: createTree(),
105         render: uuid => <ResourceFileSize uuid={uuid} />
106     },
107     {
108         name: PublicFavoritePanelColumnNames.LAST_MODIFIED,
109         selected: true,
110         configurable: true,
111         filters: createTree(),
112         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
113     }
114 ];
115
116 interface PublicFavoritePanelDataProps {
117     publicFavorites: PublicFavoritesState;
118     resources: ResourcesState;
119 }
120
121 interface PublicFavoritePanelActionProps {
122     onItemClick: (item: string) => void;
123     onContextMenu: (resources: ResourcesState) => (event: React.MouseEvent<HTMLElement>, item: string) => void;
124     onDialogOpen: (ownerUuid: string) => void;
125     onItemDoubleClick: (item: string) => void;
126 }
127 const mapStateToProps = ({ publicFavorites, resources }: RootState): PublicFavoritePanelDataProps => ({
128     publicFavorites,
129     resources,
130 });
131
132 const mapDispatchToProps = (dispatch: Dispatch): PublicFavoritePanelActionProps => ({
133     onContextMenu: (resources: ResourcesState) => (event, resourceUuid) => {
134         const resource = getResource<GroupContentsResource>(resourceUuid)(resources);
135         const kind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
136         if (kind && resource) {
137             dispatch<any>(openContextMenu(event, {
138                 name: resource.name,
139                 description: resource.description,
140                 storageClassesDesired: (resource as CollectionResource).storageClassesDesired,
141                 uuid: resourceUuid,
142                 ownerUuid: '',
143                 kind: ResourceKind.NONE,
144                 menuKind: kind
145             }));
146         }
147         dispatch<any>(loadDetailsPanel(resourceUuid));
148     },
149     onDialogOpen: (ownerUuid: string) => { return; },
150     onItemClick: (uuid: string) => {
151                 dispatch<any>(toggleOne(uuid))
152                 dispatch<any>(deselectAllOthers(uuid))
153                 dispatch<any>(loadDetailsPanel(uuid));
154     },
155     onItemDoubleClick: uuid => {
156         dispatch<any>(navigateTo(uuid));
157     }
158 });
159
160 type FavoritePanelProps = PublicFavoritePanelDataProps & PublicFavoritePanelActionProps & DispatchProp
161     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
162
163 export const PublicFavoritePanel = withStyles(styles)(
164     connect(mapStateToProps, mapDispatchToProps)(
165         class extends React.Component<FavoritePanelProps> {
166             render() {
167                 return <div className={this.props.classes.root}><DataExplorer
168                     id={PUBLIC_FAVORITE_PANEL_ID}
169                     onRowClick={this.props.onItemClick}
170                     onRowDoubleClick={this.props.onItemDoubleClick}
171                     onContextMenu={this.props.onContextMenu(this.props.resources)}
172                     contextMenuColumn={true}
173                     defaultViewIcon={PublicFavoriteIcon}
174                     defaultViewMessages={['Public favorites list is empty.']} />
175                 </div>;
176             }
177         }
178     )
179 );