import { useEffect, useRef, useState } from 'react' import PropTypes from 'prop-types' import { updateRefHeight } from '../../utils' import { useTranslation } from 'react-i18next' export function InputBox({ onSubmit, enabled, port, reverseResizeDir }) { const { t } = useTranslation() const [value, setValue] = useState('') const reverseDivRef = useRef(null) const inputRef = useRef(null) const resizedRef = useRef(false) const virtualInputRef = reverseResizeDir ? reverseDivRef : inputRef useEffect(() => { const onResizeY = () => { if (virtualInputRef.current.h !== virtualInputRef.current.offsetHeight) { virtualInputRef.current.h = virtualInputRef.current.offsetHeight if (!resizedRef.current) { resizedRef.current = true virtualInputRef.current.style.maxHeight = '' } } } virtualInputRef.current.h = virtualInputRef.current.offsetHeight virtualInputRef.current.addEventListener('mousemove', onResizeY) }, []) useEffect(() => { if (!resizedRef.current) { if (!reverseResizeDir) { updateRefHeight(inputRef) virtualInputRef.current.h = virtualInputRef.current.offsetHeight virtualInputRef.current.style.maxHeight = '160px' } } }) useEffect(() => { if (enabled) inputRef.current.focus() }, [enabled]) const handleKeyDownOrClick = (e) => { e.stopPropagation() if (e.type === 'click' || (e.keyCode === 13 && e.shiftKey === false)) { if (enabled) { e.preventDefault() if (!value) return onSubmit(value) setValue('') } else { e.preventDefault() port.postMessage({ stop: true }) } } } return (