File size: 2,072 Bytes
a8aec61 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
'use client'
import { useState } from 'react'
import { toast } from 'sonner'
import { TextArea } from '@/components/ui/textarea'
import { Button } from '@/components/ui/button'
import { usePlaygroundStore } from '@/store'
import useAIChatStreamHandler from '@/hooks/useAIStreamHandler'
import { useQueryState } from 'nuqs'
import Icon from '@/components/ui/icon'
const ChatInput = () => {
const { chatInputRef } = usePlaygroundStore()
const { handleStreamResponse } = useAIChatStreamHandler()
const [selectedAgent] = useQueryState('agent')
const [inputMessage, setInputMessage] = useState('')
const isStreaming = usePlaygroundStore((state) => state.isStreaming)
const handleSubmit = async () => {
if (!inputMessage.trim()) return
const currentMessage = inputMessage
setInputMessage('')
try {
await handleStreamResponse(currentMessage)
} catch (error) {
toast.error(
`Error in handleSubmit: ${
error instanceof Error ? error.message : String(error)
}`
)
}
}
return (
<div className="relative mx-auto mb-1 flex w-full max-w-2xl items-end justify-center gap-x-2 font-geist">
<TextArea
placeholder={'Ask anything'}
value={inputMessage}
onChange={(e) => setInputMessage(e.target.value)}
onKeyDown={(e) => {
if (
e.key === 'Enter' &&
!e.nativeEvent.isComposing &&
!e.shiftKey &&
!isStreaming
) {
e.preventDefault()
handleSubmit()
}
}}
className="w-full border border-accent bg-primaryAccent px-4 text-sm text-primary focus:border-accent"
disabled={!selectedAgent}
ref={chatInputRef}
/>
<Button
onClick={handleSubmit}
disabled={!selectedAgent || !inputMessage.trim() || isStreaming}
size="icon"
className="rounded-xl bg-primary p-5 text-primaryAccent"
>
<Icon type="send" color="primaryAccent" />
</Button>
</div>
)
}
export default ChatInput
|