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 { Trash2Icon } from 'lucide-react' import { currentPromptAtom } from '@/containers/Providers/Jotai' import { MainViewState } from '@/constants/screens' import { useActiveModel } from '@/hooks/useActiveModel' import { useCreateConversation } from '@/hooks/useCreateConversation' import useDeleteConversation from '@/hooks/useDeleteConversation' import { useGetDownloadedModels } from '@/hooks/useGetDownloadedModels' import useGetUserConversations from '@/hooks/useGetUserConversations' import { useMainViewState } from '@/hooks/useMainViewState' import useSendChatMessage from '@/hooks/useSendChatMessage' import ChatBody from '@/screens/Chat/ChatBody' import HistoryList from '@/screens/Chat/HistoryList' import { currentConversationAtom, getActiveConvoIdAtom, userConversationsAtom, waitingToSendMessage, } from '@/helpers/atoms/Conversation.atom' import { currentConvoStateAtom } from '@/helpers/atoms/Conversation.atom' const ChatScreen = () => { const currentConvo = useAtomValue(currentConversationAtom) const { downloadedModels } = useGetDownloadedModels() const { deleteConvo } = useDeleteConversation() const { activeModel, stateModel } = useActiveModel() const { setMainViewState } = useMainViewState() const [currentPrompt, setCurrentPrompt] = useAtom(currentPromptAtom) const currentConvoState = useAtomValue(currentConvoStateAtom) const { sendChatMessage } = useSendChatMessage() 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 { getUserConversations } = useGetUserConversations() const conversations = useAtomValue(userConversationsAtom) const isEnableChat = (currentConvo && activeModel) || conversations.length > 0 useEffect(() => { getUserConversations() // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const handleMessageChange = (value: string) => { setCurrentPrompt(value) } const handleSendMessage = async () => { if (activeConversationId) { sendChatMessage() } else { setIsWaitingToSend(true) await requestCreateConvo(activeModel as Model) } } useEffect(() => { if (isWaitingToSend && activeConversationId) { setIsWaitingToSend(false) sendChatMessage() } // eslint-disable-next-line react-hooks/exhaustive-deps }, [waitingToSendMessage, activeConversationId]) const handleKeyDown = async ( event: React.KeyboardEvent ) => { if (event.key === 'Enter') { if (!event.shiftKey) { event.preventDefault() handleSendMessage() } } } console.log(currentConvo) return (
{isEnableChat && (
{currentConvo?.name ?? ''} {downloadedModels.find((x) => x.name === currentConvo?.name) || activeModel?._id === currentConvo?.modelId ? ( {!stateModel.loading && ( deleteConvo()} /> )} ) : (
)}
)} {isEnableChat ? (
) : (
{downloadedModels.length === 0 && (

{`Ups, you don't have a Model`}

{`let’s download your first model.`}

)} {!activeModel && downloadedModels.length > 0 && (

{`You don’t have any actively running models`}

{`Please start a downloaded model in My Models page to use this feature.`}

⌘e to show your model
)}
)}
handleKeyDown(e)} placeholder="Type your message ..." disabled={ !activeModel || stateModel.loading || activeModel._id !== currentConvo?.modelId } value={currentPrompt} onChange={(e) => handleMessageChange(e.target.value)} />
) } export default ChatScreen