22172: Merge branch 'main' into 22127-wb2-optimization
[arvados.git] / services / workbench2 / src / views / shared-with-me-panel / shared-with-me-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 { WithStyles } from '@mui/styles';
8 import withStyles from '@mui/styles/withStyles';
9 import { DataExplorer } from "views-components/data-explorer/data-explorer";
10 import { connect, DispatchProp } from 'react-redux';
11 import { RootState } from 'store/store';
12 import { ArvadosTheme } from 'common/custom-theme';
13 import { ShareMeIcon } from 'components/icon/icon';
14 import { ResourcesState, getResource } from 'store/resources/resources';
15 import { ResourceKind } from 'models/resource';
16 import { navigateTo } from "store/navigation/navigation-action";
17 import { loadDetailsPanel } from "store/details-panel/details-panel-action";
18 import { SHARED_WITH_ME_PANEL_ID } from 'store/shared-with-me-panel/shared-with-me-panel-actions';
19 import { openContextMenu } from 'store/context-menu/context-menu-actions';
20 import { DataTableFilterItem } from 'components/data-table-filters/data-table-filters';
21 import { GroupContentsResource } from 'services/groups-service/groups-service';
22 import { toggleOne, deselectAllOthers } from 'store/multiselect/multiselect-actions';
23 import { ContainerRequestState } from 'models/container-request';
24 import { resourceToMenuKind } from 'common/resource-to-menu-kind';
25
26
27 type CssRules = "toolbar" | "button" | "root";
28
29 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
30     toolbar: {
31         paddingBottom: theme.spacing(3),
32         textAlign: "right"
33     },
34     button: {
35         marginLeft: theme.spacing(1)
36     },
37     root: {
38         width: '100%',
39     },
40 });
41
42
43 export interface ProjectPanelFilter extends DataTableFilterItem {
44     type: ResourceKind | ContainerRequestState;
45 }
46
47
48
49 interface SharedWithMePanelDataProps {
50     resources: ResourcesState;
51     userUuid: string;
52 }
53
54 type SharedWithMePanelProps = SharedWithMePanelDataProps & DispatchProp & WithStyles<CssRules>;
55
56 export const SharedWithMePanel = withStyles(styles)(
57     connect((state: RootState) => ({
58         resources: state.resources,
59         userUuid: state.auth.user!.uuid,
60     }))(
61         class extends React.Component<SharedWithMePanelProps> {
62             render() {
63                 return <div className={this.props.classes.root}><DataExplorer
64                     id={SHARED_WITH_ME_PANEL_ID}
65                     onRowClick={this.handleRowClick}
66                     onRowDoubleClick={this.handleRowDoubleClick}
67                     onContextMenu={this.handleContextMenu}
68                     contextMenuColumn={false}
69                     defaultViewIcon={ShareMeIcon}
70                     defaultViewMessages={['No shared items']} />
71                 </div>;
72             }
73
74             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
75                 const { resources } = this.props;
76                 const resource = getResource<GroupContentsResource>(resourceUuid)(resources);
77                 const menuKind = this.props.dispatch<any>(resourceToMenuKind(resourceUuid));
78                 if (menuKind && resource) {
79                     this.props.dispatch<any>(openContextMenu(event, {
80                         name: resource.name,
81                         uuid: resource.uuid,
82                         description: resource.description,
83                         ownerUuid: resource.ownerUuid,
84                         isTrashed: ('isTrashed' in resource) ? resource.isTrashed: false,
85                         kind: resource.kind,
86                         menuKind
87                     }));
88                 }
89                 this.props.dispatch<any>(loadDetailsPanel(resourceUuid));
90             }
91
92             handleRowDoubleClick = (uuid: string) => {
93                 this.props.dispatch<any>(navigateTo(uuid));
94             }
95
96             handleRowClick = (uuid: string) => {
97                 this.props.dispatch<any>(toggleOne(uuid))
98                 this.props.dispatch<any>(deselectAllOthers(uuid))
99                 this.props.dispatch<any>(loadDetailsPanel(uuid));
100             }
101         }
102     )
103 );