refs #11432 Merge branch 'origin/14432-search-object-object'
[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         dispatch<any>(loadDetailsPanel(resourceUuid));
150     },
151     onDialogOpen: (ownerUuid: string) => { return; },
152     onItemClick: (resourceUuid: string) => {
153         dispatch<any>(loadDetailsPanel(resourceUuid));
154     },
155     onItemDoubleClick: uuid => {
156         dispatch<any>(navigateTo(uuid));
157     }
158 });
159
160 type FavoritePanelProps = FavoritePanelDataProps & FavoritePanelActionProps & DispatchProp
161     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
162
163 export const FavoritePanel = withStyles(styles)(
164     connect(mapStateToProps, mapDispatchToProps)(
165         class extends React.Component<FavoritePanelProps> {
166             render() {
167                 return <DataExplorer
168                     id={FAVORITE_PANEL_ID}
169                     onRowClick={this.props.onItemClick}
170                     onRowDoubleClick={this.props.onItemDoubleClick}
171                     onContextMenu={this.props.onContextMenu}
172                     contextMenuColumn={true}
173                     dataTableDefaultView={
174                         <DataTableDefaultView
175                             icon={FavoriteIcon}
176                             messages={['Your favorites list is empty.']}
177                             />
178                     } />;
179             }
180         }
181     )
182 );