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