15012: Uses varargs version of joinFilters().
[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 * as 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 } from '~/views-components/data-explorer/renderers';
22 import { PublicFavoriteIcon } from '~/components/icon/icon';
23 import { Dispatch } from 'redux';
24 import { openContextMenu, resourceKindToContextMenuKind } from '~/store/context-menu/context-menu-actions';
25 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
26 import { navigateTo } from '~/store/navigation/navigation-action';
27 import { ContainerRequestState } from "~/models/container-request";
28 import { RootState } from '~/store/store';
29 import { DataTableDefaultView } from '~/components/data-table-default-view/data-table-default-view';
30 import { createTree } from '~/models/tree';
31 import { getSimpleObjectTypeFilters } from '~/store/resource-type-filters/resource-type-filters';
32 import { PUBLIC_FAVORITE_PANEL_ID } from '~/store/public-favorites-panel/public-favorites-action';
33 import { PublicFavoritesState } from '~/store/public-favorites/public-favorites-reducer';
34 import { getIsAdmin } from '~/store/public-favorites/public-favorites-actions';
35
36 type CssRules = "toolbar" | "button";
37
38 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
39     toolbar: {
40         paddingBottom: theme.spacing.unit * 3,
41         textAlign: "right"
42     },
43     button: {
44         marginLeft: theme.spacing.unit
45     },
46 });
47
48 export enum PublicFavoritePanelColumnNames {
49     NAME = "Name",
50     STATUS = "Status",
51     TYPE = "Type",
52     OWNER = "Owner",
53     FILE_SIZE = "File size",
54     LAST_MODIFIED = "Last modified"
55 }
56
57 export interface FavoritePanelFilter extends DataTableFilterItem {
58     type: ResourceKind | ContainerRequestState;
59 }
60
61 export const publicFavoritePanelColumns: DataColumns<string> = [
62     {
63         name: PublicFavoritePanelColumnNames.NAME,
64         selected: true,
65         configurable: true,
66         sortDirection: SortDirection.NONE,
67         filters: createTree(),
68         render: uuid => <ResourceName uuid={uuid} />
69     },
70     {
71         name: "Status",
72         selected: true,
73         configurable: true,
74         filters: createTree(),
75         render: uuid => <ProcessStatus uuid={uuid} />
76     },
77     {
78         name: PublicFavoritePanelColumnNames.TYPE,
79         selected: true,
80         configurable: true,
81         filters: getSimpleObjectTypeFilters(),
82         render: uuid => <ResourceType uuid={uuid} />
83     },
84     {
85         name: PublicFavoritePanelColumnNames.FILE_SIZE,
86         selected: true,
87         configurable: true,
88         filters: createTree(),
89         render: uuid => <ResourceFileSize uuid={uuid} />
90     },
91     {
92         name: PublicFavoritePanelColumnNames.LAST_MODIFIED,
93         selected: true,
94         configurable: true,
95         sortDirection: SortDirection.DESC,
96         filters: createTree(),
97         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
98     }
99 ];
100
101 interface PublicFavoritePanelDataProps {
102     publicFavorites: PublicFavoritesState;
103 }
104
105 interface PublicFavoritePanelActionProps {
106     onItemClick: (item: string) => void;
107     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: string) => void;
108     onDialogOpen: (ownerUuid: string) => void;
109     onItemDoubleClick: (item: string) => void;
110 }
111 const mapStateToProps = ({ publicFavorites }: RootState): PublicFavoritePanelDataProps => ({
112     publicFavorites
113 });
114
115 const mapDispatchToProps = (dispatch: Dispatch): PublicFavoritePanelActionProps => ({
116     onContextMenu: (event, resourceUuid) => {
117         const isAdmin = dispatch<any>(getIsAdmin());
118         const kind = resourceKindToContextMenuKind(resourceUuid, isAdmin);
119         if (kind) {
120             dispatch<any>(openContextMenu(event, {
121                 name: '',
122                 uuid: resourceUuid,
123                 ownerUuid: '',
124                 kind: ResourceKind.NONE,
125                 menuKind: kind
126             }));
127         }
128         dispatch<any>(loadDetailsPanel(resourceUuid));
129     },
130     onDialogOpen: (ownerUuid: string) => { return; },
131     onItemClick: (uuid: string) => {
132         dispatch<any>(loadDetailsPanel(uuid));
133     },
134     onItemDoubleClick: uuid => {
135         dispatch<any>(navigateTo(uuid));
136     }
137 });
138
139 type FavoritePanelProps = PublicFavoritePanelDataProps & PublicFavoritePanelActionProps & DispatchProp
140     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
141
142 export const PublicFavoritePanel = withStyles(styles)(
143     connect(mapStateToProps, mapDispatchToProps)(
144         class extends React.Component<FavoritePanelProps> {
145             render() {
146                 return <DataExplorer
147                     id={PUBLIC_FAVORITE_PANEL_ID}
148                     onRowClick={this.props.onItemClick}
149                     onRowDoubleClick={this.props.onItemDoubleClick}
150                     onContextMenu={this.props.onContextMenu}
151                     contextMenuColumn={true}
152                     dataTableDefaultView={
153                         <DataTableDefaultView
154                             icon={PublicFavoriteIcon}
155                             messages={['Public favorites list is empty.']} />
156                     } />;
157             }
158         }
159     )
160 );