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