diff --git a/src/frontend/src/features/rooms/livekit/components/chat/Input.tsx b/src/frontend/src/features/rooms/livekit/components/chat/Input.tsx new file mode 100644 index 00000000..d4aa68e1 --- /dev/null +++ b/src/frontend/src/features/rooms/livekit/components/chat/Input.tsx @@ -0,0 +1,120 @@ +import { Button } from '@/primitives' +import { HStack } from '@/styled-system/jsx' +import { RiSendPlane2Fill } from '@remixicon/react' +import { useState, useEffect } from 'react' +import { TextArea } from '@/primitives/TextArea' +import { RefObject } from 'react' +import { useTranslation } from 'react-i18next' + +const MAX_ROWS = 6 + +interface ChatInputProps { + inputRef: RefObject + onSubmit: (text: string) => void + isSending: boolean +} + +export const ChatInput = ({ + inputRef, + onSubmit, + isSending, +}: ChatInputProps) => { + const { t } = useTranslation('rooms', { keyPrefix: 'controls.chat.input' }) + const [text, setText] = useState('') + const [rows, setRows] = useState(1) + + const handleSubmit = () => { + onSubmit(text) + setText('') + } + + const isDisabled = !text.trim() || isSending + + const submitOnEnter = (e: React.KeyboardEvent) => { + if (e.key !== 'Enter' || (e.key === 'Enter' && e.shiftKey)) return + e.preventDefault() + if (!isDisabled) handleSubmit() + } + + useEffect(() => { + const resize = () => { + if (!inputRef.current) return + + const textAreaLineHeight = 20 // Adjust this value based on your TextArea's line height + const previousRows = inputRef.current.rows + inputRef.current.rows = 1 + + const currentRows = Math.floor( + inputRef.current.scrollHeight / textAreaLineHeight + ) + + if (currentRows === previousRows) { + inputRef.current.rows = currentRows + } + + if (currentRows >= MAX_ROWS) { + inputRef.current.rows = MAX_ROWS + inputRef.current.scrollTop = inputRef.current.scrollHeight + } + + if (currentRows < MAX_ROWS) { + inputRef.current.style.overflowY = 'hidden' + } else { + inputRef.current.style.overflowY = 'auto' + } + + setRows(currentRows < MAX_ROWS ? currentRows : MAX_ROWS) + } + + resize() + }, [text, inputRef]) + + return ( + +