import { Box, Button, Grid, GridItem, useBreakpointValue } from '@chakra-ui/react'; import { observer } from 'mobx-react-lite'; import React, { useEffect, useRef, useState } from 'react'; import { useMaxWidth } from '../../../hooks/useMaxWidth'; import chatStore from '../../../stores/ClientChatStore'; import userOptionsStore from '../../../stores/UserOptionsStore'; import InputMenu from '../input-menu/InputMenu'; import SendButton from './ChatInputSendButton'; import InputTextarea from './ChatInputTextArea'; 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('40%'); 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 (const 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: '30rem', lg: '50rem', md: '80%', sm: '100vw' }, { ssr: true }, ); const inputMinWidth = useBreakpointValue({ lg: '40rem', md: '30rem' }, { ssr: true }); useEffect(() => { setInputWidth('100%'); }, [inputMaxWidth, inputMinWidth]); return ( {couldFollow && ( )} ); }); export default ChatInput;