21364: changed itemsAvailable to not change on loadMore Arvados-DCO-1.1-Signed-off...
[arvados.git] / services / workbench2 / src / views-components / main-app-bar / account-menu.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 { MenuItem, Divider } from "@material-ui/core";
7 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
8 import { User, getUserDisplayName } from "models/user";
9 import { DropdownMenu } from "components/dropdown-menu/dropdown-menu";
10 import { UserPanelIcon } from "components/icon/icon";
11 import { DispatchProp, connect } from 'react-redux';
12 import { authActions, getNewExtraToken } from 'store/auth/auth-action';
13 import { RootState } from "store/store";
14 import { openTokenDialog } from 'store/token-dialog/token-dialog-actions';
15 import { openRepositoriesPanel } from "store/repositories/repositories-actions";
16 import {
17     navigateToSiteManager,
18     navigateToSshKeysUser,
19     navigateToMyAccount,
20     navigateToLinkAccount,
21 } from 'store/navigation/navigation-action';
22 import { openUserVirtualMachines } from "store/virtual-machines/virtual-machines-actions";
23 import { pluginConfig } from 'plugins';
24 import { ElementListReducer } from 'common/plugintypes';
25
26 interface AccountMenuProps {
27     user?: User;
28     currentRoute: string;
29     workbenchURL: string;
30     apiToken?: string;
31     localCluster: string;
32 }
33
34 const mapStateToProps = (state: RootState): AccountMenuProps => ({
35     user: state.auth.user,
36     currentRoute: state.router.location ? state.router.location.pathname : '',
37     workbenchURL: state.auth.config.workbenchUrl,
38     apiToken: state.auth.apiToken,
39     localCluster: state.auth.localCluster
40 });
41
42 type CssRules = 'link';
43
44 const styles: StyleRulesCallback<CssRules> = () => ({
45     link: {
46         textDecoration: 'none',
47         color: 'inherit'
48     }
49 });
50
51 export const AccountMenuComponent =
52     ({ user, dispatch, currentRoute, workbenchURL, apiToken, localCluster, classes }: AccountMenuProps & DispatchProp<any> & WithStyles<CssRules>) => {
53         let accountMenuItems = <>
54             <MenuItem onClick={() => dispatch(openUserVirtualMachines())}>Shell Access</MenuItem>
55             <MenuItem onClick={() => dispatch(openRepositoriesPanel())}>Repositories</MenuItem>
56             <MenuItem onClick={() => {
57                 dispatch<any>(getNewExtraToken(true));
58                 dispatch(openTokenDialog);
59             }}>Get API token</MenuItem>
60             <MenuItem onClick={() => dispatch(navigateToSshKeysUser)}>Ssh Keys</MenuItem>
61             <MenuItem onClick={() => dispatch(navigateToSiteManager)}>Site Manager</MenuItem>
62             <MenuItem onClick={() => dispatch(navigateToMyAccount)}>My account</MenuItem>
63             <MenuItem onClick={() => dispatch(navigateToLinkAccount)}>Link account</MenuItem>
64         </>;
65
66         const reduceItemsFn: (a: React.ReactElement[],
67             b: ElementListReducer) => React.ReactElement[] = (a, b) => b(a);
68
69         accountMenuItems = React.createElement(React.Fragment, null,
70             pluginConfig.accountMenuList.reduce(reduceItemsFn, React.Children.toArray(accountMenuItems.props.children)));
71
72         return user
73             ? <DropdownMenu
74                 icon={<UserPanelIcon />}
75                 id="account-menu"
76                 title="Account Management"
77                 key={currentRoute}>
78                 <MenuItem disabled>
79                     {getUserDisplayName(user)} {user.uuid.substring(0, 5) !== localCluster && `(${user.uuid.substring(0, 5)})`}
80                 </MenuItem>
81                 {user.isActive && accountMenuItems}
82                 <Divider />
83                 <MenuItem data-cy="logout-menuitem"
84                     onClick={() => dispatch(authActions.LOGOUT({ deleteLinkData: true, preservePath: false }))}>
85                     Logout
86                 </MenuItem>
87             </DropdownMenu>
88             : null;
89     };
90
91 export const AccountMenu = withStyles(styles)(connect(mapStateToProps)(AccountMenuComponent));