16073: Add process IO panels with image preview and raw view
[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 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 const wb1URL = (route: string) => {
43     const r = route.replace(/^\//, "");
44     if (r.match(/^(projects|collections)\//)) {
45         return r;
46     } else if (r.match(/^processes\//)) {
47         return r.replace(/^processes/, "container_requests");
48     }
49     return "";
50 };
51
52 type CssRules = 'link';
53
54 const styles: StyleRulesCallback<CssRules> = () => ({
55     link: {
56         textDecoration: 'none',
57         color: 'inherit'
58     }
59 });
60
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>
74             <MenuItem>
75                 <a href={`${workbenchURL.replace(/\/$/, "")}/${wb1URL(currentRoute)}?api_token=${apiToken}`}
76                     className={classes.link}>
77                     Switch to Workbench v1</a></MenuItem>
78         </>;
79
80         const reduceItemsFn: (a: React.ReactElement[],
81             b: ElementListReducer) => React.ReactElement[] = (a, b) => b(a);
82
83         accountMenuItems = React.createElement(React.Fragment, null,
84             pluginConfig.accountMenuList.reduce(reduceItemsFn, React.Children.toArray(accountMenuItems.props.children)));
85
86         return user
87             ? <DropdownMenu
88                 icon={<UserPanelIcon />}
89                 id="account-menu"
90                 title="Account Management"
91                 key={currentRoute}>
92                 <MenuItem disabled>
93                     {getUserDisplayName(user)} {user.uuid.substring(0, 5) !== localCluster && `(${user.uuid.substring(0, 5)})`}
94                 </MenuItem>
95                 {user.isActive && accountMenuItems}
96                 <Divider />
97                 <MenuItem data-cy="logout-menuitem"
98                     onClick={() => dispatch(authActions.LOGOUT({ deleteLinkData: true }))}>
99                     Logout
100                  </MenuItem>
101             </DropdownMenu>
102             : null;
103     };
104
105 export const AccountMenu = withStyles(styles)(connect(mapStateToProps)(AccountMenuComponent));