Merge branch '14318-new-button-missing-in-several-places'
[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 * 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 { resourceLabel } from '~/common/labels';
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     ResourceOwner,
23     ResourceType
24 } from '~/views-components/data-explorer/renderers';
25 import { FavoriteIcon } from '~/components/icon/icon';
26 import { Dispatch } from 'redux';
27 import { openContextMenu, resourceKindToContextMenuKind } 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 { DataTableDefaultView } from '~/components/data-table-default-view/data-table-default-view';
34
35 type CssRules = "toolbar" | "button";
36
37 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
38     toolbar: {
39         paddingBottom: theme.spacing.unit * 3,
40         textAlign: "right"
41     },
42     button: {
43         marginLeft: theme.spacing.unit
44     },
45 });
46
47 export enum FavoritePanelColumnNames {
48     NAME = "Name",
49     STATUS = "Status",
50     TYPE = "Type",
51     OWNER = "Owner",
52     FILE_SIZE = "File size",
53     LAST_MODIFIED = "Last modified"
54 }
55
56 export interface FavoritePanelFilter extends DataTableFilterItem {
57     type: ResourceKind | ContainerRequestState;
58 }
59
60 export const favoritePanelColumns: DataColumns<string, FavoritePanelFilter> = [
61     {
62         name: FavoritePanelColumnNames.NAME,
63         selected: true,
64         configurable: true,
65         sortDirection: SortDirection.ASC,
66         filters: [],
67         render: uuid => <ResourceName uuid={uuid} />
68     },
69     {
70         name: "Status",
71         selected: true,
72         configurable: true,
73         filters: [],
74         render: uuid => <ProcessStatus uuid={uuid} />
75     },
76     {
77         name: FavoritePanelColumnNames.TYPE,
78         selected: true,
79         configurable: true,
80         filters: [
81             {
82                 name: resourceLabel(ResourceKind.COLLECTION),
83                 selected: true,
84                 type: ResourceKind.COLLECTION
85             },
86             {
87                 name: resourceLabel(ResourceKind.PROCESS),
88                 selected: true,
89                 type: ResourceKind.PROCESS
90             },
91             {
92                 name: resourceLabel(ResourceKind.PROJECT),
93                 selected: true,
94                 type: ResourceKind.PROJECT
95             }
96         ],
97         render: uuid => <ResourceType uuid={uuid} />
98     },
99     {
100         name: FavoritePanelColumnNames.OWNER,
101         selected: true,
102         configurable: true,
103         filters: [],
104         render: uuid => <ResourceOwner uuid={uuid} />
105     },
106     {
107         name: FavoritePanelColumnNames.FILE_SIZE,
108         selected: true,
109         configurable: true,
110         filters: [],
111         render: uuid => <ResourceFileSize uuid={uuid} />
112     },
113     {
114         name: FavoritePanelColumnNames.LAST_MODIFIED,
115         selected: true,
116         configurable: true,
117         sortDirection: SortDirection.NONE,
118         filters: [],
119         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
120     }
121 ];
122
123 interface FavoritePanelDataProps {
124     favorites: FavoritesState;
125 }
126
127 interface FavoritePanelActionProps {
128     onItemClick: (item: string) => void;
129     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: string) => void;
130     onDialogOpen: (ownerUuid: string) => void;
131     onItemDoubleClick: (item: string) => void;
132 }
133 const mapStateToProps = ({ favorites }: RootState): FavoritePanelDataProps => ({
134     favorites
135 });
136
137 const mapDispatchToProps = (dispatch: Dispatch): FavoritePanelActionProps => ({
138     onContextMenu: (event, resourceUuid) => {
139         const kind = resourceKindToContextMenuKind(resourceUuid);
140         if (kind) {
141             dispatch<any>(openContextMenu(event, {
142                 name: '',
143                 uuid: resourceUuid,
144                 ownerUuid: '',
145                 kind: ResourceKind.NONE,
146                 menuKind: kind
147             }));
148         }
149     },
150     onDialogOpen: (ownerUuid: string) => { return; },
151     onItemClick: (resourceUuid: string) => {
152         dispatch<any>(loadDetailsPanel(resourceUuid));
153     },
154     onItemDoubleClick: uuid => {
155         dispatch<any>(navigateTo(uuid));
156     }
157 });
158
159 type FavoritePanelProps = FavoritePanelDataProps & FavoritePanelActionProps & DispatchProp
160     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
161
162 export const FavoritePanel = withStyles(styles)(
163     connect(mapStateToProps, mapDispatchToProps)(
164         class extends React.Component<FavoritePanelProps> {
165             render() {
166                 return <DataExplorer
167                     id={FAVORITE_PANEL_ID}
168                     onRowClick={this.props.onItemClick}
169                     onRowDoubleClick={this.props.onItemDoubleClick}
170                     onContextMenu={this.props.onContextMenu}
171                     contextMenuColumn={true}
172                     dataTableDefaultView={
173                         <DataTableDefaultView
174                             icon={FavoriteIcon}
175                             messages={['Your favorites list is empty.']}
176                             />
177                     } />;
178             }
179         }
180     )
181 );