import { KeyboardEventHandler, useEffect, useRef, useState } from "react"; import { PerformerState } from "@performer/core"; export type MessageInputProps = { disclaimer?: string; onSubmit: (text: string) => void; state: PerformerState; }; export function MessageInput({ disclaimer, onSubmit, state, }: MessageInputProps) { const [text, setText] = useState(""); const textAreaRef = useRef(null); const hiddenDivRef = useRef(null); const submitBtnRef = useRef(null); useEffect(() => { if (hiddenDivRef.current && textAreaRef.current) { hiddenDivRef.current.innerHTML = text.replace(/\n/g, "
") + "
"; textAreaRef.current.style.height = `${hiddenDivRef.current.offsetHeight}px`; } }, [text]); const handleChange = (e: any) => { setText(e.target.value); }; const handleKeyPress: KeyboardEventHandler = (e) => { if (e.key === "Enter" && !e.shiftKey && !e.ctrlKey) { e.preventDefault(); submitBtnRef.current?.click(); setText(""); } }; useEffect(() => { if (textAreaRef.current && state === "listening") { textAreaRef.current.focus(); } }, [state]); return ( <>
{ e.preventDefault(); onSubmit(text); }} className="stretch mx-2 flex flex-row gap-3 last:mb-2 md:mx-4 md:last:mb-6 lg:mx-auto lg:max-w-2xl xl:max-w-3xl" >
{text}
{disclaimer && (
{disclaimer}
)} ); }