X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/8882246b577a8d24a0e83dcd8905069f7b1937c8..5e805cf2209d3afe42699e4658d8a12e50bcd5a4:/src/views-components/main-app-bar/account-menu.tsx

diff --git a/src/views-components/main-app-bar/account-menu.tsx b/src/views-components/main-app-bar/account-menu.tsx
index 271cf00f..7faf27c2 100644
--- a/src/views-components/main-app-bar/account-menu.tsx
+++ b/src/views-components/main-app-bar/account-menu.tsx
@@ -2,35 +2,41 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from "react";
+import React from "react";
 import { MenuItem, Divider } from "@material-ui/core";
 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
-import { User, getUserFullname } from "~/models/user";
-import { DropdownMenu } from "~/components/dropdown-menu/dropdown-menu";
-import { Link } from "react-router-dom";
-import { UserPanelIcon } from "~/components/icon/icon";
+import { User, getUserDisplayName } from "models/user";
+import { DropdownMenu } from "components/dropdown-menu/dropdown-menu";
+import { UserPanelIcon } from "components/icon/icon";
 import { DispatchProp, connect } from 'react-redux';
-import { logout } from '~/store/auth/auth-action';
-import { RootState } from "~/store/store";
-import { openCurrentTokenDialog } from '~/store/current-token-dialog/current-token-dialog-actions';
-import { openRepositoriesPanel } from "~/store/repositories/repositories-actions";
+import { authActions, getNewExtraToken } from 'store/auth/auth-action';
+import { RootState } from "store/store";
+import { openTokenDialog } from 'store/token-dialog/token-dialog-actions';
+import { openRepositoriesPanel } from "store/repositories/repositories-actions";
 import {
     navigateToSiteManager,
     navigateToSshKeysUser,
-    navigateToMyAccount
-} from '~/store/navigation/navigation-action';
-import { openUserVirtualMachines } from "~/store/virtual-machines/virtual-machines-actions";
+    navigateToMyAccount,
+    navigateToLinkAccount
+} from 'store/navigation/navigation-action';
+import { openUserVirtualMachines } from "store/virtual-machines/virtual-machines-actions";
+import { pluginConfig } from 'plugins';
+import { ElementListReducer } from 'common/plugintypes';
 
 interface AccountMenuProps {
     user?: User;
     currentRoute: string;
     workbenchURL: string;
+    apiToken?: string;
+    localCluster: string;
 }
 
 const mapStateToProps = (state: RootState): AccountMenuProps => ({
     user: state.auth.user,
     currentRoute: state.router.location ? state.router.location.pathname : '',
-    workbenchURL: state.config.workbenchUrl,
+    workbenchURL: state.auth.config.workbenchUrl,
+    apiToken: state.auth.apiToken,
+    localCluster: state.auth.localCluster
 });
 
 const wb1URL = (route: string) => {
@@ -52,29 +58,48 @@ const styles: StyleRulesCallback<CssRules> = () => ({
     }
 });
 
-export const AccountMenu = withStyles(styles)(
-    connect(mapStateToProps)(
-        ({ user, dispatch, currentRoute, workbenchURL, classes }: AccountMenuProps & DispatchProp<any> & WithStyles<CssRules>) =>
-            user
-                ? <DropdownMenu
-                    icon={<UserPanelIcon />}
-                    id="account-menu"
-                    title="Account Management"
-                    key={currentRoute}>
-                    <MenuItem disabled>
-                        {getUserFullname(user)}
-                    </MenuItem>
-                    <MenuItem onClick={() => dispatch(openUserVirtualMachines())}>Virtual Machines</MenuItem>
-                    {!user.isAdmin && <MenuItem onClick={() => dispatch(openRepositoriesPanel())}>Repositories</MenuItem>}
-                    <MenuItem onClick={() => dispatch(openCurrentTokenDialog)}>Current token</MenuItem>
-                    <MenuItem onClick={() => dispatch(navigateToSshKeysUser)}>Ssh Keys</MenuItem>
-                    <MenuItem onClick={() => dispatch(navigateToSiteManager)}>Site Manager</MenuItem>
-                    <MenuItem onClick={() => dispatch(navigateToMyAccount)}>My account</MenuItem>
-                    <MenuItem>
-                        <a href={`${workbenchURL.replace(/\/$/, "")}/${wb1URL(currentRoute)}`}
-                            className={classes.link}>
-                            Switch to Workbench v1</a></MenuItem>
-                    <Divider />
-                    <MenuItem onClick={() => dispatch(logout())}>Logout</MenuItem>
-                </DropdownMenu>
-                : null));
+export const AccountMenuComponent =
+    ({ user, dispatch, currentRoute, workbenchURL, apiToken, localCluster, classes }: AccountMenuProps & DispatchProp<any> & WithStyles<CssRules>) => {
+        let accountMenuItems = <>
+            <MenuItem onClick={() => dispatch(openUserVirtualMachines())}>Virtual Machines</MenuItem>
+            <MenuItem onClick={() => dispatch(openRepositoriesPanel())}>Repositories</MenuItem>
+            <MenuItem onClick={() => {
+                dispatch<any>(getNewExtraToken(true));
+                dispatch(openTokenDialog);
+            }}>Get API token</MenuItem>
+            <MenuItem onClick={() => dispatch(navigateToSshKeysUser)}>Ssh Keys</MenuItem>
+            <MenuItem onClick={() => dispatch(navigateToSiteManager)}>Site Manager</MenuItem>
+            <MenuItem onClick={() => dispatch(navigateToMyAccount)}>My account</MenuItem>
+            <MenuItem onClick={() => dispatch(navigateToLinkAccount)}>Link account</MenuItem>
+            <MenuItem>
+                <a href={`${workbenchURL.replace(/\/$/, "")}/${wb1URL(currentRoute)}?api_token=${apiToken}`}
+                    className={classes.link}>
+                    Switch to Workbench v1</a></MenuItem>
+        </>;
+
+        const reduceItemsFn: (a: React.ReactElement[],
+            b: ElementListReducer) => React.ReactElement[] = (a, b) => b(a);
+
+        accountMenuItems = React.createElement(React.Fragment, null,
+            pluginConfig.accountMenuList.reduce(reduceItemsFn, React.Children.toArray(accountMenuItems.props.children)));
+
+        return user
+            ? <DropdownMenu
+                icon={<UserPanelIcon />}
+                id="account-menu"
+                title="Account Management"
+                key={currentRoute}>
+                <MenuItem disabled>
+                    {getUserDisplayName(user)} {user.uuid.substring(0, 5) !== localCluster && `(${user.uuid.substring(0, 5)})`}
+                </MenuItem>
+                {user.isActive && accountMenuItems}
+                <Divider />
+                <MenuItem data-cy="logout-menuitem"
+                    onClick={() => dispatch(authActions.LOGOUT({ deleteLinkData: true }))}>
+                    Logout
+		 </MenuItem>
+            </DropdownMenu>
+            : null;
+    };
+
+export const AccountMenu = withStyles(styles)(connect(mapStateToProps)(AccountMenuComponent));
