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