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