1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
17 navigateToSiteManager,
18 navigateToSshKeysUser,
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';
26 interface AccountMenuProps {
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
42 const wb1URL = (route: string) => {
43 const r = route.replace(/^\//, "");
44 if (r.match(/^(projects|collections)\//)) {
46 } else if (r.match(/^processes\//)) {
47 return r.replace(/^processes/, "container_requests");
52 type CssRules = 'link';
54 const styles: StyleRulesCallback<CssRules> = () => ({
56 textDecoration: 'none',
61 export const AccountMenuComponent =
62 ({ user, dispatch, currentRoute, workbenchURL, apiToken, localCluster, classes }: AccountMenuProps & DispatchProp<any> & WithStyles<CssRules>) => {
63 let accountMenuItems = <>
64 <MenuItem onClick={() => dispatch(openUserVirtualMachines())}>Virtual Machines</MenuItem>
65 <MenuItem onClick={() => dispatch(openRepositoriesPanel())}>Repositories</MenuItem>
66 <MenuItem onClick={() => {
67 dispatch<any>(getNewExtraToken(true));
68 dispatch(openTokenDialog);
69 }}>Get API token</MenuItem>
70 <MenuItem onClick={() => dispatch(navigateToSshKeysUser)}>Ssh Keys</MenuItem>
71 <MenuItem onClick={() => dispatch(navigateToSiteManager)}>Site Manager</MenuItem>
72 <MenuItem onClick={() => dispatch(navigateToMyAccount)}>My account</MenuItem>
73 <MenuItem onClick={() => dispatch(navigateToLinkAccount)}>Link account</MenuItem>
75 <a href={`${workbenchURL.replace(/\/$/, "")}/${wb1URL(currentRoute)}?api_token=${apiToken}`}
76 className={classes.link}>
77 Switch to Workbench v1</a></MenuItem>
80 const reduceItemsFn: (a: React.ReactElement[],
81 b: ElementListReducer) => React.ReactElement[] = (a, b) => b(a);
83 accountMenuItems = React.createElement(React.Fragment, null,
84 pluginConfig.accountMenuList.reduce(reduceItemsFn, React.Children.toArray(accountMenuItems.props.children)));
88 icon={<UserPanelIcon />}
90 title="Account Management"
93 {getUserDisplayName(user)} {user.uuid.substring(0, 5) !== localCluster && `(${user.uuid.substring(0, 5)})`}
95 {user.isActive && accountMenuItems}
97 <MenuItem data-cy="logout-menuitem"
98 onClick={() => dispatch(authActions.LOGOUT({ deleteLinkData: true }))}>
105 export const AccountMenu = withStyles(styles)(connect(mapStateToProps)(AccountMenuComponent));