import Image from "next/image"; import { FC, useRef } from "react"; import TypeAnimation from "../../TypeAnimation"; type TInputAreaProps = { promptValue: string; setPromptValue: React.Dispatch>; handleSubmit: (query: string) => void; handleSecondary?: (query: string) => void; disabled?: boolean; reset?: () => void; isStopped?: boolean; }; // Debounce function to limit the rate at which a function can fire function debounce(func: Function, wait: number) { let timeout: NodeJS.Timeout | undefined; return function executedFunction(...args: any[]) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; } const InputArea: FC = ({ promptValue, setPromptValue, handleSubmit, handleSecondary, disabled, reset, isStopped, }) => { // Only show input if not stopped if (isStopped) { return null; } const placeholder = handleSecondary ? "Any questions about this report?" : "What would you like to research next?"; const textareaRef = useRef(null); const resetHeight = () => { if (textareaRef.current) { textareaRef.current.style.height = '3em'; // Reset to base height } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { if (e.shiftKey) { return; // Allow new line on Shift+Enter } else { e.preventDefault(); if (!disabled) { if (reset) reset(); handleSubmit(promptValue); setPromptValue(''); // Clear prompt value resetHeight(); // Reset height after submit } } } }; // Debounced version of the height adjustment function const adjustHeight = debounce((target: HTMLTextAreaElement) => { target.style.height = 'auto'; // Reset height to auto to allow shrinking target.style.height = `${target.scrollHeight}px`; // Adjust height }, 100); // Adjust the delay as needed const handleTextareaChange = (e: React.ChangeEvent) => { const target = e.target; adjustHeight(target); // Use debounced function setPromptValue(target.value); }; return (
{ e.preventDefault(); if (reset) reset(); handleSubmit(promptValue); setPromptValue(''); // Clear prompt value resetHeight(); }} >