File tree Expand file tree Collapse file tree
packages/app/src/components Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -65,20 +65,28 @@ type AppNavUserMenuProps = {
6565 onClickUserPreferences ?: ( ) => void ;
6666} ;
6767
68+ const getUserInitials = ( userName : string ) => {
69+ const nameParts = userName . trim ( ) . split ( / \s + / ) . filter ( Boolean ) ;
70+
71+ if ( nameParts . length === 0 ) {
72+ return 'U' ;
73+ }
74+
75+ return nameParts . map ( name => name . charAt ( 0 ) . toUpperCase ( ) ) . join ( '' ) ;
76+ } ;
77+
6878export const AppNavUserMenu = ( {
6979 userName = 'User' ,
7080 teamName,
7181 logoutUrl,
7282 onClickUserPreferences,
7383} : AppNavUserMenuProps ) => {
7484 const { isCollapsed } = React . useContext ( AppNavContext ) ;
85+ const resolvedUserName = userName . trim ( ) || 'User' ;
7586
76- const initials = userName
77- . split ( ' ' )
78- . map ( name => name [ 0 ] . toUpperCase ( ) )
79- . join ( '' ) ;
87+ const initials = getUserInitials ( resolvedUserName ) ;
8088
81- const displayName = IS_LOCAL_MODE ? 'Local mode' : userName ;
89+ const displayName = IS_LOCAL_MODE ? 'Local mode' : resolvedUserName ;
8290
8391 return (
8492 < Menu position = "top-start" transitionProps = { { transition : 'fade-up' } } >
Original file line number Diff line number Diff line change 1+ import React from 'react' ;
2+ import { screen } from '@testing-library/react' ;
3+
4+ import { AppNavContext , AppNavUserMenu } from '../AppNav/AppNav.components' ;
5+
6+ const renderAppNavUserMenu = ( userName ?: string ) => {
7+ return renderWithMantine (
8+ < AppNavContext . Provider value = { { isCollapsed : false , pathname : '/' } } >
9+ < AppNavUserMenu userName = { userName } teamName = "HyperDX" />
10+ </ AppNavContext . Provider > ,
11+ ) ;
12+ } ;
13+
14+ describe ( 'AppNavUserMenu' , ( ) => {
15+ it ( 'renders initials for multi-word names with extra whitespace' , ( ) => {
16+ renderAppNavUserMenu ( ' Ernest Iliiasov ' ) ;
17+
18+ expect ( screen . getByText ( 'EI' ) ) . toBeInTheDocument ( ) ;
19+ expect ( screen . getByText ( / E r n e s t \s + I l i i a s o v / ) ) . toBeInTheDocument ( ) ;
20+ } ) ;
21+
22+ it ( 'falls back to the default user label for blank names' , ( ) => {
23+ renderAppNavUserMenu ( ' ' ) ;
24+
25+ expect ( screen . getByText ( 'U' ) ) . toBeInTheDocument ( ) ;
26+ expect ( screen . getByText ( 'User' ) ) . toBeInTheDocument ( ) ;
27+ } ) ;
28+ } ) ;
You can’t perform that action at this time.
0 commit comments