Merge branch '17205-render-owner-name'
[arvados-workbench2.git] / 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 * as 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 } from '~/store/auth/auth-action';
13 import { RootState } from "~/store/store";
14 import { openCurrentTokenDialog } from '~/store/current-token-dialog/current-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
24 interface AccountMenuProps {
25     user?: User;
26     currentRoute: string;
27     workbenchURL: string;
28     apiToken?: string;
29     localCluster: string;
30 }
31
32 const mapStateToProps = (state: RootState): AccountMenuProps => ({
33     user: state.auth.user,
34     currentRoute: state.router.location ? state.router.location.pathname : '',
35     workbenchURL: state.auth.config.workbenchUrl,
36     apiToken: state.auth.apiToken,
37     localCluster: state.auth.localCluster
38 });
39
40 const wb1URL = (route: string) => {
41     const r = route.replace(/^\//, "");
42     if (r.match(/^(projects|collections)\//)) {
43         return r;
44     } else if (r.match(/^processes\//)) {
45         return r.replace(/^processes/, "container_requests");
46     }
47     return "";
48 };
49
50 type CssRules = 'link';
51
52 const styles: StyleRulesCallback<CssRules> = () => ({
53     link: {
54         textDecoration: 'none',
55         color: 'inherit'
56     }
57 });
58
59 export const AccountMenuComponent =
60     ({ user, dispatch, currentRoute, workbenchURL, apiToken, localCluster, classes }: AccountMenuProps & DispatchProp<any> & WithStyles<CssRules>) =>
61         user
62         ? <DropdownMenu
63             icon={<UserPanelIcon />}
64             id="account-menu"
65             title="Account Management"
66             key={currentRoute}>
67             <MenuItem disabled>
68                 {getUserDisplayName(user)} {user.uuid.substr(0, 5) !== localCluster && `(${user.uuid.substr(0, 5)})`}
69             </MenuItem>
70             {user.isActive ? <>
71                 <MenuItem onClick={() => dispatch(openUserVirtualMachines())}>Virtual Machines</MenuItem>
72                 {!user.isAdmin && <MenuItem onClick={() => dispatch(openRepositoriesPanel())}>Repositories</MenuItem>}
73                 <MenuItem onClick={() => dispatch(openCurrentTokenDialog)}>Current token</MenuItem>
74                 <MenuItem onClick={() => dispatch(navigateToSshKeysUser)}>Ssh Keys</MenuItem>
75                 <MenuItem onClick={() => dispatch(navigateToSiteManager)}>Site Manager</MenuItem>
76                 <MenuItem onClick={() => dispatch(navigateToMyAccount)}>My account</MenuItem>
77                 <MenuItem onClick={() => dispatch(navigateToLinkAccount)}>Link account</MenuItem>
78             </> : null}
79             <MenuItem>
80                 <a href={`${workbenchURL.replace(/\/$/, "")}/${wb1URL(currentRoute)}?api_token=${apiToken}`}
81                     className={classes.link}>
82                     Switch to Workbench v1</a></MenuItem>
83             <Divider />
84             <MenuItem data-cy="logout-menuitem"
85                 onClick={() => dispatch(authActions.LOGOUT({deleteLinkData: true}))}>
86                 Logout
87             </MenuItem>
88         </DropdownMenu>
89         : null;
90
91 export const AccountMenu = withStyles(styles)( connect(mapStateToProps)(AccountMenuComponent) );