data-explorer-routing-and-admins-context-menu-for-resources
[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, 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 { ArvadosTheme } from '~/common/custom-theme';
15 import { RestoreFromTrashIcon, TrashIcon } from '~/components/icon/icon';
16 import { TRASH_PANEL_ID } from "~/store/trash-panel/trash-panel-action";
17 import { getProperty } from "~/store/properties/properties";
18 import { PROJECT_PANEL_CURRENT_UUID } from "~/store/project-panel/project-panel-action";
19 import { openContextMenu } from "~/store/context-menu/context-menu-actions";
20 import { getResource, ResourcesState } from "~/store/resources/resources";
21 import {
22     ResourceDeleteDate,
23     ResourceFileSize,
24     ResourceName,
25     ResourceTrashDate,
26     ResourceType
27 } from "~/views-components/data-explorer/renderers";
28 import { navigateTo } from "~/store/navigation/navigation-action";
29 import { loadDetailsPanel } from "~/store/details-panel/details-panel-action";
30 import { toggleTrashed } from "~/store/trash/trash-actions";
31 import { ContextMenuKind } from "~/views-components/context-menu/context-menu";
32 import { Dispatch } from "redux";
33 import { DataTableDefaultView } from '~/components/data-table-default-view/data-table-default-view';
34 import { createTree } from '~/models/tree';
35 import {
36     getInitialResourceTypeFilters,
37     getTrashPanelTypeFilters
38 } from '~/store/resource-type-filters/resource-type-filters';
39
40 type CssRules = "toolbar" | "button";
41
42 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
43     toolbar: {
44         paddingBottom: theme.spacing.unit * 3,
45         textAlign: "right"
46     },
47     button: {
48         marginLeft: theme.spacing.unit
49     },
50 });
51
52 export enum TrashPanelColumnNames {
53     NAME = "Name",
54     TYPE = "Type",
55     FILE_SIZE = "File size",
56     TRASHED_DATE = "Trashed date",
57     TO_BE_DELETED = "To be deleted"
58 }
59
60 export interface TrashPanelFilter extends DataTableFilterItem {
61     type: ResourceKind;
62 }
63
64 export const ResourceRestore =
65     connect((state: RootState, props: { uuid: string, dispatch?: Dispatch<any> }) => {
66         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
67         return { resource, dispatch: props.dispatch };
68     })((props: { resource?: TrashableResource, dispatch?: Dispatch<any> }) =>
69         <Tooltip title="Restore">
70             <IconButton style={{ padding: '0' }} onClick={() => {
71                 if (props.resource && props.dispatch) {
72                     props.dispatch(toggleTrashed(
73                         props.resource.kind,
74                         props.resource.uuid,
75                         props.resource.ownerUuid,
76                         props.resource.isTrashed
77                     ));
78                 }}}
79             >
80                 <RestoreFromTrashIcon />
81             </IconButton>
82         </Tooltip>
83     );
84
85 export const trashPanelColumns: DataColumns<string> = [
86     {
87         name: TrashPanelColumnNames.NAME,
88         selected: true,
89         configurable: true,
90         sortDirection: SortDirection.NONE,
91         filters: createTree(),
92         render: uuid => <ResourceName uuid={uuid} />
93     },
94     {
95         name: TrashPanelColumnNames.TYPE,
96         selected: true,
97         configurable: true,
98         sortDirection: SortDirection.NONE,
99         filters: getTrashPanelTypeFilters(),
100         render: uuid => <ResourceType uuid={uuid} />,
101     },
102     {
103         name: TrashPanelColumnNames.FILE_SIZE,
104         selected: true,
105         configurable: true,
106         sortDirection: SortDirection.NONE,
107         filters: createTree(),
108         render: uuid => <ResourceFileSize uuid={uuid} />
109     },
110     {
111         name: TrashPanelColumnNames.TRASHED_DATE,
112         selected: true,
113         configurable: true,
114         sortDirection: SortDirection.DESC,
115         filters: createTree(),
116         render: uuid => <ResourceTrashDate uuid={uuid} />
117     },
118     {
119         name: TrashPanelColumnNames.TO_BE_DELETED,
120         selected: true,
121         configurable: true,
122         sortDirection: SortDirection.NONE,
123         filters: createTree(),
124         render: uuid => <ResourceDeleteDate uuid={uuid} />
125     },
126     {
127         name: '',
128         selected: true,
129         configurable: false,
130         sortDirection: SortDirection.NONE,
131         filters: createTree(),
132         render: uuid => <ResourceRestore uuid={uuid} />
133     }
134 ];
135
136 interface TrashPanelDataProps {
137     currentItemId: string;
138     resources: ResourcesState;
139 }
140
141 type TrashPanelProps = TrashPanelDataProps & DispatchProp & WithStyles<CssRules>;
142
143 export const TrashPanel = withStyles(styles)(
144     connect((state: RootState) => ({
145         currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
146         resources: state.resources
147     }))(
148         class extends React.Component<TrashPanelProps> {
149             render() {
150                 return <DataExplorer
151                     id={TRASH_PANEL_ID}
152                     onRowClick={this.handleRowClick}
153                     onRowDoubleClick={this.handleRowDoubleClick}
154                     onContextMenu={this.handleContextMenu}
155                     contextMenuColumn={false}
156                     dataTableDefaultView={
157                         <DataTableDefaultView
158                             icon={TrashIcon}
159                             messages={['Your trash list is empty.']}/>
160                     } />;
161             }
162
163             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
164                 const resource = getResource<TrashableResource>(resourceUuid)(this.props.resources);
165                 if (resource) {
166                     this.props.dispatch<any>(openContextMenu(event, {
167                         name: '',
168                         uuid: resource.uuid,
169                         ownerUuid: resource.ownerUuid,
170                         isTrashed: resource.isTrashed,
171                         kind: resource.kind,
172                         menuKind: ContextMenuKind.TRASH
173                     }));
174                 }
175                 this.props.dispatch<any>(loadDetailsPanel(resourceUuid));
176             }
177
178             handleRowDoubleClick = (uuid: string) => {
179                 this.props.dispatch<any>(navigateTo(uuid));
180             }
181
182             handleRowClick = (uuid: string) => {
183                 this.props.dispatch(loadDetailsPanel(uuid));
184             }
185         }
186     )
187 );