2020-10-12 06:25:59 -04:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
|
2023-09-12 11:29:49 -04:00
|
|
|
type MessageInputProps = {
|
|
|
|
resetUnreadMessages: () => void
|
|
|
|
sendMessage: (message: string) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
function MessageInput({ resetUnreadMessages, sendMessage }: MessageInputProps) {
|
2020-10-12 06:25:59 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2023-09-12 11:29:49 -04:00
|
|
|
function handleKeyDown(event: React.KeyboardEvent) {
|
|
|
|
const selectingCharacter = event.nativeEvent.isComposing
|
|
|
|
if (event.key === 'Enter' && !selectingCharacter) {
|
2020-10-12 06:25:59 -04:00
|
|
|
event.preventDefault()
|
2023-09-12 11:29:49 -04:00
|
|
|
const target = event.target as HTMLInputElement
|
|
|
|
sendMessage(target.value)
|
2022-08-11 05:16:24 -04:00
|
|
|
// wrap the form reset in setTimeout so input sources have time to finish
|
|
|
|
// https://github.com/overleaf/internal/pull/9206
|
|
|
|
window.setTimeout(() => {
|
2023-09-12 11:29:49 -04:00
|
|
|
target.blur()
|
|
|
|
target.closest('form')?.reset()
|
|
|
|
target.focus()
|
2022-08-11 05:16:24 -04:00
|
|
|
}, 0)
|
2020-10-12 06:25:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-08-11 05:16:24 -04:00
|
|
|
<form className="new-message">
|
2020-11-09 09:52:34 -05:00
|
|
|
<label htmlFor="chat-input" className="sr-only">
|
2022-10-03 11:22:47 -04:00
|
|
|
{t('your_message_to_collaborators')}
|
2020-11-09 09:52:34 -05:00
|
|
|
</label>
|
2020-10-12 06:25:59 -04:00
|
|
|
<textarea
|
2020-11-09 09:52:34 -05:00
|
|
|
id="chat-input"
|
2022-10-03 11:22:47 -04:00
|
|
|
placeholder={`${t('your_message_to_collaborators')}…`}
|
2020-10-12 06:25:59 -04:00
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
onClick={resetUnreadMessages}
|
|
|
|
/>
|
2022-08-11 05:16:24 -04:00
|
|
|
</form>
|
2020-10-12 06:25:59 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageInput.propTypes = {
|
|
|
|
resetUnreadMessages: PropTypes.func.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
sendMessage: PropTypes.func.isRequired,
|
2020-10-12 06:25:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default MessageInput
|