"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import Image from "next/image"; import { AnimatePresence, motion } from "framer-motion"; import { Globe, Paperclip, Plus, Send } from "lucide-react"; import { cn } from "../../lib/utils"; import { Textarea } from "./textarea"; interface UseAutoResizeTextareaProps { minHeight: number; maxHeight?: number; } function useAutoResizeTextarea({ minHeight, maxHeight, }: UseAutoResizeTextareaProps) { const textareaRef = useRef(null); const adjustHeight = useCallback( (reset?: boolean) => { const textarea = textareaRef.current; if (!textarea) return; if (reset) { textarea.style.height = `${minHeight}px`; return; } textarea.style.height = `${minHeight}px`; const newHeight = Math.max( minHeight, Math.min(textarea.scrollHeight, maxHeight ?? Number.POSITIVE_INFINITY) ); textarea.style.height = `${newHeight}px`; }, [minHeight, maxHeight] ); useEffect(() => { const textarea = textareaRef.current; if (textarea) { textarea.style.height = `${minHeight}px`; } }, [minHeight]); useEffect(() => { const handleResize = () => adjustHeight(); window.addEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize); }, [adjustHeight]); return { textareaRef, adjustHeight }; } const MIN_HEIGHT = 48; const MAX_HEIGHT = 164; const AnimatedPlaceholder = ({ showSearch }: { showSearch: boolean }) => ( {showSearch ? "Search the web..." : "Ask Skiper Ai..."} ); export function AiInput() { const [value, setValue] = useState(""); const { textareaRef, adjustHeight } = useAutoResizeTextarea({ minHeight: MIN_HEIGHT, maxHeight: MAX_HEIGHT, }); const [showSearch, setShowSearch] = useState(true); const [imagePreview, setImagePreview] = useState(null); const fileInputRef = useRef(null); const handelClose = (e: any) => { e.preventDefault(); e.stopPropagation(); if (fileInputRef.current) { fileInputRef.current.value = ""; // Reset file input } setImagePreview(null); // Use null instead of empty string }; const handelChange = (e: any) => { const file = e.target.files ? e.target.files[0] : null; if (file) { setImagePreview(URL.createObjectURL(file)); } }; const handleSubmit = () => { setValue(""); adjustHeight(true); }; useEffect(() => { return () => { if (imagePreview) { URL.revokeObjectURL(imagePreview); } }; }, [imagePreview]); return (