Move default views to corresponding panels
[arvados-workbench2.git] / src / views / trash-panel / trash-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 { IconButton, 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 { RootState } from '~/store/store';
11 import { DataTableFilterItem } from '~/components/data-table-filters/data-table-filters';
12 import { SortDirection } from '~/components/data-table/data-column';
13 import { ResourceKind, TrashableResource } from '~/models/resource';
14 import { resourceLabel } from '~/common/labels';
15 import { ArvadosTheme } from '~/common/custom-theme';
16 import { RestoreFromTrashIcon, TrashIcon } from '~/components/icon/icon';
17 import { TRASH_PANEL_ID } from "~/store/trash-panel/trash-panel-action";
18 import { getProperty } from "~/store/properties/properties";
19 import { PROJECT_PANEL_CURRENT_UUID } from "~/store/project-panel/project-panel-action";
20 import { openContextMenu } from "~/store/context-menu/context-menu-actions";
21 import { getResource, ResourcesState } from "~/store/resources/resources";
22 import {
23     ResourceDeleteDate,
24     ResourceFileSize,
25     ResourceName,
26     ResourceTrashDate,
27     ResourceType
28 } from "~/views-components/data-explorer/renderers";
29 import { navigateTo } from "~/store/navigation/navigation-action";
30 import { loadDetailsPanel } from "~/store/details-panel/details-panel-action";
31 import { toggleCollectionTrashed, toggleProjectTrashed, toggleTrashed } from "~/store/trash/trash-actions";
32 import { ContextMenuKind } from "~/views-components/context-menu/context-menu";
33 import { Dispatch } from "redux";
34 import { DefaultView } from '~/components/default-view/default-view';
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 TrashPanelColumnNames {
49     NAME = "Name",
50     TYPE = "Type",
51     FILE_SIZE = "File size",
52     TRASHED_DATE = "Trashed date",
53     TO_BE_DELETED = "To be deleted"
54 }
55
56 export interface TrashPanelFilter extends DataTableFilterItem {
57     type: ResourceKind;
58 }
59
60 export const ResourceRestore =
61     connect((state: RootState, props: { uuid: string, dispatch?: Dispatch<any> }) => {
62         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
63         return { resource, dispatch: props.dispatch };
64     })((props: { resource?: TrashableResource, dispatch?: Dispatch<any> }) =>
65         <IconButton onClick={() => {
66             if (props.resource && props.dispatch) {
67                 props.dispatch(toggleTrashed(
68                     props.resource.kind,
69                     props.resource.uuid,
70                     props.resource.ownerUuid,
71                     props.resource.isTrashed
72                 ));
73             }
74         }}>
75             <RestoreFromTrashIcon />
76         </IconButton>
77     );
78
79 export const trashPanelColumns: DataColumns<string, TrashPanelFilter> = [
80     {
81         name: TrashPanelColumnNames.NAME,
82         selected: true,
83         configurable: true,
84         sortDirection: SortDirection.ASC,
85         filters: [],
86         render: uuid => <ResourceName uuid={uuid} />,
87         width: "450px"
88     },
89     {
90         name: TrashPanelColumnNames.TYPE,
91         selected: true,
92         configurable: true,
93         sortDirection: SortDirection.NONE,
94         filters: [
95             {
96                 name: resourceLabel(ResourceKind.COLLECTION),
97                 selected: true,
98                 type: ResourceKind.COLLECTION
99             },
100             {
101                 name: resourceLabel(ResourceKind.PROCESS),
102                 selected: true,
103                 type: ResourceKind.PROCESS
104             },
105             {
106                 name: resourceLabel(ResourceKind.PROJECT),
107                 selected: true,
108                 type: ResourceKind.PROJECT
109             }
110         ],
111         render: uuid => <ResourceType uuid={uuid} />,
112         width: "125px"
113     },
114     {
115         name: TrashPanelColumnNames.FILE_SIZE,
116         selected: true,
117         configurable: true,
118         sortDirection: SortDirection.NONE,
119         filters: [],
120         render: uuid => <ResourceFileSize uuid={uuid} />,
121         width: "50px"
122     },
123     {
124         name: TrashPanelColumnNames.TRASHED_DATE,
125         selected: true,
126         configurable: true,
127         sortDirection: SortDirection.NONE,
128         filters: [],
129         render: uuid => <ResourceTrashDate uuid={uuid} />,
130         width: "50px"
131     },
132     {
133         name: TrashPanelColumnNames.TO_BE_DELETED,
134         selected: true,
135         configurable: true,
136         sortDirection: SortDirection.NONE,
137         filters: [],
138         render: uuid => <ResourceDeleteDate uuid={uuid} />,
139         width: "50px"
140     },
141     {
142         name: '',
143         selected: true,
144         configurable: false,
145         sortDirection: SortDirection.NONE,
146         filters: [],
147         render: uuid => <ResourceRestore uuid={uuid} />,
148         width: "50px"
149     }
150 ];
151
152 interface TrashPanelDataProps {
153     currentItemId: string;
154     resources: ResourcesState;
155 }
156
157 type TrashPanelProps = TrashPanelDataProps & DispatchProp & WithStyles<CssRules>;
158
159 export const TrashPanel = withStyles(styles)(
160     connect((state: RootState) => ({
161         currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
162         resources: state.resources
163     }))(
164         class extends React.Component<TrashPanelProps> {
165             render() {
166                 return this.hasAnyTrashedResources()
167                     ? <DataExplorer
168                         id={TRASH_PANEL_ID}
169                         onRowClick={this.handleRowClick}
170                         onRowDoubleClick={this.handleRowDoubleClick}
171                         onContextMenu={this.handleContextMenu}
172                         contextMenuColumn={false} />
173                     : <DefaultView
174                         icon={TrashIcon}
175                         messages={['Your trash list is empty.']} />;
176             }
177
178             hasAnyTrashedResources = () => {
179                 // TODO: implement check if there is anything in the trash,
180                 //       without taking pagination into the account
181                 return true;
182             }
183
184             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
185                 const resource = getResource<TrashableResource>(resourceUuid)(this.props.resources);
186                 if (resource) {
187                     this.props.dispatch<any>(openContextMenu(event, {
188                         name: '',
189                         uuid: resource.uuid,
190                         ownerUuid: resource.ownerUuid,
191                         isTrashed: resource.isTrashed,
192                         kind: resource.kind,
193                         menuKind: ContextMenuKind.TRASH
194                     }));
195                 }
196             }
197
198             handleRowDoubleClick = (uuid: string) => {
199                 this.props.dispatch<any>(navigateTo(uuid));
200             }
201
202             handleRowClick = (uuid: string) => {
203                 this.props.dispatch(loadDetailsPanel(uuid));
204             }
205         }
206     )
207 );