Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3028: Show user icon after robot message in Chat #3078

Merged
10 changes: 1 addition & 9 deletions web/src/components/ChatConversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@
isVisible: boolean
}

const shouldShowIcon = (index: number, message: ChatMessageModel, messages: ChatMessageModel[]): boolean => {
const previousMessage = messages[index - 1]
return (
previousMessage?.userIsAuthor !== message.userIsAuthor ||
previousMessage.isAutomaticAnswer !== message.isAutomaticAnswer
)
}

const TypingIndicator = ({ isVisible }: TypingIndicatorProps): ReactElement | null =>
isVisible ? (
<TypingIndicatorWrapper>
Expand Down Expand Up @@ -89,7 +81,7 @@
<>
{!hasError && <InitialMessage>{t('initialMessage')}</InitialMessage>}
{messages.map((message, index) => (
<ChatMessage message={message} key={message.id} showIcon={shouldShowIcon(index, message, messages)} />
<ChatMessage message={message} key={message.id} previousMessage={messages[index - 1]} />

Check notice on line 84 in web/src/components/ChatConversation.tsx

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ No longer an issue: Complex Method

ChatConversation is no longer above the threshold for cyclomatic complexity
))}
<TypingIndicator isVisible={typingIndicatorVisible} />
<div ref={messagesEndRef} />
Expand Down
7 changes: 5 additions & 2 deletions web/src/components/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Circle = styled.div`
font-size: ${props => props.theme.fonts.decorativeFontSizeSmall};
`

type ChatMessageProps = { message: ChatMessageModel; showIcon: boolean }
type ChatMessageProps = { message: ChatMessageModel; previousMessage: ChatMessageModel | undefined }

const getIcon = (userIsAuthor: boolean, isAutomaticAnswer: boolean, t: TFunction<'chat'>): ReactElement => {
if (userIsAuthor) {
Expand All @@ -57,9 +57,12 @@ const getIcon = (userIsAuthor: boolean, isAutomaticAnswer: boolean, t: TFunction
return <Icon src={icon} title={isAutomaticAnswer ? t('bot') : t('human')} />
}

const ChatMessage = ({ message, showIcon }: ChatMessageProps): ReactElement => {
const ChatMessage = ({ message, previousMessage }: ChatMessageProps): ReactElement => {
const { t } = useTranslation('chat')
const { body, userIsAuthor, isAutomaticAnswer } = message
const hasAuthorChanged = message.userIsAuthor !== previousMessage?.userIsAuthor
const hasAutomaticAnswerChanged = message.isAutomaticAnswer !== previousMessage?.isAutomaticAnswer
const showIcon = previousMessage ? hasAuthorChanged || hasAutomaticAnswerChanged : true

return (
<Container $isAuthor={userIsAuthor}>
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/__tests__/ChatConversation.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('ChatConversation', () => {
expect(getByTestId(testMessages[0]!.id)).toBeTruthy()
expect(getByText('...')).toBeTruthy()
expect(getByTestId(testMessages[1]!.id)).toBeTruthy()
expect(getByText('chat:humanIcon')).toBeTruthy()
expect(getByText('chat:human')).toBeTruthy()
expect(getByText('...')).toBeTruthy()

act(() => jest.runAllTimers())
Expand Down
Loading