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