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