refactor: Moves `getUserDisplayName` outside from meteor app (#35031)
parent
a9956c04d8
commit
f89741d174
@ -0,0 +1,16 @@ |
||||
import { getUserDisplayName } from '@rocket.chat/core-typings'; |
||||
|
||||
const fakeUser = { |
||||
name: 'John Doe', |
||||
username: 'john.doe', |
||||
}; |
||||
|
||||
it('should return username if UI_Use_Real_Name setting is false', () => { |
||||
const result = getUserDisplayName(fakeUser.name, fakeUser.username, false); |
||||
expect(result).toBe(fakeUser.username); |
||||
}); |
||||
|
||||
it('should return name if UI_Use_Real_Name setting is true', () => { |
||||
const result = getUserDisplayName(fakeUser.name, fakeUser.username, true); |
||||
expect(result).toBe(fakeUser.name); |
||||
}); |
||||
@ -0,0 +1,55 @@ |
||||
import type { IMessage, MessageQuoteAttachment } from '@rocket.chat/core-typings'; |
||||
import { css } from '@rocket.chat/css-in-js'; |
||||
import { IconButton, Box, Margins } from '@rocket.chat/fuselage'; |
||||
import { useUserDisplayName } from '@rocket.chat/ui-client'; |
||||
import type { ReactElement } from 'react'; |
||||
import { memo } from 'react'; |
||||
|
||||
import { QuoteAttachment } from '../../../../components/message/content/attachments/QuoteAttachment'; |
||||
import AttachmentProvider from '../../../../providers/AttachmentProvider'; |
||||
import { useChat } from '../../contexts/ChatContext'; |
||||
|
||||
const MessageBoxReply = ({ reply }: { reply: IMessage }): ReactElement | null => { |
||||
const chat = useChat(); |
||||
|
||||
const displayName = useUserDisplayName(reply?.u); |
||||
|
||||
const closeWrapperStyle = css` |
||||
position: absolute; |
||||
right: 0.5rem; |
||||
top: 0.75rem; |
||||
`;
|
||||
|
||||
return ( |
||||
<Margins block={4}> |
||||
<Box display='flex' position='relative'> |
||||
<AttachmentProvider> |
||||
<QuoteAttachment |
||||
attachment={ |
||||
{ |
||||
text: reply.msg, |
||||
md: reply.md, |
||||
author_name: reply.alias || displayName, |
||||
author_icon: `/avatar/${reply.u.username}`, |
||||
ts: reply.ts, |
||||
attachments: reply?.attachments?.map((obj) => ({ ...obj, collapsed: true })), |
||||
collapsed: true, |
||||
} as MessageQuoteAttachment |
||||
} |
||||
/> |
||||
</AttachmentProvider> |
||||
<Box |
||||
className={closeWrapperStyle} |
||||
data-mid={reply._id} |
||||
onClick={(): void => { |
||||
chat?.composer?.dismissQuotedMessage(reply._id); |
||||
}} |
||||
> |
||||
<IconButton mini icon='cross' /> |
||||
</Box> |
||||
</Box> |
||||
</Margins> |
||||
); |
||||
}; |
||||
|
||||
export default memo(MessageBoxReply); |
||||
@ -1,4 +0,0 @@ |
||||
import type { IUser } from '@rocket.chat/core-typings'; |
||||
|
||||
export const getUserDisplayName = (name: IUser['name'], username: IUser['username'], useRealName: boolean): string | undefined => |
||||
useRealName ? name || username : username; |
||||
@ -0,0 +1,7 @@ |
||||
export * from './useFeaturePreview'; |
||||
export * from './useDefaultSettingFeaturePreviewList'; |
||||
export * from './useFeaturePreviewList'; |
||||
export * from './usePreferenceFeaturePreviewList'; |
||||
export * from './useDocumentTitle'; |
||||
export * from './useUserDisplayName'; |
||||
export * from './useValidatePassword'; |
||||
@ -0,0 +1,36 @@ |
||||
import { mockAppRoot } from '@rocket.chat/mock-providers'; |
||||
import { renderHook } from '@testing-library/react'; |
||||
|
||||
import { useUserDisplayName } from './useUserDisplayName'; |
||||
|
||||
const fakeUser = { |
||||
name: 'John Doe', |
||||
username: 'john.doe', |
||||
}; |
||||
|
||||
it('should return username if UI_Use_Real_Name setting is false', () => { |
||||
const { result } = renderHook(() => useUserDisplayName(fakeUser), { |
||||
legacyRoot: true, |
||||
wrapper: mockAppRoot().withSetting('UI_Use_Real_Name', false).build(), |
||||
}); |
||||
|
||||
expect(result.current).toBe(fakeUser.username); |
||||
}); |
||||
|
||||
it('should return name if UI_Use_Real_Name setting is true', () => { |
||||
const { result } = renderHook(() => useUserDisplayName(fakeUser), { |
||||
legacyRoot: true, |
||||
wrapper: mockAppRoot().withSetting('UI_Use_Real_Name', true).build(), |
||||
}); |
||||
|
||||
expect(result.current).toBe(fakeUser.name); |
||||
}); |
||||
|
||||
it('should return username if UI_Use_Real_Name setting is true and user has no name', () => { |
||||
const { result } = renderHook(() => useUserDisplayName({ ...fakeUser, name: undefined }), { |
||||
legacyRoot: true, |
||||
wrapper: mockAppRoot().withSetting('UI_Use_Real_Name', true).build(), |
||||
}); |
||||
|
||||
expect(result.current).toBe(fakeUser.username); |
||||
}); |
||||
@ -1,8 +1,7 @@ |
||||
import type { IUser } from '@rocket.chat/core-typings'; |
||||
import { getUserDisplayName } from '@rocket.chat/core-typings'; |
||||
import { useSetting } from '@rocket.chat/ui-contexts'; |
||||
|
||||
import { getUserDisplayName } from '../../lib/getUserDisplayName'; |
||||
|
||||
export const useUserDisplayName = ({ name, username }: Pick<IUser, 'name' | 'username'>): string | undefined => { |
||||
const useRealName = useSetting('UI_Use_Real_Name'); |
||||
|
||||
@ -1,7 +1,3 @@ |
||||
export * from './components'; |
||||
export * from './hooks/useFeaturePreview'; |
||||
export * from './hooks/useDefaultSettingFeaturePreviewList'; |
||||
export * from './hooks/useFeaturePreviewList'; |
||||
export * from './hooks/usePreferenceFeaturePreviewList'; |
||||
export * from './hooks/useDocumentTitle'; |
||||
export * from './helpers'; |
||||
export * from './hooks'; |
||||
|
||||
Loading…
Reference in new issue