/
SendMessage.js
147 lines (120 loc) · 4.17 KB
/
SendMessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict'
import React from 'react'
import PropTypes from 'prop-types'
import { useTranslation } from 'react-i18next'
import { emojiIndex } from 'emoji-mart'
import EmojiPicker from '../components/EmojiPicker'
import '../styles/SendMessage.scss'
function SendMessage ({ channelName, onSendMessage, theme, useEmojis, emojiSet, disabled }) {
const [t] = useTranslation()
const [inputValue, setInputValue] = React.useState('')
const [emojiResults, setEmojiResults] = React.useState([])
const [emojiPickerActive, setEmojiPickerActive] = React.useState(false)
const inputRef = React.useRef()
const emojiPickerRef = React.useRef()
const submitHiddenButton = React.useRef()
const focusInput = React.useCallback(() => {
if (inputRef.current) inputRef.current.focus()
}, [])
const handleInputSubmit = React.useCallback(
e => {
e.preventDefault()
if (!inputRef.current) return
const inputField = inputRef.current
const text = inputField.value.trim()
setInputValue('')
setEmojiResults([])
setEmojiPickerActive(false)
focusInput()
onSendMessage(text).catch(e => {
// There was an error sending the message
// Revert input value and focus the field
setInputValue(text)
focusInput()
})
},
[onSendMessage, focusInput]
)
const handleInputChange = React.useCallback(() => {
if (!inputRef.current) return
const inputValue = inputRef.current.value
const lastWord = inputValue.split(' ').pop()
const emojiPickerActive = lastWord.startsWith(':') && useEmojis
const lastEmojiIdx = emojiPickerActive ? inputValue.lastIndexOf(':') : null
const emojiSearch = emojiPickerActive ? inputValue.slice(lastEmojiIdx) : null
let emojiResults = emojiSearch ? emojiIndex.search(emojiSearch) : []
if (emojiResults.length === 0) {
// Slice the ':' prefix so we search for emojis instead of emoticons so
// ':smile' => 'smile'
// This is needed because we can also search for ':)' which requires the
// ':' prefix
emojiResults = emojiSearch ? emojiIndex.search(emojiSearch.slice(1)) : []
}
setInputValue(inputValue)
setEmojiResults(emojiResults)
setEmojiPickerActive(emojiPickerActive)
}, [useEmojis])
const handleInputKeyDown = (e) => {
if(e.keyCode === 13 && !e.shiftKey) {
e.preventDefault();
submitHiddenButton.current.click();
}
};
const handleEmojiChange = React.useCallback(
(emoji, done = false) => {
if (!emoji || !inputRef.current) return
const i = inputValue.lastIndexOf(':')
const text = inputValue.slice(0, i) + emoji.colons
setInputValue(text)
setEmojiPickerActive(!done)
inputRef.current.focus()
},
[inputValue]
)
React.useLayoutEffect(focusInput, [channelName, disabled, focusInput])
function getEmojiPickerStyle (emojiSize) {
return {
maxWidth: (emojiSize + 2) * 10, // 2 * 1px padding or border
bottom: inputRef.current ? inputRef.current.offsetHeight + 10 : '45px'
}
}
const pickerEmojiSize = 24
return (
<div className='SendMessage'>
<form onSubmit={handleInputSubmit}>
{emojiPickerActive && emojiResults.length > 0 ? (
<EmojiPicker
ref={emojiPickerRef}
emojis={emojiResults}
onChange={handleEmojiChange}
emojiSize={pickerEmojiSize}
emojiSet={emojiSet}
style={getEmojiPickerStyle(pickerEmojiSize)}
/>
) : null}
<textarea
cols="30"
rows="2"
ref={inputRef}
type='text'
placeholder={t('channel.sendMessagePlaceholder')}
style={theme}
value={inputValue}
onKeyDown={handleInputKeyDown}
onChange={handleInputChange}
disabled={disabled}
/>
<input type="submit" ref={submitHiddenButton} />
</form>
</div>
)
}
SendMessage.propTypes = {
channelName: PropTypes.string.isRequired,
onSendMessage: PropTypes.func.isRequired,
theme: PropTypes.object.isRequired,
useEmojis: PropTypes.bool.isRequired,
emojiSet: PropTypes.string.isRequired,
disabled: PropTypes.bool
}
export default SendMessage