Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/app/src/components/AppNav/AppNav.components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,28 @@ type AppNavUserMenuProps = {
onClickUserPreferences?: () => void;
};

const getUserInitials = (userName: string) => {
const nameParts = userName.trim().split(/\s+/).filter(Boolean);

if (nameParts.length === 0) {
return 'U';
}

return nameParts.map(name => name.charAt(0).toUpperCase()).join('');
};

export const AppNavUserMenu = ({
userName = 'User',
teamName,
logoutUrl,
onClickUserPreferences,
}: AppNavUserMenuProps) => {
const { isCollapsed } = React.useContext(AppNavContext);
const resolvedUserName = userName.trim() || 'User';

const initials = userName
.split(' ')
.map(name => name[0].toUpperCase())
.join('');
const initials = getUserInitials(resolvedUserName);

const displayName = IS_LOCAL_MODE ? 'Local mode' : userName;
const displayName = IS_LOCAL_MODE ? 'Local mode' : resolvedUserName;

return (
<Menu position="top-start" transitionProps={{ transition: 'fade-up' }}>
Expand Down
28 changes: 28 additions & 0 deletions packages/app/src/components/__tests__/AppNavUserMenu.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { screen } from '@testing-library/react';

import { AppNavContext, AppNavUserMenu } from '../AppNav/AppNav.components';

const renderAppNavUserMenu = (userName?: string) => {
return renderWithMantine(
<AppNavContext.Provider value={{ isCollapsed: false, pathname: '/' }}>
<AppNavUserMenu userName={userName} teamName="HyperDX" />
</AppNavContext.Provider>,
);
};

describe('AppNavUserMenu', () => {
it('renders initials for multi-word names with extra whitespace', () => {
renderAppNavUserMenu(' Ada Lovelace ');

expect(screen.getByText('AL')).toBeInTheDocument();
expect(screen.getByText(/Ada\s+Lovelace/)).toBeInTheDocument();
});

it('falls back to the default user label for blank names', () => {
renderAppNavUserMenu(' ');

expect(screen.getByText('U')).toBeInTheDocument();
expect(screen.getByText('User')).toBeInTheDocument();
});
});
Loading