"use client" import React, { useCallback, useRef, useEffect } from "react" import { Button } from "@/components/ui/button" import { Textarea } from "@/components/ui/textarea" import { Loader2, Send, Trash, Image as ImageIcon, X } from "lucide-react" import Image from "next/image" interface ChatInputProps { input: string status: "submitted" | "streaming" | "ready" | "error" onSubmit: (e: React.FormEvent) => void onChange: (e: React.ChangeEvent) => void setMessages: (messages: any[]) => void files?: FileList onFileChange?: (files: FileList | undefined) => void } export function ChatInput({ input, status, onSubmit, onChange, setMessages, files, onFileChange }: ChatInputProps) { const textareaRef = useRef(null) const fileInputRef = useRef(null) // Auto-resize textarea based on content const adjustTextareaHeight = useCallback(() => { const textarea = textareaRef.current if (textarea) { textarea.style.height = "auto" textarea.style.height = `${Math.min(textarea.scrollHeight, 200)}px` } }, []) useEffect(() => { adjustTextareaHeight() }, [input, adjustTextareaHeight]) // Handle keyboard shortcuts const handleKeyDown = (e: React.KeyboardEvent) => { if ((e.metaKey || e.ctrlKey) && e.key === "Enter") { e.preventDefault() const form = e.currentTarget.closest("form") if (form && input.trim() && status !== "streaming") { form.requestSubmit() } } } // Handle file changes const handleFileChange = (e: React.ChangeEvent) => { if (onFileChange) { onFileChange(e.target.files || undefined) } } // Clear file selection const clearFiles = () => { if (fileInputRef.current) { fileInputRef.current.value = '' } if (onFileChange) { onFileChange(undefined) } } // Trigger file input click const triggerFileInput = () => { fileInputRef.current?.click() } return (
{/* File preview area */} {files && files.length > 0 && (
{Array.from(files).map((file, index) => (
{file.type.startsWith('image/') ? ( {file.name} ) : (
{file.name}
)}
))}
)}