Skip to content

Commit

Permalink
Add typing indicator for automatic replies in chat
Browse files Browse the repository at this point in the history
  • Loading branch information
lunars97 committed Jan 29, 2025
1 parent 08e3ab8 commit 50de658
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions web/src/components/ChatConversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ const InitialMessage = styled.div`
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};
`

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

const TIMEOUT = 60000

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) {
Expand All @@ -41,6 +54,18 @@ const ChatConversation = ({ messages, hasError, className }: ChatConversationPro
}
}, [messages, messagesCount])

useEffect(() => {
if (!lastMessage?.userIsAuthor && lastMessage?.isAutomaticAnswer) {
setShowTypingIndicator(true)

const hideIndicatorTimer = setTimeout(() => {
setShowTypingIndicator(false)
}, TIMEOUT)

return () => clearTimeout(hideIndicatorTimer)
}
}, [lastMessage?.id, lastMessage?.isAutomaticAnswer, lastMessage?.userIsAuthor, messages.length]) // Correct dependencies

return (
<Container className={className}>
{messages.length > 0 ? (
Expand All @@ -53,6 +78,11 @@ const ChatConversation = ({ messages, hasError, className }: ChatConversationPro
showIcon={messages[index - 1]?.userIsAuthor !== message.userIsAuthor}
/>
))}
{showTypingIndicator && (
<TypingIndicator>
<strong>...</strong>
</TypingIndicator>
)}

Check warning on line 85 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

0 comments on commit 50de658

Please sign in to comment.