Merge branch 'main' into 21720-material-ui-upgrade
[arvados.git] / services / workbench2 / src / views / groups-panel / groups-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 { connect } from 'react-redux';
7 import { CustomStyleRulesCallback } from 'common/custom-theme';
8 import { Grid, Button } from "@mui/material";
9 import { WithStyles } from '@mui/styles';
10 import withStyles from '@mui/styles/withStyles';
11 import { DataExplorer } from "views-components/data-explorer/data-explorer";
12 import { DataColumns } from 'components/data-table/data-table';
13 import { SortDirection } from 'components/data-table/data-column';
14 import { GroupMembersCount, ResourceUuid } from 'views-components/data-explorer/renderers';
15 import { AddIcon } from 'components/icon/icon';
16 import { ResourceName } from 'views-components/data-explorer/renderers';
17 import { createTree } from 'models/tree';
18 import { GROUPS_PANEL_ID, openCreateGroupDialog } from 'store/groups-panel/groups-panel-actions';
19 import { noop } from 'lodash/fp';
20 import { ContextMenuKind } from 'views-components/context-menu/menu-item-sort';
21 import { getResource, ResourcesState } from 'store/resources/resources';
22 import { GroupResource } from 'models/group';
23 import { RootState } from 'store/store';
24 import { openContextMenu } from 'store/context-menu/context-menu-actions';
25 import { ArvadosTheme } from 'common/custom-theme';
26 import { loadDetailsPanel } from 'store/details-panel/details-panel-action';
27 import { toggleOne, deselectAllOthers } from 'store/multiselect/multiselect-actions';
28
29 type CssRules = "root";
30
31 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
32     root: {
33         width: '100%',
34     }
35 });
36
37 export enum GroupsPanelColumnNames {
38     GROUP = "Name",
39     UUID = "UUID",
40     MEMBERS = "Members",
41 }
42
43 export const groupsPanelColumns: DataColumns<string, GroupResource> = [
44     {
45         name: GroupsPanelColumnNames.GROUP,
46         selected: true,
47         configurable: true,
48         sort: {direction: SortDirection.ASC, field: "name"},
49         filters: createTree(),
50         render: uuid => <ResourceName uuid={uuid} />
51     },
52     {
53         name: GroupsPanelColumnNames.UUID,
54         selected: true,
55         configurable: true,
56         filters: createTree(),
57         render: uuid => <ResourceUuid uuid={uuid} />,
58     },
59     {
60         name: GroupsPanelColumnNames.MEMBERS,
61         selected: true,
62         configurable: true,
63         filters: createTree(),
64         render: uuid => <GroupMembersCount uuid={uuid} />,
65     },
66 ];
67
68 const mapStateToProps = (state: RootState) => {
69     return {
70         resources: state.resources
71     };
72 };
73
74 const mapDispatchToProps = (dispatch: any) => {
75     return {
76         onContextMenu: (ev, resource) => dispatch(openContextMenu(ev, resource)),
77         onNewGroup: () => dispatch(openCreateGroupDialog()),
78         handleRowClick: (uuid: string) => {
79             dispatch(toggleOne(uuid))
80             dispatch(deselectAllOthers(uuid))
81             dispatch(loadDetailsPanel(uuid));
82         }
83     };
84 };
85
86 export interface GroupsPanelProps {
87     onNewGroup: () => void;
88     handleRowClick: (uuid: string) => void;
89     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: any) => void;
90     resources: ResourcesState;
91 }
92
93 export const GroupsPanel = withStyles(styles)(connect(
94     mapStateToProps, mapDispatchToProps
95 )(
96     class GroupsPanel extends React.Component<GroupsPanelProps & WithStyles<CssRules>> {
97
98         render() {
99             return (
100                 <div className={this.props.classes.root}><DataExplorer
101                     id={GROUPS_PANEL_ID}
102                     data-cy="groups-panel-data-explorer"
103                     onRowClick={this.props.handleRowClick}
104                     onRowDoubleClick={noop}
105                     onContextMenu={this.handleContextMenu}
106                     contextMenuColumn={true}
107                     hideColumnSelector
108                     actions={
109                         <Grid container justifyContent='flex-end'>
110                             <Button
111                                 data-cy="groups-panel-new-group"
112                                 variant="contained"
113                                 color="primary"
114                                 onClick={this.props.onNewGroup}>
115                                 <AddIcon /> New group
116                         </Button>
117                         </Grid>
118                     } /></div>
119             );
120         }
121
122         handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
123             const resource = getResource<GroupResource>(resourceUuid)(this.props.resources);
124             if (resource) {
125                 this.props.onContextMenu(event, {
126                     name: resource.name,
127                     uuid: resource.uuid,
128                     description: resource.description,
129                     ownerUuid: resource.ownerUuid,
130                     kind: resource.kind,
131                     menuKind: ContextMenuKind.GROUPS
132                 });
133             }
134         }
135     }));