Merge branch '19434-collapse-left-panel' closes #19434
[arvados-workbench2.git] / 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 { 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 { SortDirection } from 'components/data-table/data-column';
13 import { ResourceKind } from 'models/resource';
14 import { ArvadosTheme } from 'common/custom-theme';
15 import {
16     ProcessStatus,
17     ResourceFileSize,
18     ResourceLastModifiedDate,
19     ResourceType,
20     ResourceName,
21     ResourceOwnerWithName
22 } from 'views-components/data-explorer/renderers';
23 import { PublicFavoriteIcon } from 'components/icon/icon';
24 import { Dispatch } from 'redux';
25 import {
26     openContextMenu,
27     resourceUuidToContextMenuKind
28 } from 'store/context-menu/context-menu-actions';
29 import { loadDetailsPanel } from 'store/details-panel/details-panel-action';
30 import { navigateTo } from 'store/navigation/navigation-action';
31 import { ContainerRequestState } from "models/container-request";
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 { PUBLIC_FAVORITE_PANEL_ID } from 'store/public-favorites-panel/public-favorites-action';
36 import { PublicFavoritesState } from 'store/public-favorites/public-favorites-reducer';
37 import { getResource, ResourcesState } from 'store/resources/resources';
38 import { GroupContentsResource } from 'services/groups-service/groups-service';
39 import { CollectionResource } from 'models/collection';
40
41 type CssRules = "toolbar" | "button" | "root";
42
43 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
44     toolbar: {
45         paddingBottom: theme.spacing.unit * 3,
46         textAlign: "right"
47     },
48     button: {
49         marginLeft: theme.spacing.unit
50     },
51     root: {
52         width: '100%',
53     },
54 });
55
56 export enum PublicFavoritePanelColumnNames {
57     NAME = "Name",
58     STATUS = "Status",
59     TYPE = "Type",
60     OWNER = "Owner",
61     FILE_SIZE = "File size",
62     LAST_MODIFIED = "Last modified"
63 }
64
65 export interface FavoritePanelFilter extends DataTableFilterItem {
66     type: ResourceKind | ContainerRequestState;
67 }
68
69 export const publicFavoritePanelColumns: DataColumns<string> = [
70     {
71         name: PublicFavoritePanelColumnNames.NAME,
72         selected: true,
73         configurable: true,
74         sortDirection: SortDirection.NONE,
75         filters: createTree(),
76         render: uuid => <ResourceName uuid={uuid} />
77     },
78     {
79         name: "Status",
80         selected: true,
81         configurable: true,
82         filters: createTree(),
83         render: uuid => <ProcessStatus uuid={uuid} />
84     },
85     {
86         name: PublicFavoritePanelColumnNames.TYPE,
87         selected: true,
88         configurable: true,
89         filters: getSimpleObjectTypeFilters(),
90         render: uuid => <ResourceType uuid={uuid} />
91     },
92     {
93         name: PublicFavoritePanelColumnNames.OWNER,
94         selected: false,
95         configurable: true,
96         filters: createTree(),
97         render: uuid => <ResourceOwnerWithName uuid={uuid} />
98     },
99     {
100         name: PublicFavoritePanelColumnNames.FILE_SIZE,
101         selected: true,
102         configurable: true,
103         filters: createTree(),
104         render: uuid => <ResourceFileSize uuid={uuid} />
105     },
106     {
107         name: PublicFavoritePanelColumnNames.LAST_MODIFIED,
108         selected: true,
109         configurable: true,
110         sortDirection: SortDirection.DESC,
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>(loadDetailsPanel(uuid));
152     },
153     onItemDoubleClick: uuid => {
154         dispatch<any>(navigateTo(uuid));
155     }
156 });
157
158 type FavoritePanelProps = PublicFavoritePanelDataProps & PublicFavoritePanelActionProps & DispatchProp
159     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
160
161 export const PublicFavoritePanel = withStyles(styles)(
162     connect(mapStateToProps, mapDispatchToProps)(
163         class extends React.Component<FavoritePanelProps> {
164             render() {
165                 return <div className={this.props.classes.root}><DataExplorer
166                     id={PUBLIC_FAVORITE_PANEL_ID}
167                     onRowClick={this.props.onItemClick}
168                     onRowDoubleClick={this.props.onItemDoubleClick}
169                     onContextMenu={this.props.onContextMenu(this.props.resources)}
170                     contextMenuColumn={true}
171                     defaultViewIcon={PublicFavoriteIcon}
172                     defaultViewMessages={['Public favorites list is empty.']} />
173                 </div>;
174             }
175         }
176     )
177 );