Merge branch 'master'
[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 { FavoritePanelItem } from './favorite-panel-item';
7 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
8 import { DataExplorer } from "~/views-components/data-explorer/data-explorer";
9 import { DispatchProp, connect } from 'react-redux';
10 import { DataColumns } from '~/components/data-table/data-table';
11 import { RouteComponentProps } from 'react-router';
12 import { RootState } from '~/store/store';
13 import { DataTableFilterItem } from '~/components/data-table-filters/data-table-filters';
14 import { ContainerRequestState } from '~/models/container-request';
15 import { SortDirection } from '~/components/data-table/data-column';
16 import { ResourceKind } from '~/models/resource';
17 import { resourceLabel } from '~/common/labels';
18 import { ArvadosTheme } from '~/common/custom-theme';
19 import { renderName, renderStatus, renderType, renderOwner, renderFileSize, renderDate } from '~/views-components/data-explorer/renderers';
20 import { FAVORITE_PANEL_ID } from "~/store/favorite-panel/favorite-panel-action";
21 import { FavoriteIcon } from '~/components/icon/icon';
22
23 type CssRules = "toolbar" | "button";
24
25 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
26     toolbar: {
27         paddingBottom: theme.spacing.unit * 3,
28         textAlign: "right"
29     },
30     button: {
31         marginLeft: theme.spacing.unit
32     },
33 });
34
35 export enum FavoritePanelColumnNames {
36     NAME = "Name",
37     STATUS = "Status",
38     TYPE = "Type",
39     OWNER = "Owner",
40     FILE_SIZE = "File size",
41     LAST_MODIFIED = "Last modified"
42 }
43
44 export interface FavoritePanelFilter extends DataTableFilterItem {
45     type: ResourceKind | ContainerRequestState;
46 }
47
48 export const columns: DataColumns<FavoritePanelItem, FavoritePanelFilter> = [
49     {
50         name: FavoritePanelColumnNames.NAME,
51         selected: true,
52         configurable: true,
53         sortDirection: SortDirection.ASC,
54         filters: [],
55         render: renderName,
56         width: "450px"
57     },
58     {
59         name: "Status",
60         selected: true,
61         configurable: true,
62         sortDirection: SortDirection.NONE,
63         filters: [
64             {
65                 name: ContainerRequestState.COMMITTED,
66                 selected: true,
67                 type: ContainerRequestState.COMMITTED
68             },
69             {
70                 name: ContainerRequestState.FINAL,
71                 selected: true,
72                 type: ContainerRequestState.FINAL
73             },
74             {
75                 name: ContainerRequestState.UNCOMMITTED,
76                 selected: true,
77                 type: ContainerRequestState.UNCOMMITTED
78             }
79         ],
80         render: renderStatus,
81         width: "75px"
82     },
83     {
84         name: FavoritePanelColumnNames.TYPE,
85         selected: true,
86         configurable: true,
87         sortDirection: SortDirection.NONE,
88         filters: [
89             {
90                 name: resourceLabel(ResourceKind.COLLECTION),
91                 selected: true,
92                 type: ResourceKind.COLLECTION
93             },
94             {
95                 name: resourceLabel(ResourceKind.PROCESS),
96                 selected: true,
97                 type: ResourceKind.PROCESS
98             },
99             {
100                 name: resourceLabel(ResourceKind.PROJECT),
101                 selected: true,
102                 type: ResourceKind.PROJECT
103             }
104         ],
105         render: item => renderType(item.kind),
106         width: "125px"
107     },
108     {
109         name: FavoritePanelColumnNames.OWNER,
110         selected: true,
111         configurable: true,
112         sortDirection: SortDirection.NONE,
113         filters: [],
114         render: item => renderOwner(item.owner),
115         width: "200px"
116     },
117     {
118         name: FavoritePanelColumnNames.FILE_SIZE,
119         selected: true,
120         configurable: true,
121         sortDirection: SortDirection.NONE,
122         filters: [],
123         render: item => renderFileSize(item.fileSize),
124         width: "50px"
125     },
126     {
127         name: FavoritePanelColumnNames.LAST_MODIFIED,
128         selected: true,
129         configurable: true,
130         sortDirection: SortDirection.NONE,
131         filters: [],
132         render: item => renderDate(item.lastModified),
133         width: "150px"
134     }
135 ];
136
137 interface FavoritePanelDataProps {
138     currentItemId: string;
139 }
140
141 interface FavoritePanelActionProps {
142     onItemClick: (item: FavoritePanelItem) => void;
143     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: FavoritePanelItem) => void;
144     onDialogOpen: (ownerUuid: string) => void;
145     onItemDoubleClick: (item: FavoritePanelItem) => void;
146     onItemRouteChange: (itemId: string) => void;
147 }
148
149 type FavoritePanelProps = FavoritePanelDataProps & FavoritePanelActionProps & DispatchProp
150                         & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
151
152 export const FavoritePanel = withStyles(styles)(
153     connect((state: RootState) => ({ currentItemId: state.projects.currentItemId }))(
154         class extends React.Component<FavoritePanelProps> {
155             render() {
156                 return <DataExplorer
157                     id={FAVORITE_PANEL_ID}
158                     columns={columns}
159                     onRowClick={this.props.onItemClick}
160                     onRowDoubleClick={this.props.onItemDoubleClick}
161                     onContextMenu={this.props.onContextMenu}
162                     extractKey={(item: FavoritePanelItem) => item.uuid}
163                     defaultIcon={FavoriteIcon}
164                     defaultMessages={['Your favorites list is empty.']}/>
165                 ;
166             }
167
168             componentWillReceiveProps({ match, currentItemId, onItemRouteChange }: FavoritePanelProps) {
169                 if (match.params.id !== currentItemId) {
170                     onItemRouteChange(match.params.id);
171                 }
172             }
173         }
174     )
175 );