14841: Pass token when switching to workbench v1
[arvados.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, getUserFullname } from "~/models/user";
9 import { DropdownMenu } from "~/components/dropdown-menu/dropdown-menu";
10 import { Link } from "react-router-dom";
11 import { UserPanelIcon } from "~/components/icon/icon";
12 import { DispatchProp, connect } from 'react-redux';
13 import { logout } from '~/store/auth/auth-action';
14 import { RootState } from "~/store/store";
15 import { openCurrentTokenDialog } from '~/store/current-token-dialog/current-token-dialog-actions';
16 import { openRepositoriesPanel } from "~/store/repositories/repositories-actions";
17 import {
18     navigateToSiteManager,
19     navigateToSshKeysUser,
20     navigateToMyAccount
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 }
30
31 const mapStateToProps = (state: RootState): AccountMenuProps => ({
32     user: state.auth.user,
33     currentRoute: state.router.location ? state.router.location.pathname : '',
34     workbenchURL: state.config.workbenchUrl,
35     apiToken: state.auth.apiToken
36 });
37
38 const wb1URL = (route: string) => {
39     const r = route.replace(/^\//, "");
40     if (r.match(/^(projects|collections)\//)) {
41         return r;
42     } else if (r.match(/^processes\//)) {
43         return r.replace(/^processes/, "container_requests");
44     }
45     return "";
46 };
47
48 type CssRules = 'link';
49
50 const styles: StyleRulesCallback<CssRules> = () => ({
51     link: {
52         textDecoration: 'none',
53         color: 'inherit'
54     }
55 });
56
57 export const AccountMenu = withStyles(styles)(
58     connect(mapStateToProps)(
59         ({ user, dispatch, currentRoute, workbenchURL, apiToken, classes }: AccountMenuProps & DispatchProp<any> & WithStyles<CssRules>) =>
60             user
61                 ? <DropdownMenu
62                     icon={<UserPanelIcon />}
63                     id="account-menu"
64                     title="Account Management"
65                     key={currentRoute}>
66                     <MenuItem disabled>
67                         {getUserFullname(user)}
68                     </MenuItem>
69                     <MenuItem onClick={() => dispatch(openUserVirtualMachines())}>Virtual Machines</MenuItem>
70                     {!user.isAdmin && <MenuItem onClick={() => dispatch(openRepositoriesPanel())}>Repositories</MenuItem>}
71                     <MenuItem onClick={() => dispatch(openCurrentTokenDialog)}>Current token</MenuItem>
72                     <MenuItem onClick={() => dispatch(navigateToSshKeysUser)}>Ssh Keys</MenuItem>
73                     <MenuItem onClick={() => dispatch(navigateToSiteManager)}>Site Manager</MenuItem>
74                     <MenuItem onClick={() => dispatch(navigateToMyAccount)}>My account</MenuItem>
75                     <MenuItem>
76                         <a href={`${workbenchURL.replace(/\/$/, "")}/${wb1URL(currentRoute)}?api_token=${apiToken}`}
77                             className={classes.link}>
78                             Switch to Workbench v1</a></MenuItem>
79                     <Divider />
80                     <MenuItem onClick={() => dispatch(logout())}>Logout</MenuItem>
81                 </DropdownMenu>
82                 : null));