"use client"; import React, { useCallback, useEffect, useState, useRef } from "react"; import { Textarea } from "@/components/ui/textarea-chat"; import { Send, Loader2, Sparkles } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useChatLogic } from "@/hooks/useChatLogic"; import type { Chat } from "@/lib/types"; type Props = { selectedChat: Chat | null; selectedChatId: string | null; scrollToBottom: () => void; }; const MessageBar: React.FC = ({ selectedChat, selectedChatId, scrollToBottom, }) => { const { isLoading, handleSubmit } = useChatLogic( selectedChat, selectedChatId, scrollToBottom ); const [input, setInput] = useState(""); const textareaRef = useRef(null); // Auto-resize function const adjustTextareaHeight = useCallback(() => { const textarea = textareaRef.current; if (textarea) { textarea.style.height = "auto"; const newHeight = Math.min(textarea.scrollHeight, 120); // Reduced max height for mobile textarea.style.height = `${newHeight}px`; } }, []); useEffect(() => { adjustTextareaHeight(); }, [input, adjustTextareaHeight]); const handleFormSubmit = useCallback( async (e: React.FormEvent) => { e.preventDefault(); if (input.trim()) { await handleSubmit(input); setInput(""); setTimeout(() => adjustTextareaHeight(), 0); } }, [handleSubmit, input, adjustTextareaHeight] ); const handleKeyDown = useCallback( async (e: React.KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); if (input.trim()) { await handleSubmit(input); setInput(""); setTimeout(() => adjustTextareaHeight(), 0); } } }, [handleSubmit, input, adjustTextareaHeight] ); useEffect(() => { const handleKeyPress = (e: KeyboardEvent) => { if (e.key === "/" && document.activeElement?.tagName !== "TEXTAREA") { e.preventDefault(); textareaRef.current?.focus(); } }; window.addEventListener("keydown", handleKeyPress); return () => window.removeEventListener("keydown", handleKeyPress); }, []); return (