Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

ChatInput.tsx

74 lines2.4 KB

1import { Send } from 'lucide-react'
2import { useState, useRef, useEffect } from 'react'
3​
4interface ChatInputProps {
5 onSend: (message: string) => void
6 disabled?: boolean
7 placeholder?: string
8 exampleQueries?: string
9}
10​
11export default function ChatInput({
12 onSend,
13 disabled = false,
14 placeholder = 'Ask a question about GitHub or NPM analytics...',
15 exampleQueries = '"Show download trends for @tanstack/query" | "Find repos with unusual growth" | "Compare React state libraries"',
16}: ChatInputProps) {
17 const [input, setInput] = useState('')
18 const textareaRef = useRef<HTMLTextAreaElement>(null)
19​
20 // Auto-resize textarea
21 useEffect(() => {
22 if (textareaRef.current) {
23 textareaRef.current.style.height = 'auto'
24 textareaRef.current.style.height = `${Math.min(textareaRef.current.scrollHeight, 200)}px`
25 }
26 }, [input])
27​
28 const handleSubmit = () => {
29 if (input.trim() && !disabled) {
30 onSend(input.trim())
31 setInput('')
32 }
33 }
34​
35 const handleKeyDown = (e: React.KeyboardEvent) => {
36 if (e.key === 'Enter' && !e.shiftKey) {
37 e.preventDefault()
38 handleSubmit()
39 }
40 }
41​
42 return (
43 <div className="border-t border-gray-700 bg-gray-800 p-4">
44 <div className="max-w-4xl mx-auto">
45 <div className="flex gap-3 items-end">
46 <div className="flex-1 relative">
47 <textarea
48 ref={textareaRef}
49 value={input}
50 onChange={(e) => setInput(e.target.value)}
51 onKeyDown={handleKeyDown}
52 placeholder={placeholder}
53 disabled={disabled}
54 rows={1}
55 className="w-full px-4 py-3 bg-gray-900 border border-gray-600 rounded-lg resize-none text-gray-100 placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-cyan-500 focus:border-transparent disabled:bg-gray-800 disabled:text-gray-500 disabled:cursor-not-allowed"
56 />
57 </div>
58 <button
59 onClick={handleSubmit}
60 disabled={disabled || !input.trim()}
61 className="p-3 bg-cyan-600 text-white rounded-lg hover:bg-cyan-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
62 >
63 <Send size={20} />
64 </button>
65 </div>
66​
67 <div className="mt-2 text-xs text-gray-500">
68 <strong className="text-gray-400">Example queries:</strong>
69 <span className="ml-2">{exampleQueries}</span>
70 </div>
71 </div>
72 </div>
73 )