From 41268e705c2c918df7d465b836531aecd8a91fef Mon Sep 17 00:00:00 2001 From: David Cho-Lerat Date: Mon, 15 Jul 2024 17:19:26 +0200 Subject: [PATCH] SONAR-22453 Use the new DropdownMenu from Echoes for the user menu --- .../components/nav/global/GlobalNavUser.tsx | 50 +++++++------------ .../nav/global/GlobalNavUserMenu.tsx | 49 ++++-------------- .../global/__tests__/GlobalNavUser-test.tsx | 8 +-- 3 files changed, 30 insertions(+), 77 deletions(-) diff --git a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavUser.tsx b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavUser.tsx index e9d95f3a2bf..ffee37efd99 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavUser.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavUser.tsx @@ -17,16 +17,9 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { - Avatar, - BareButton, - ButtonSecondary, - Dropdown, - PopupPlacement, - PopupZLevel, -} from 'design-system'; +import { DropdownMenu, DropdownMenuAlign, Tooltip } from '@sonarsource/echoes-react'; +import { Avatar, BareButton, ButtonSecondary } from 'design-system'; import * as React from 'react'; -import Tooltip from '../../../../components/controls/Tooltip'; import { translate } from '../../../../helpers/l10n'; import { getBaseUrl } from '../../../../helpers/system'; import { GlobalSettingKeys } from '../../../../types/settings'; @@ -60,31 +53,22 @@ export function GlobalNavUser() { const gravatarServerUrl = settings[GlobalSettingKeys.GravatarServerUrl] ?? ''; return ( - } + items={} > - {({ a11yAttrs: { role, ...a11yAttrs }, onToggleClick, open }) => ( - - - - - - )} - + + + + + + ); } diff --git a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavUserMenu.tsx b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavUserMenu.tsx index 941de1625f6..4542d126bc4 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavUserMenu.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavUserMenu.tsx @@ -17,50 +17,23 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { - ItemButton, - ItemDivider, - ItemHeader, - ItemHeaderHighlight, - ItemNavLink, -} from 'design-system'; + +import { DropdownMenu } from '@sonarsource/echoes-react'; import * as React from 'react'; -import { useNavigate } from 'react-router-dom'; import { translate } from '../../../../helpers/l10n'; -import { LoggedInUser } from '../../../../types/users'; - -interface UserAccountMenuProps { - currentUser: LoggedInUser; -} - -export function GlobalNavUserMenu({ currentUser }: UserAccountMenuProps) { - const navigateTo = useNavigate(); - const firstItemRef = React.useRef(null); - - const handleLogout = React.useCallback(() => { - navigateTo('/sessions/logout'); - }, [navigateTo]); - - React.useEffect(() => { - firstItemRef.current?.focus(); - }, [firstItemRef]); +export function GlobalNavUserMenu() { return ( <> - - {currentUser.name} - {currentUser.email != null && ( -
- {currentUser.email} -
- )} -
- - + {translate('my_account.page')} - - - {translate('layout.logout')} + + + + + + {translate('layout.logout')} + ); } diff --git a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavUser-test.tsx b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavUser-test.tsx index 2598be73adf..d6d58becb43 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavUser-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavUser-test.tsx @@ -17,6 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + import { screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import * as React from 'react'; @@ -35,12 +36,7 @@ it('should render the right interface for logged in user', async () => { renderGlobalNavUser(); await user.click(screen.getByRole('button')); - expect(screen.getAllByRole('menuitem')).toHaveLength(3); - - // This line fails with the following issue: - // Will lose the focus to the body - // Remove the comment tag after fixing the issue - // expect(screen.getByRole('menuitem', { name: 'my_account.page' })).toHaveFocus(); + expect(screen.getAllByRole('menuitem')).toHaveLength(2); }); function renderGlobalNavUser(overrides: { currentUser?: CurrentUser } = {}) { -- 2.39.5