container uuid column up
[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 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     ResourceOwnerWithName,
22     ResourceType
23 } from 'views-components/data-explorer/renderers';
24 import { FavoriteIcon } from 'components/icon/icon';
25 import {
26     openContextMenu,
27     resourceUuidToContextMenuKind
28 } from 'store/context-menu/context-menu-actions';
29 import { loadDetailsPanel } from 'store/details-panel/details-panel-action';
30 import { navigateTo } from 'store/navigation/navigation-action';
31 import { ContainerRequestState } from "models/container-request";
32 import { FavoritesState } from 'store/favorites/favorites-reducer';
33 import { RootState } from 'store/store';
34 import { createTree } from 'models/tree';
35 import { getSimpleObjectTypeFilters } from 'store/resource-type-filters/resource-type-filters';
36 import { getResource, ResourcesState } from 'store/resources/resources';
37 import { GroupContentsResource } from 'services/groups-service/groups-service';
38 import { GroupClass, GroupResource } from 'models/group';
39 import { getProperty } from 'store/properties/properties';
40 import { PROJECT_PANEL_CURRENT_UUID } from 'store/project-panel/project-panel-action';
41 import { CollectionResource } from 'models/collection';
42
43 type CssRules = "toolbar" | "button" | "root";
44
45 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
46     toolbar: {
47         paddingBottom: theme.spacing.unit * 3,
48         textAlign: "right"
49     },
50     button: {
51         marginLeft: theme.spacing.unit
52     },
53     root: {
54         width: '100%',
55     },
56 });
57
58 export enum FavoritePanelColumnNames {
59     NAME = "Name",
60     STATUS = "Status",
61     TYPE = "Type",
62     OWNER = "Owner",
63     FILE_SIZE = "File size",
64     LAST_MODIFIED = "Last modified"
65 }
66
67 export interface FavoritePanelFilter extends DataTableFilterItem {
68     type: ResourceKind | ContainerRequestState;
69 }
70
71 export const favoritePanelColumns: DataColumns<string> = [
72     {
73         name: FavoritePanelColumnNames.NAME,
74         selected: true,
75         configurable: true,
76         sortDirection: SortDirection.NONE,
77         filters: createTree(),
78         render: uuid => <ResourceName uuid={uuid} />
79     },
80     {
81         name: "Status",
82         selected: true,
83         configurable: true,
84         filters: createTree(),
85         render: uuid => <ProcessStatus uuid={uuid} />
86     },
87     {
88         name: FavoritePanelColumnNames.TYPE,
89         selected: true,
90         configurable: true,
91         filters: getSimpleObjectTypeFilters(),
92         render: uuid => <ResourceType uuid={uuid} />
93     },
94     {
95         name: FavoritePanelColumnNames.OWNER,
96         selected: false,
97         configurable: true,
98         filters: createTree(),
99         render: uuid => <ResourceOwnerWithName uuid={uuid} />
100     },
101     {
102         name: FavoritePanelColumnNames.FILE_SIZE,
103         selected: true,
104         configurable: true,
105         filters: createTree(),
106         render: uuid => <ResourceFileSize uuid={uuid} />
107     },
108     {
109         name: FavoritePanelColumnNames.LAST_MODIFIED,
110         selected: true,
111         configurable: true,
112         sortDirection: SortDirection.DESC,
113         filters: createTree(),
114         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
115     }
116 ];
117
118 interface FavoritePanelDataProps {
119     currentItemId: any;
120     favorites: FavoritesState;
121     resources: ResourcesState;
122     userUuid: string;
123 }
124
125 interface FavoritePanelActionProps {
126     onItemClick: (item: string) => void;
127     onDialogOpen: (ownerUuid: string) => void;
128     onItemDoubleClick: (item: string) => void;
129 }
130 const mapStateToProps = (state : RootState): FavoritePanelDataProps => ({
131     favorites: state.favorites,
132     resources: state.resources,
133     userUuid: state.auth.user!.uuid,
134     currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
135 });
136
137 type FavoritePanelProps = FavoritePanelDataProps & FavoritePanelActionProps & DispatchProp
138     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
139
140 export const FavoritePanel = withStyles(styles)(
141     connect(mapStateToProps)(
142         class extends React.Component<FavoritePanelProps> {
143
144             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
145                 const { resources } = this.props;
146                 const resource = getResource<GroupContentsResource>(resourceUuid)(resources);
147
148                 let readonly = false;
149                 const project = getResource<GroupResource>(this.props.currentItemId)(resources);
150
151                 if (project && project.groupClass === GroupClass.FILTER) {
152                     readonly = true;
153                 }
154
155                 const menuKind = this.props.dispatch<any>(resourceUuidToContextMenuKind(resourceUuid, readonly));
156
157                 if (menuKind && resource) {
158                     this.props.dispatch<any>(openContextMenu(event, {
159                         name: resource.name,
160                         uuid: resource.uuid,
161                         ownerUuid: resource.ownerUuid,
162                         isTrashed: ('isTrashed' in resource) ? resource.isTrashed: false,
163                         kind: resource.kind,
164                         menuKind,
165                         description: resource.description,
166                         storageClassesDesired: (resource as CollectionResource).storageClassesDesired,
167                     }));
168                 }
169                 this.props.dispatch<any>(loadDetailsPanel(resourceUuid));
170             }
171
172             handleRowDoubleClick = (uuid: string) => {
173                 this.props.dispatch<any>(navigateTo(uuid));
174             }
175
176             handleRowClick = (uuid: string) => {
177                 this.props.dispatch<any>(loadDetailsPanel(uuid));
178             }
179
180             render() {
181                 return <div className={this.props.classes.root}><DataExplorer
182                     id={FAVORITE_PANEL_ID}
183                     onRowClick={this.handleRowClick}
184                     onRowDoubleClick={this.handleRowDoubleClick}
185                     onContextMenu={this.handleContextMenu}
186                     contextMenuColumn={true}
187                     defaultViewIcon={FavoriteIcon}
188                     defaultViewMessages={['Your favorites list is empty.']} />
189                 </div>;
190             }
191         }
192     )
193 );