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 { 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 { toggleTrashed } from "~/store/trash/trash-actions";
32 import { ContextMenuKind } from "~/views-components/context-menu/context-menu";
33 import { Dispatch } from "redux";
34 import { PanelDefaultView } from '~/components/panel-default-view/panel-default-view';
35 import { DataTableDefaultView } from '~/components/data-table-default-view/data-table-default-view';
37 type CssRules = "toolbar" | "button";
39 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
41 paddingBottom: theme.spacing.unit * 3,
45 marginLeft: theme.spacing.unit
49 export enum TrashPanelColumnNames {
52 FILE_SIZE = "File size",
53 TRASHED_DATE = "Trashed date",
54 TO_BE_DELETED = "To be deleted"
57 export interface TrashPanelFilter extends DataTableFilterItem {
61 export const ResourceRestore =
62 connect((state: RootState, props: { uuid: string, dispatch?: Dispatch<any> }) => {
63 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
64 return { resource, dispatch: props.dispatch };
65 })((props: { resource?: TrashableResource, dispatch?: Dispatch<any> }) =>
66 <IconButton onClick={() => {
67 if (props.resource && props.dispatch) {
68 props.dispatch(toggleTrashed(
71 props.resource.ownerUuid,
72 props.resource.isTrashed
76 <RestoreFromTrashIcon />
80 export const trashPanelColumns: DataColumns<string, TrashPanelFilter> = [
82 name: TrashPanelColumnNames.NAME,
85 sortDirection: SortDirection.ASC,
87 render: uuid => <ResourceName uuid={uuid} />,
91 name: TrashPanelColumnNames.TYPE,
94 sortDirection: SortDirection.NONE,
97 name: resourceLabel(ResourceKind.COLLECTION),
99 type: ResourceKind.COLLECTION
102 name: resourceLabel(ResourceKind.PROCESS),
104 type: ResourceKind.PROCESS
107 name: resourceLabel(ResourceKind.PROJECT),
109 type: ResourceKind.PROJECT
112 render: uuid => <ResourceType uuid={uuid} />,
116 name: TrashPanelColumnNames.FILE_SIZE,
119 sortDirection: SortDirection.NONE,
121 render: uuid => <ResourceFileSize uuid={uuid} />,
125 name: TrashPanelColumnNames.TRASHED_DATE,
128 sortDirection: SortDirection.NONE,
130 render: uuid => <ResourceTrashDate uuid={uuid} />,
134 name: TrashPanelColumnNames.TO_BE_DELETED,
137 sortDirection: SortDirection.NONE,
139 render: uuid => <ResourceDeleteDate uuid={uuid} />,
146 sortDirection: SortDirection.NONE,
148 render: uuid => <ResourceRestore uuid={uuid} />,
153 interface TrashPanelDataProps {
154 currentItemId: string;
155 resources: ResourcesState;
158 type TrashPanelProps = TrashPanelDataProps & DispatchProp & WithStyles<CssRules>;
160 export const TrashPanel = withStyles(styles)(
161 connect((state: RootState) => ({
162 currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
163 resources: state.resources
165 class extends React.Component<TrashPanelProps> {
167 return this.hasAnyTrashedResources()
170 onRowClick={this.handleRowClick}
171 onRowDoubleClick={this.handleRowDoubleClick}
172 onContextMenu={this.handleContextMenu}
173 contextMenuColumn={false}
174 dataTableDefaultView={<DataTableDefaultView icon={TrashIcon}/>} />
177 messages={['Your trash list is empty.']} />;
180 hasAnyTrashedResources = () => {
181 // TODO: implement check if there is anything in the trash,
182 // without taking pagination into the account
186 handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
187 const resource = getResource<TrashableResource>(resourceUuid)(this.props.resources);
189 this.props.dispatch<any>(openContextMenu(event, {
192 ownerUuid: resource.ownerUuid,
193 isTrashed: resource.isTrashed,
195 menuKind: ContextMenuKind.TRASH
200 handleRowDoubleClick = (uuid: string) => {
201 this.props.dispatch<any>(navigateTo(uuid));
204 handleRowClick = (uuid: string) => {
205 this.props.dispatch(loadDetailsPanel(uuid));