Skip to content

Commit

Permalink
feat(chat): disable message send upon error
Browse files Browse the repository at this point in the history
  • Loading branch information
baaalint committed Sep 6, 2024
1 parent 27e403c commit 7f1bcb1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions ui/src/atoms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const userWithTokenAtom = atomWithStorage<User | null>('user', localStora
export const publicUserAtom = atom<User>({});
export const usernameAtom = atom<string>('');
export const isTypingAtom = atom<boolean>(false);
export const canSendMessageAtom = atom<boolean>(true);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const selectedRowAtom = atom<any>({});
Expand Down
4 changes: 3 additions & 1 deletion ui/src/components/feature/ChatSessionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import Client from '@services/Api'
import { colors } from '@shared/theme'
import { Session } from '@shared/types/session'
import { isTypingAtom, messagesAtom, sessionIdAtom, usernameAtom } from 'atoms'
import { canSendMessageAtom, isTypingAtom, messagesAtom, sessionIdAtom, usernameAtom } from 'atoms'
import { useAtom } from 'jotai'
import { useEffect, useRef, useState } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
Expand All @@ -51,6 +51,7 @@ const ChatSessionList = (props: Props) => {
const [isEditing, setIsEditing] = useState(false)
const [description, setDescription] = useState('')
const [, fetchSessions] = useAtom(sessionsWithFetchAtom)
const [, setCanSendMessage] = useAtom(canSendMessageAtom)
const inputRef = useRef<HTMLInputElement>(null)

const { colorMode } = useColorMode()
Expand All @@ -68,6 +69,7 @@ const ChatSessionList = (props: Props) => {
props.setNew(false)
setIsTyping(false)
setMessages(session.history!)
setCanSendMessage(true)
navigate(`/chat/${session.uid}`)
}

Expand Down
29 changes: 23 additions & 6 deletions ui/src/components/shared/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@
// limitations under the License.

import { ArrowUpIcon, AttachmentIcon } from '@chakra-ui/icons'
import { Flex, IconButton, Input } from '@chakra-ui/react'
import { Flex, IconButton, Input, useToast } from '@chakra-ui/react'
import Client from '@services/Api'
import { messagesAtom, sessionIdAtom } from 'atoms'
import { canSendMessageAtom, messagesAtom, sessionIdAtom } from 'atoms'
import { useAtom } from 'jotai'
import { useState } from 'react'

const Message = () => {
const [inputValue, setInputValue] = useState('')

const [sessionId] = useAtom(sessionIdAtom)
const [, setMessages] = useAtom(messagesAtom)
const [canSendMessage, setCanSendMessage] = useAtom(canSendMessageAtom)

const toast = useToast()

const submitMessage = async () => {
setCanSendMessage(false)
setMessages(prevMessages => {
const safeMessages = Array.isArray(prevMessages) ? prevMessages : []
return [...safeMessages, { role: 'Human', content: inputValue, sources: [] }]
Expand All @@ -40,10 +45,22 @@ const Message = () => {
question: inputValue,
session_id: sessionId,
data_source: 'default'
}).then(res => {
if (res.error) {
toast({
title: 'An unexpected error occured',
description: res.error,
status: 'error',
duration: 5000,
isClosable: true
})
setCanSendMessage(false)
return res
}
setCanSendMessage(true)
return res
})

console.log('RESULT', result)

setMessages(prevMessages => {
const safeMessages = Array.isArray(prevMessages) ? prevMessages : []
return [...safeMessages.slice(0, -1), { role: 'AI', content: result.data.data.answer, sources: result.sources }]
Expand Down Expand Up @@ -71,9 +88,9 @@ const Message = () => {
placeholder="Send message..."
value={inputValue}
onChange={e => setInputValue(e.target.value)}
onKeyDown={handleKeyPress}
onKeyDown={e => canSendMessage && handleKeyPress(e)}
/>
<IconButton aria-label="Send" icon={<ArrowUpIcon />} onClick={handleClick} />
<IconButton isDisabled={!canSendMessage} aria-label="Send" icon={<ArrowUpIcon />} onClick={handleClick} />
</Flex>
)
}
Expand Down

0 comments on commit 7f1bcb1

Please sign in to comment.