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