import { Fragment, useEffect, useRef, useState } from 'react' import { Button, Badge, Textarea } from '@janhq/uikit' import { useAtom, useAtomValue } from 'jotai' import { Trash2Icon, Paintbrush } from 'lucide-react' import { twMerge } from 'tailwind-merge' import { currentPromptAtom } from '@/containers/Providers/Jotai' import ShortCut from '@/containers/Shortcut' import { MainViewState } from '@/constants/screens' import { useActiveModel } from '@/hooks/useActiveModel' import useDeleteThread from '@/hooks/useDeleteConversation' import { useGetDownloadedModels } from '@/hooks/useGetDownloadedModels' import { useMainViewState } from '@/hooks/useMainViewState' import useSendChatMessage from '@/hooks/useSendChatMessage' import ChatBody from '@/screens/Chat/ChatBody' import ThreadList from '@/screens/Chat/ThreadList' import Sidebar from './Sidebar' import { activeThreadAtom, getActiveThreadIdAtom, threadsAtom, waitingToSendMessage, } from '@/helpers/atoms/Conversation.atom' import { activeThreadStateAtom } from '@/helpers/atoms/Conversation.atom' const ChatScreen = () => { const currentConvo = useAtomValue(activeThreadAtom) const { downloadedModels } = useGetDownloadedModels() const { deleteThread, cleanThread } = useDeleteThread() const { activeModel, stateModel } = useActiveModel() const { setMainViewState } = useMainViewState() const [currentPrompt, setCurrentPrompt] = useAtom(currentPromptAtom) const currentConvoState = useAtomValue(activeThreadStateAtom) const { sendChatMessage } = useSendChatMessage() const isWaitingForResponse = currentConvoState?.waitingForResponse ?? false const disabled = currentPrompt.trim().length === 0 || isWaitingForResponse const activeThreadId = useAtomValue(getActiveThreadIdAtom) const [isWaitingToSend, setIsWaitingToSend] = useAtom(waitingToSendMessage) const conversations = useAtomValue(threadsAtom) const isEnableChat = (currentConvo && activeModel) || conversations.length > 0 const [isModelAvailable, setIsModelAvailable] = useState( true // downloadedModels.some((x) => x.id === currentConvo?.modelId) ) const textareaRef = useRef(null) const modelRef = useRef(activeModel) useEffect(() => { modelRef.current = activeModel }, [activeModel]) const onPromptChange = (e: React.ChangeEvent) => { setCurrentPrompt(e.target.value) } useEffect(() => { if (isWaitingToSend && activeThreadId) { setIsWaitingToSend(false) sendChatMessage() } // eslint-disable-next-line react-hooks/exhaustive-deps }, [waitingToSendMessage, activeThreadId]) useEffect(() => { if (textareaRef.current !== null) { const scrollHeight = textareaRef.current.scrollHeight if (currentPrompt.length === 0) { textareaRef.current.style.height = '40px' } else { textareaRef.current.style.height = `${ scrollHeight < 40 ? 40 : scrollHeight }px` } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [currentPrompt]) const onKeyDown = async (e: React.KeyboardEvent) => { if (e.key === 'Enter') { if (!e.shiftKey) { e.preventDefault() sendChatMessage() } } } return (
{isEnableChat && currentConvo && (
{currentConvo.title}
{!isModelAvailable && ( )} cleanThread()} /> deleteThread()} />
)} {isEnableChat ? (
) : (
{downloadedModels.length === 0 && (

{`Oops, 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 to use this feature.`}

  to show your model
)}
)}