diff --git a/frontend/src/components/App/TopBar.tsx b/frontend/src/components/App/TopBar.tsx index f8391bdf37..ba44097618 100644 --- a/frontend/src/components/App/TopBar.tsx +++ b/frontend/src/components/App/TopBar.tsx @@ -10,7 +10,7 @@ import { useTheme } from '@mui/material/styles'; import Toolbar from '@mui/material/Toolbar'; import useMediaQuery from '@mui/material/useMediaQuery'; import { has } from 'lodash'; -import React from 'react'; +import React, { memo, useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { useDispatch } from 'react-redux'; import { useHistory } from 'react-router-dom'; @@ -76,12 +76,23 @@ export default function TopBar({}: TopBarProps) { return !!cluster ? !!getToken(cluster) : false; } - function logout() { + const logout = useCallback(() => { if (!!cluster) { setToken(cluster, null); } history.push('/'); - } + }, [cluster]); + + const handletoggleOpen = useCallback(() => { + // For medium view we default to closed if they have not made a selection. + // This handles the case when the user resizes the window from large to small. + // If they have not made a selection then the window size stays the default for + // the size. + + const openSideBar = isMedium && isSidebarOpenUserSelected === undefined ? false : isSidebarOpen; + + dispatch(setWhetherSidebarOpen(!openSideBar)); + }, [isMedium, isSidebarOpenUserSelected, isSidebarOpen]); if (hideAppBar) { return null; @@ -94,17 +105,7 @@ export default function TopBar({}: TopBarProps) { hasToken={hasToken()} isSidebarOpen={isSidebarOpen} isSidebarOpenUserSelected={isSidebarOpenUserSelected} - onToggleOpen={() => { - // For medium view we default to closed if they have not made a selection. - // This handles the case when the user resizes the window from large to small. - // If they have not made a selection then the window size stays the default for - // the size. - - const openSideBar = - isMedium && isSidebarOpenUserSelected === undefined ? false : isSidebarOpen; - - dispatch(setWhetherSidebarOpen(!openSideBar)); - }} + onToggleOpen={handletoggleOpen} cluster={cluster || undefined} clusters={clustersConfig || undefined} /> @@ -190,264 +191,269 @@ function AppBarActions({ return <>{actions}; } -export function PureTopBar({ - appBarActions, - appBarActionsProcessors = [], - logout, - hasToken, - cluster, - clusters, - isSidebarOpen, - isSidebarOpenUserSelected, - onToggleOpen, -}: PureTopBarProps) { - const { t } = useTranslation(); - const theme = useTheme(); - const isSmall = useMediaQuery(theme.breakpoints.down('sm')); - const dispatch = useDispatch(); - const history = useHistory(); +export const PureTopBar = memo( + ({ + appBarActions, + appBarActionsProcessors = [], + logout, + hasToken, + cluster, + clusters, + isSidebarOpen, + isSidebarOpenUserSelected, + onToggleOpen, + }: PureTopBarProps) => { + const { t } = useTranslation(); + const theme = useTheme(); + const isSmall = useMediaQuery(theme.breakpoints.down('sm')); + const dispatch = useDispatch(); + const history = useHistory(); - const openSideBar = !!(isSidebarOpenUserSelected === undefined ? false : isSidebarOpen); + const openSideBar = !!(isSidebarOpenUserSelected === undefined ? false : isSidebarOpen); - const [anchorEl, setAnchorEl] = React.useState(null); - const [mobileMoreAnchorEl, setMobileMoreAnchorEl] = React.useState(null); - const isClusterContext = !!cluster; + const [anchorEl, setAnchorEl] = React.useState(null); + const [mobileMoreAnchorEl, setMobileMoreAnchorEl] = React.useState(null); + const isClusterContext = !!cluster; - const isMenuOpen = Boolean(anchorEl); - const isMobileMenuOpen = Boolean(mobileMoreAnchorEl); + const isMenuOpen = Boolean(anchorEl); + const isMobileMenuOpen = Boolean(mobileMoreAnchorEl); - const handleProfileMenuOpen = (event: React.MouseEvent) => { - setAnchorEl(event.currentTarget); - }; + const handleProfileMenuOpen = (event: React.MouseEvent) => { + setAnchorEl(event.currentTarget); + }; - const handleMobileMenuClose = () => { - setMobileMoreAnchorEl(null); - }; + const handleMobileMenuClose = () => { + setMobileMoreAnchorEl(null); + }; - const handleMenuClose = () => { - setAnchorEl(null); - handleMobileMenuClose(); - }; + const handleMenuClose = () => { + setAnchorEl(null); + handleMobileMenuClose(); + }; - const handleMobileMenuOpen = (event: React.MouseEvent) => { - setMobileMoreAnchorEl(event.currentTarget); - }; - const userMenuId = 'primary-user-menu'; + const handleMobileMenuOpen = (event: React.MouseEvent) => { + setMobileMoreAnchorEl(event.currentTarget); + }; + const userMenuId = 'primary-user-menu'; - const renderUserMenu = !!isClusterContext && ( - { - handleMenuClose(); - handleMobileMenuClose(); - }} - style={{ zIndex: 1400 }} - sx={{ - '& .MuiMenu-list': { - paddingBottom: 0, - }, - }} - > - { - logout(); + const renderUserMenu = !!isClusterContext && ( + { handleMenuClose(); + handleMobileMenuClose(); }} - disabled={!hasToken} - > - - - - - - { - history.push(createRouteURL('settings')); - handleMenuClose(); + style={{ zIndex: 1400 }} + sx={{ + '& .MuiMenu-list': { + paddingBottom: 0, + }, }} > - - - - {t('translation|General Settings')} - - { - dispatch(setVersionDialogOpen(true)); - handleMenuClose(); - }} + { + logout(); + handleMenuClose(); + }} + disabled={!hasToken} + > + + + + + + { + history.push(createRouteURL('settings')); + handleMenuClose(); + }} + > + + + + {t('translation|General Settings')} + + { + dispatch(setVersionDialogOpen(true)); + handleMenuClose(); + }} + > + + + + + {helpers.getProductName()} {helpers.getVersion()['VERSION']} + + + + ); + + const mobileMenuId = 'primary-menu-mobile'; + const allAppBarActionsMobile: AppBarAction[] = [ + { + id: DefaultAppBarAction.CLUSTER, + action: isClusterContext && ( + handleMenuClose()} /> + ), + }, + ...appBarActions, + { + id: DefaultAppBarAction.NOTIFICATION, + action: null, + }, + { + id: DefaultAppBarAction.SETTINGS, + action: ( + + + + ), + }, + { + id: DefaultAppBarAction.USER, + action: !!isClusterContext && ( + + { + handleMenuClose(); + handleProfileMenuOpen(event); + }} + size="medium" + > + + + + ), + }, + ]; + const renderMobileMenu = ( + - - - - - {helpers.getProductName()} {helpers.getVersion()['VERSION']} - - - - ); + + + ); - const mobileMenuId = 'primary-menu-mobile'; - const allAppBarActionsMobile: AppBarAction[] = [ - { - id: DefaultAppBarAction.CLUSTER, - action: isClusterContext && ( - handleMenuClose()} /> - ), - }, - ...appBarActions, - { - id: DefaultAppBarAction.NOTIFICATION, - action: null, - }, - { - id: DefaultAppBarAction.SETTINGS, - action: ( - - - - ), - }, - { - id: DefaultAppBarAction.USER, - action: !!isClusterContext && ( - + const allAppBarActions: AppBarAction[] = [ + { + id: DefaultAppBarAction.GLOBAL_SEARCH, + action: , + }, + { + id: DefaultAppBarAction.CLUSTER, + action: ( + ({ + paddingRight: theme.spacing(10), + })} + > + + + ), + }, + ...appBarActions, + { + id: DefaultAppBarAction.NOTIFICATION, + action: null, + }, + { + id: DefaultAppBarAction.SETTINGS, + action: , + }, + { + id: DefaultAppBarAction.USER, + action: !!isClusterContext && ( { - handleMenuClose(); - handleProfileMenuOpen(event); - }} size="medium" > - - ), - }, - ]; - const renderMobileMenu = ( - - - - ); - - const allAppBarActions: AppBarAction[] = [ - { - id: DefaultAppBarAction.GLOBAL_SEARCH, - action: , - }, - { - id: DefaultAppBarAction.CLUSTER, - action: ( - + ({ - paddingRight: theme.spacing(10), - })} - > - - - ), - }, - ...appBarActions, - { - id: DefaultAppBarAction.NOTIFICATION, - action: null, - }, - { - id: DefaultAppBarAction.SETTINGS, - action: , - }, - { - id: DefaultAppBarAction.USER, - action: !!isClusterContext && ( - - - - ), - }, - ]; - return ( - <> - ({ - marginLeft: drawerWidth, - zIndex: theme.zIndex.drawer + 1, - '& > *': { - color: theme.palette.text.primary, - }, - backgroundColor: theme.palette.background.default, - })} - elevation={1} - component="nav" - aria-label={t('Appbar Tools')} - enableColorOnDark - > - *': { + color: theme.palette.text.primary, }, - }} + backgroundColor: theme.palette.background.default, + })} + elevation={1} + component="nav" + aria-label={t('Appbar Tools')} + enableColorOnDark > - {isSmall ? ( - <> - - - - - - - - ) : ( - <> - - - - )} - - - {renderUserMenu} - {isSmall && renderMobileMenu} - - ); -} + + {isSmall ? ( + <> + + + + + + + + ) : ( + <> + + + + )} + + + {renderUserMenu} + {isSmall && renderMobileMenu} + + ); + } +);