08df05c247b61b8be75874ba87c4adbaa7834d71
[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
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                 props.dispatch(toggleTrashed(
67                     props.resource.kind,
68                     props.resource.uuid,
69                     props.resource.ownerUuid,
70                     props.resource.isTrashed
71                 ));
72             }
73         }}>
74             <RestoreFromTrashIcon/>
75         </IconButton>
76     );
77
78 export const trashPanelColumns: DataColumns<string, TrashPanelFilter> = [
79     {
80         name: TrashPanelColumnNames.NAME,
81         selected: true,
82         configurable: true,
83         sortDirection: SortDirection.ASC,
84         filters: [],
85         render: uuid => <ResourceName uuid={uuid}/>,
86         width: "450px"
87     },
88     {
89         name: TrashPanelColumnNames.TYPE,
90         selected: true,
91         configurable: true,
92         sortDirection: SortDirection.NONE,
93         filters: [
94             {
95                 name: resourceLabel(ResourceKind.COLLECTION),
96                 selected: true,
97                 type: ResourceKind.COLLECTION
98             },
99             {
100                 name: resourceLabel(ResourceKind.PROCESS),
101                 selected: true,
102                 type: ResourceKind.PROCESS
103             },
104             {
105                 name: resourceLabel(ResourceKind.PROJECT),
106                 selected: true,
107                 type: ResourceKind.PROJECT
108             }
109         ],
110         render: uuid => <ResourceType uuid={uuid}/>,
111         width: "125px"
112     },
113     {
114         name: TrashPanelColumnNames.FILE_SIZE,
115         selected: true,
116         configurable: true,
117         sortDirection: SortDirection.NONE,
118         filters: [],
119         render: uuid => <ResourceFileSize uuid={uuid} />,
120         width: "50px"
121     },
122     {
123         name: TrashPanelColumnNames.TRASHED_DATE,
124         selected: true,
125         configurable: true,
126         sortDirection: SortDirection.NONE,
127         filters: [],
128         render: uuid => <ResourceTrashDate uuid={uuid} />,
129         width: "50px"
130     },
131     {
132         name: TrashPanelColumnNames.TO_BE_DELETED,
133         selected: true,
134         configurable: true,
135         sortDirection: SortDirection.NONE,
136         filters: [],
137         render: uuid => <ResourceDeleteDate uuid={uuid} />,
138         width: "50px"
139     },
140     {
141         name: '',
142         selected: true,
143         configurable: false,
144         sortDirection: SortDirection.NONE,
145         filters: [],
146         render: uuid => <ResourceRestore uuid={uuid}/>,
147         width: "50px"
148     }
149 ];
150
151 interface TrashPanelDataProps {
152     currentItemId: string;
153     resources: ResourcesState;
154 }
155
156 type TrashPanelProps = TrashPanelDataProps & DispatchProp & WithStyles<CssRules>;
157
158 export const TrashPanel = withStyles(styles)(
159     connect((state: RootState) => ({
160         currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
161         resources: state.resources
162     }))(
163         class extends React.Component<TrashPanelProps> {
164             render() {
165                 return <DataExplorer
166                     id={TRASH_PANEL_ID}
167                     onRowClick={this.handleRowClick}
168                     onRowDoubleClick={this.handleRowDoubleClick}
169                     onContextMenu={this.handleContextMenu}
170                     defaultIcon={TrashIcon}
171                     defaultMessages={['Your trash list is empty.']}
172                     contextMenuColumn={false}/>
173                 ;
174             }
175
176             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
177                 const resource = getResource<TrashableResource>(resourceUuid)(this.props.resources);
178                 if (resource) {
179                     this.props.dispatch<any>(openContextMenu(event, {
180                         name: '',
181                         uuid: resource.uuid,
182                         ownerUuid: resource.ownerUuid,
183                         isTrashed: resource.isTrashed,
184                         kind: resource.kind,
185                         menuKind: ContextMenuKind.TRASH
186                     }));
187                 }
188             }
189
190             handleRowDoubleClick = (uuid: string) => {
191                 this.props.dispatch<any>(navigateTo(uuid));
192             }
193
194             handleRowClick = (uuid: string) => {
195                 this.props.dispatch(loadDetailsPanel(uuid));
196             }
197         }
198     )
199 );