"use client"; import { useLayoutEffect, useRef, useState } from "react"; import { ImageIcon, SendHorizonalIcon } from "lucide-react"; type MessageInputProps = { disabled?: boolean; uploadingImage?: boolean; onSend: (content: string) => Promise; onSendImage: (file: File) => Promise; }; export function MessageInput({ disabled, uploadingImage = false, onSend, onSendImage, }: MessageInputProps) { const [value, setValue] = useState(""); const [submitting, setSubmitting] = useState(false); const textareaRef = useRef(null); const imageInputRef = useRef(null); useLayoutEffect(() => { const textarea = textareaRef.current; if (!textarea) { return; } const lineHeight = 20; const maxHeight = lineHeight * 8; textarea.style.height = "0px"; textarea.style.height = `${Math.min(textarea.scrollHeight, maxHeight)}px`; textarea.style.overflowY = textarea.scrollHeight > maxHeight ? "auto" : "hidden"; }, [value]); async function handleSubmit() { const content = value.trim(); if (!content || disabled || submitting) { return; } setSubmitting(true); try { await onSend(content); setValue(""); } finally { setSubmitting(false); requestAnimationFrame(() => { textareaRef.current?.focus(); }); } } async function handleSelectImage(event: React.ChangeEvent) { const file = event.target.files?.[0]; event.target.value = ""; if (!file || disabled || uploadingImage || submitting) { return; } if (!file.type.startsWith("image/")) { return; } try { await onSendImage(file); } finally { requestAnimationFrame(() => { textareaRef.current?.focus(); }); } } return (