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

3029: Add typing indicator for automatic replies in chat #3056

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions web/src/components/ChatConversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
margin-bottom: 12px;
`

const TypingIndicator = styled.div`
text-align: center;
border-radius: 5px;
padding: 8px;
width: 20px;
border: 1px solid ${props => props.theme.colors.textDecorationColor};
font-size: ${props => props.theme.fonts.contentFontSize};
`
steffenkleinle marked this conversation as resolved.
Show resolved Hide resolved

const ErrorSendingStatus = styled.div`
background-color: ${props => props.theme.colors.invalidInput};
border-radius: 5px;
Expand All @@ -29,30 +38,52 @@
className?: string
}

const TIMEOUT = 60000
steffenkleinle marked this conversation as resolved.
Show resolved Hide resolved

const ChatConversation = ({ messages, hasError, className }: ChatConversationProps): ReactElement => {
const { t } = useTranslation('chat')
const [messagesCount, setMessagesCount] = useState(0)
const [showTypingIndicator, setShowTypingIndicator] = useState(false)
const messagesEndRef = useRef<HTMLDivElement>(null)
const lastMessage = messages[messages.length - 1]

useEffect(() => {
if (messagesCount < messages.length) {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' })
setMessagesCount(messages.length)
}
}, [messages, messagesCount])

// eslint-disable-next-line consistent-return
steffenkleinle marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
if (!lastMessage?.userIsAuthor && lastMessage?.isAutomaticAnswer) {
steffenkleinle marked this conversation as resolved.
Show resolved Hide resolved
setShowTypingIndicator(true)

const hideIndicatorTimer = setTimeout(() => {
steffenkleinle marked this conversation as resolved.
Show resolved Hide resolved
setShowTypingIndicator(false)
}, TIMEOUT)

return () => clearTimeout(hideIndicatorTimer)
}
}, [lastMessage?.id, lastMessage?.isAutomaticAnswer, lastMessage?.userIsAuthor])

return (
<Container className={className}>
{messages.length > 0 ? (
<>
{!hasError && <InitialMessage>{t('initialMessage')}</InitialMessage>}
{messages.map((message, index) => (
<ChatMessage
message={message}
key={message.id}
showIcon={messages[index - 1]?.userIsAuthor !== message.userIsAuthor}
/>
))}
{showTypingIndicator && (
<TypingIndicator>
<strong>...</strong>
</TypingIndicator>
)}

Check warning on line 86 in web/src/components/ChatConversation.tsx

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

ChatConversation has a cyclomatic complexity of 15, threshold = 10. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
<div ref={messagesEndRef} />
</>
) : (
Expand Down
Loading