From 7e7f5e0dc4a5778bbc43e3cc5b5196ee23a73cc9 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 7 Nov 2023 22:23:43 +0700 Subject: [PATCH] chore: fixed an issue where app does not yield message result (#561) --- web/helpers/atoms/Conversation.atom.ts | 6 +----- web/screens/Chat/index.tsx | 14 +++++++++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/web/helpers/atoms/Conversation.atom.ts b/web/helpers/atoms/Conversation.atom.ts index 81f8d7b071..f15b60581c 100644 --- a/web/helpers/atoms/Conversation.atom.ts +++ b/web/helpers/atoms/Conversation.atom.ts @@ -12,15 +12,11 @@ export const getActiveConvoIdAtom = atom((get) => get(activeConversationIdAtom)) export const setActiveConvoIdAtom = atom( null, (_get, set, convoId: string | undefined) => { - // if (convoId) { - // console.debug(`Set active conversation id: ${convoId}`) - // set(setMainViewStateAtom, MainViewState.Chat) - // } - set(activeConversationIdAtom, convoId) } ) +export const waitingToSendMessage = atom(undefined) /** * Stores all conversation states for the current user */ diff --git a/web/screens/Chat/index.tsx b/web/screens/Chat/index.tsx index 02235e41cc..4b5dc3094b 100644 --- a/web/screens/Chat/index.tsx +++ b/web/screens/Chat/index.tsx @@ -1,9 +1,9 @@ -import { Fragment } from 'react' +import { Fragment, useEffect } from 'react' import { Model } from '@janhq/core/lib/types' import { ScrollArea, Input, Button, Badge } from '@janhq/uikit' -import { useAtom, useAtomValue } from 'jotai' +import { useAtom, useAtomValue, useSetAtom } from 'jotai' import { Trash2Icon } from 'lucide-react' import { currentPromptAtom } from '@/containers/Providers/Jotai' @@ -28,6 +28,7 @@ import HistoryList from '@/screens/Chat/HistoryList' import { currentConversationAtom, getActiveConvoIdAtom, + waitingToSendMessage, } from '@/helpers/atoms/Conversation.atom' import { currentConvoStateAtom } from '@/helpers/atoms/Conversation.atom' @@ -46,6 +47,7 @@ const ChatScreen = () => { const isWaitingForResponse = currentConvoState?.waitingForResponse ?? false const disabled = currentPrompt.trim().length === 0 || isWaitingForResponse const activeConversationId = useAtomValue(getActiveConvoIdAtom) + const [isWaitingToSend, setIsWaitingToSend] = useAtom(waitingToSendMessage) const { requestCreateConvo } = useCreateConversation() const handleMessageChange = (value: string) => { @@ -56,10 +58,16 @@ const ChatScreen = () => { if (activeConversationId) { sendChatMessage() } else { + setIsWaitingToSend(true) await requestCreateConvo(activeModel as Model) - sendChatMessage() } } + useEffect(() => { + if (isWaitingToSend && activeConversationId) { + setIsWaitingToSend(false) + sendChatMessage() + } + }, [waitingToSendMessage, activeConversationId]) const handleKeyDown = async ( event: React.KeyboardEvent