import React, { useEffect, useRef, useState } from "react"; import { Box, Button, Grid, GridItem, useBreakpointValue, } from "@chakra-ui/react"; import { observer } from "mobx-react-lite"; import chatStore from "../../stores/ClientChatStore"; import InputMenu from "./flyoutmenu/InputMenu"; import InputTextarea from "./ChatInputTextArea"; import SendButton from "./ChatInputSendButton"; import { useMaxWidth } from "../../layout/useMaxWidth"; import userOptionsStore from "../../stores/UserOptionsStore"; const ChatInput = observer(() => { const inputRef = useRef(null); const containerRef = useRef(null); const maxWidth = useMaxWidth(); const [inputValue, setInputValue] = useState(""); const [containerHeight, setContainerHeight] = useState(56); const [containerBorderRadius, setContainerBorderRadius] = useState(9999); const [shouldFollow, setShouldFollow] = useState( userOptionsStore.followModeEnabled, ); const [couldFollow, setCouldFollow] = useState(chatStore.isLoading); const [inputWidth, setInputWidth] = useState("50%"); useEffect(() => { setShouldFollow(chatStore.isLoading && userOptionsStore.followModeEnabled); setCouldFollow(chatStore.isLoading); }, [chatStore.isLoading, userOptionsStore.followModeEnabled]); useEffect(() => { inputRef.current?.focus(); setInputValue(chatStore.input); }, [chatStore.input]); useEffect(() => { if (containerRef.current) { const observer = new ResizeObserver((entries) => { for (let entry of entries) { const newHeight = entry.target.clientHeight; setContainerHeight(newHeight); const newBorderRadius = Math.max(28 - (newHeight - 56) * 0.2, 16); setContainerBorderRadius(newBorderRadius); } }); observer.observe(containerRef.current); return () => observer.disconnect(); } }, []); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); chatStore.sendMessage(); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); chatStore.sendMessage(); } }; const inputMaxWidth = useBreakpointValue( { base: "50rem", lg: "50rem", md: "80%", sm: "100vw" }, { ssr: true }, ); const inputMinWidth = useBreakpointValue({ lg: "40rem" }, { ssr: true }); useEffect(() => { setInputWidth("100%"); }, [inputMaxWidth, inputMinWidth]); return ( {couldFollow && ( )} ); }); export default ChatInput;