Merge branch 'main' of git.arvados.org:arvados into 22202-delete-process-navigation
[arvados.git] / services / workbench2 / src / views / user-panel / user-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 { Paper, Typography } from '@mui/material';
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 { DataColumns, SortDirection } from 'components/data-table/data-column';
13 import { openUserContextMenu } from "store/context-menu/context-menu-actions";
14 import { getResource, ResourcesState } from "store/resources/resources";
15 import {
16     UserResourceFullName,
17     ResourceUuid,
18     ResourceEmail,
19     ResourceIsAdmin,
20     ResourceUsername,
21     UserResourceAccountStatus,
22 } from "views-components/data-explorer/renderers";
23 import { navigateToUserProfile } from "store/navigation/navigation-action";
24 import { createTree } from 'models/tree';
25 import { compose, Dispatch } from 'redux';
26 import { UserResource } from 'models/user';
27 import { ShareMeIcon } from 'components/icon/icon';
28 import { USERS_PANEL_ID, openUserCreateDialog } from 'store/users/users-actions';
29 import { noop } from 'lodash';
30 import { CustomStyleRulesCallback } from 'common/custom-theme';
31
32 type UserPanelRules = "button" | 'root';
33
34 const styles: CustomStyleRulesCallback<UserPanelRules> = (theme) => ({
35     button: {
36         marginTop: theme.spacing(1),
37         marginRight: theme.spacing(2),
38         textAlign: 'right',
39         alignSelf: 'center'
40     },
41     root: {
42         width: '100%',
43     },
44 });
45
46 export enum UserPanelColumnNames {
47     NAME = "Name",
48     UUID = "Uuid",
49     EMAIL = "Email",
50     STATUS = "Account Status",
51     ADMIN = "Admin",
52     REDIRECT_TO_USER = "Redirect to user",
53     USERNAME = "Username"
54 }
55
56 export const userPanelColumns: DataColumns<string, UserResource> = [
57     {
58         name: UserPanelColumnNames.NAME,
59         selected: true,
60         configurable: true,
61         sort: {direction: SortDirection.NONE, field: "firstName"},
62         filters: createTree(),
63         render: uuid => <UserResourceFullName uuid={uuid} link={true} />
64     },
65     {
66         name: UserPanelColumnNames.UUID,
67         selected: true,
68         configurable: true,
69         sort: {direction: SortDirection.NONE, field: "uuid"},
70         filters: createTree(),
71         render: uuid => <ResourceUuid uuid={uuid} />
72     },
73     {
74         name: UserPanelColumnNames.EMAIL,
75         selected: true,
76         configurable: true,
77         sort: {direction: SortDirection.NONE, field: "email"},
78         filters: createTree(),
79         render: uuid => <ResourceEmail uuid={uuid} />
80     },
81     {
82         name: UserPanelColumnNames.STATUS,
83         selected: true,
84         configurable: true,
85         filters: createTree(),
86         render: uuid => <UserResourceAccountStatus uuid={uuid} />
87     },
88     {
89         name: UserPanelColumnNames.ADMIN,
90         selected: true,
91         configurable: false,
92         filters: createTree(),
93         render: uuid => <ResourceIsAdmin uuid={uuid} />
94     },
95     {
96         name: UserPanelColumnNames.USERNAME,
97         selected: true,
98         configurable: false,
99         sort: {direction: SortDirection.NONE, field: "username"},
100         filters: createTree(),
101         render: uuid => <ResourceUsername uuid={uuid} />
102     }
103 ];
104
105 interface UserPanelDataProps {
106     resources: ResourcesState;
107 }
108
109 interface UserPanelActionProps {
110     openUserCreateDialog: () => void;
111     handleRowClick: (uuid: string) => void;
112     handleContextMenu: (event, resource: UserResource) => void;
113 }
114
115 const mapStateToProps = (state: RootState) => {
116     return {
117         resources: state.resources
118     };
119 };
120
121 const mapDispatchToProps = (dispatch: Dispatch) => ({
122     openUserCreateDialog: () => dispatch<any>(openUserCreateDialog()),
123     handleRowClick: (uuid: string) => dispatch<any>(navigateToUserProfile(uuid)),
124     handleContextMenu: (event, resource: UserResource) => dispatch<any>(openUserContextMenu(event, resource)),
125 });
126
127 type UserPanelProps = UserPanelDataProps & UserPanelActionProps & DispatchProp & WithStyles<UserPanelRules>;
128
129 export const UserPanel = compose(
130     withStyles(styles),
131     connect(mapStateToProps, mapDispatchToProps))(
132         class extends React.Component<UserPanelProps> {
133             render() {
134                 return <Paper className={this.props.classes.root}>
135                     <DataExplorer
136                         id={USERS_PANEL_ID}
137                         title={
138                             <Typography>
139                            User records are created automatically on first log in.
140                            To add a new user, add them to your configured log in provider.
141                             </Typography>}
142                         onRowClick={noop}
143                         onRowDoubleClick={noop}
144                         onContextMenu={this.handleContextMenu}
145                         contextMenuColumn={true}
146                         hideColumnSelector
147                         paperProps={{
148                             elevation: 0,
149                         }}
150                         defaultViewIcon={ShareMeIcon}
151                         defaultViewMessages={['Your user list is empty.']}
152                         forceMultiSelectMode />
153                 </Paper>;
154             }
155
156             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
157                 event.stopPropagation();
158                 const resource = getResource<UserResource>(resourceUuid)(this.props.resources);
159                 if (resource) {
160                     this.props.handleContextMenu(event, resource);
161                 }
162             }
163         }
164     );