Extract FileInput's parse function
[arvados-workbench2.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 { ArvadosTheme } from '~/common/custom-theme';
15 import { FAVORITE_PANEL_ID } from "~/store/favorite-panel/favorite-panel-action";
16 import {
17     ProcessStatus,
18     ResourceFileSize,
19     ResourceLastModifiedDate,
20     ResourceName,
21     ResourceOwner,
22     ResourceType
23 } from '~/views-components/data-explorer/renderers';
24 import { FavoriteIcon } from '~/components/icon/icon';
25 import { Dispatch } from 'redux';
26 import { openContextMenu, resourceKindToContextMenuKind } from '~/store/context-menu/context-menu-actions';
27 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
28 import { navigateTo } from '~/store/navigation/navigation-action';
29 import { ContainerRequestState } from "~/models/container-request";
30 import { FavoritesState } from '~/store/favorites/favorites-reducer';
31 import { RootState } from '~/store/store';
32 import { DataTableDefaultView } from '~/components/data-table-default-view/data-table-default-view';
33 import { createTree } from '~/models/tree';
34 import { getSimpleObjectTypeFilters } from '~/store/resource-type-filters/resource-type-filters';
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 FavoritePanelColumnNames {
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 favoritePanelColumns: DataColumns<string> = [
62     {
63         name: FavoritePanelColumnNames.NAME,
64         selected: true,
65         configurable: true,
66         sortDirection: SortDirection.ASC,
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: FavoritePanelColumnNames.TYPE,
79         selected: true,
80         configurable: true,
81         filters: getSimpleObjectTypeFilters(),
82         render: uuid => <ResourceType uuid={uuid} />
83     },
84     {
85         name: FavoritePanelColumnNames.OWNER,
86         selected: true,
87         configurable: true,
88         filters: createTree(),
89         render: uuid => <ResourceOwner uuid={uuid} />
90     },
91     {
92         name: FavoritePanelColumnNames.FILE_SIZE,
93         selected: true,
94         configurable: true,
95         filters: createTree(),
96         render: uuid => <ResourceFileSize uuid={uuid} />
97     },
98     {
99         name: FavoritePanelColumnNames.LAST_MODIFIED,
100         selected: true,
101         configurable: true,
102         sortDirection: SortDirection.NONE,
103         filters: createTree(),
104         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
105     }
106 ];
107
108 interface FavoritePanelDataProps {
109     favorites: FavoritesState;
110 }
111
112 interface FavoritePanelActionProps {
113     onItemClick: (item: string) => void;
114     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: string) => void;
115     onDialogOpen: (ownerUuid: string) => void;
116     onItemDoubleClick: (item: string) => void;
117 }
118 const mapStateToProps = ({ favorites }: RootState): FavoritePanelDataProps => ({
119     favorites
120 });
121
122 const mapDispatchToProps = (dispatch: Dispatch): FavoritePanelActionProps => ({
123     onContextMenu: (event, resourceUuid) => {
124         const kind = resourceKindToContextMenuKind(resourceUuid);
125         if (kind) {
126             dispatch<any>(openContextMenu(event, {
127                 name: '',
128                 uuid: resourceUuid,
129                 ownerUuid: '',
130                 kind: ResourceKind.NONE,
131                 menuKind: kind
132             }));
133         }
134         dispatch<any>(loadDetailsPanel(resourceUuid));
135     },
136     onDialogOpen: (ownerUuid: string) => { return; },
137     onItemClick: (resourceUuid: string) => {
138         dispatch<any>(loadDetailsPanel(resourceUuid));
139     },
140     onItemDoubleClick: uuid => {
141         dispatch<any>(navigateTo(uuid));
142     }
143 });
144
145 type FavoritePanelProps = FavoritePanelDataProps & FavoritePanelActionProps & DispatchProp
146     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
147
148 export const FavoritePanel = withStyles(styles)(
149     connect(mapStateToProps, mapDispatchToProps)(
150         class extends React.Component<FavoritePanelProps> {
151             render() {
152                 return <DataExplorer
153                     id={FAVORITE_PANEL_ID}
154                     onRowClick={this.props.onItemClick}
155                     onRowDoubleClick={this.props.onItemDoubleClick}
156                     onContextMenu={this.props.onContextMenu}
157                     contextMenuColumn={true}
158                     dataTableDefaultView={
159                         <DataTableDefaultView
160                             icon={FavoriteIcon}
161                             messages={['Your favorites list is empty.']}
162                             />
163                     } />;
164             }
165         }
166     )
167 );