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