1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { IconButton, StyleRulesCallback, WithStyles, withStyles } 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 { ContextMenuResource, openContextMenu } from "~/store/context-menu/context-menu-actions";
21 import { getResource, ResourcesState } from "~/store/resources/resources";
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 { toggleCollectionTrashed, toggleProjectTrashed } from "~/store/trash/trash-actions";
32 import { ContextMenuKind } from "~/views-components/context-menu/context-menu";
33 import { Dispatch } from "redux";
35 type CssRules = "toolbar" | "button";
37 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
39 paddingBottom: theme.spacing.unit * 3,
43 marginLeft: theme.spacing.unit
47 export enum TrashPanelColumnNames {
50 FILE_SIZE = "File size",
51 TRASHED_DATE = "Trashed date",
52 TO_BE_DELETED = "To be deleted"
55 export interface TrashPanelFilter extends DataTableFilterItem {
59 export const ResourceRestore =
60 connect((state: RootState, props: { uuid: string, dispatch?: Dispatch<any> }) => {
61 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
62 return { resource, dispatch: props.dispatch };
63 })((props: { resource?: TrashableResource, dispatch?: Dispatch<any> }) =>
64 <IconButton onClick={() => {
65 if (props.resource && props.dispatch) {
66 const res = props.resource;
68 if (props.resource.kind === ResourceKind.PROJECT) {
69 props.dispatch(toggleProjectTrashed(res.uuid, res.ownerUuid, res.isTrashed));
70 } else if (props.resource.kind === ResourceKind.COLLECTION) {
71 props.dispatch(toggleCollectionTrashed(res.uuid, res.isTrashed));
75 <RestoreFromTrashIcon/>
79 export const trashPanelColumns: DataColumns<string, TrashPanelFilter> = [
81 name: TrashPanelColumnNames.NAME,
84 sortDirection: SortDirection.ASC,
86 render: uuid => <ResourceName uuid={uuid}/>,
90 name: TrashPanelColumnNames.TYPE,
93 sortDirection: SortDirection.NONE,
96 name: resourceLabel(ResourceKind.COLLECTION),
98 type: ResourceKind.COLLECTION
101 name: resourceLabel(ResourceKind.PROCESS),
103 type: ResourceKind.PROCESS
106 name: resourceLabel(ResourceKind.PROJECT),
108 type: ResourceKind.PROJECT
111 render: uuid => <ResourceType uuid={uuid}/>,
115 name: TrashPanelColumnNames.FILE_SIZE,
118 sortDirection: SortDirection.NONE,
120 render: uuid => <ResourceFileSize uuid={uuid} />,
124 name: TrashPanelColumnNames.TRASHED_DATE,
127 sortDirection: SortDirection.NONE,
129 render: uuid => <ResourceTrashDate uuid={uuid} />,
133 name: TrashPanelColumnNames.TO_BE_DELETED,
136 sortDirection: SortDirection.NONE,
138 render: uuid => <ResourceDeleteDate uuid={uuid} />,
145 sortDirection: SortDirection.NONE,
147 render: uuid => <ResourceRestore uuid={uuid}/>,
152 interface TrashPanelDataProps {
153 currentItemId: string;
154 resources: ResourcesState;
157 type TrashPanelProps = TrashPanelDataProps & DispatchProp & WithStyles<CssRules>;
159 export const TrashPanel = withStyles(styles)(
160 connect((state: RootState) => ({
161 currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
162 resources: state.resources
164 class extends React.Component<TrashPanelProps> {
168 onRowClick={this.handleRowClick}
169 onRowDoubleClick={this.handleRowDoubleClick}
170 onContextMenu={this.handleContextMenu}
171 defaultIcon={TrashIcon}
172 defaultMessages={['Your trash list is empty.']}
173 contextMenuColumn={false}/>
177 handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
178 const resource = getResource<TrashableResource>(resourceUuid)(this.props.resources);
180 this.props.dispatch<any>(openContextMenu(event, {
183 ownerUuid: resource.ownerUuid,
184 isTrashed: resource.isTrashed,
186 menuKind: ContextMenuKind.TRASH
191 handleRowDoubleClick = (uuid: string) => {
192 this.props.dispatch<any>(navigateTo(uuid));
195 handleRowClick = (uuid: string) => {
196 this.props.dispatch(loadDetailsPanel(uuid));