@@ -30,12 +30,16 @@ enum CommunicationEvents {
30
30
DISCONNECTED = "disconnected" ,
31
31
}
32
32
33
- const Chat : React . FC = ( ) => {
33
+ type ChatProps = {
34
+ isActive : boolean ;
35
+ } ;
36
+
37
+ const Chat : React . FC < ChatProps > = ( { isActive } ) => {
34
38
const [ messages , setMessages ] = useState < Message [ ] > ( [ ] ) ;
35
39
const [ inputValue , setInputValue ] = useState ( "" ) ;
36
40
const match = useMatch ( ) ;
37
41
const auth = useAuth ( ) ;
38
- const endOfMessagesRef = useRef < HTMLDivElement > ( null ) ;
42
+ const messagesRef = useRef < HTMLDivElement > ( null ) ;
39
43
40
44
if ( ! match ) {
41
45
throw new Error ( USE_MATCH_ERROR_MESSAGE ) ;
@@ -87,8 +91,14 @@ const Chat: React.FC = () => {
87
91
} , [ ] ) ;
88
92
89
93
useEffect ( ( ) => {
90
- endOfMessagesRef . current ?. scrollIntoView ( { behavior : "smooth" } ) ;
91
- } , [ messages ] ) ;
94
+ if ( messagesRef . current ) {
95
+ const nodes = messagesRef . current . querySelectorAll ( "div > div" ) ;
96
+ if ( nodes . length > 0 ) {
97
+ const lastNode = nodes [ nodes . length - 1 ] ;
98
+ lastNode . scrollIntoView ( { behavior : "smooth" } ) ;
99
+ }
100
+ }
101
+ } , [ messages , isActive ] ) ;
92
102
93
103
return (
94
104
< >
@@ -98,6 +108,7 @@ const Chat: React.FC = () => {
98
108
overflowY : "auto" ,
99
109
padding : 2 ,
100
110
} }
111
+ ref = { messagesRef }
101
112
>
102
113
{ messages . map ( ( msg , id ) =>
103
114
msg . type === "bot_generated" ? (
@@ -166,7 +177,7 @@ const Chat: React.FC = () => {
166
177
)
167
178
) }
168
179
</ Box >
169
- < div ref = { endOfMessagesRef } />
180
+ { /* <div ref={messagesRef } /> */ }
170
181
< TextField
171
182
placeholder = "Type message..."
172
183
margin = "none"
@@ -175,11 +186,12 @@ const Chat: React.FC = () => {
175
186
value = { inputValue }
176
187
onChange = { ( e ) => setInputValue ( e . target . value ) }
177
188
onKeyDown = { ( e ) => {
178
- if ( e . key === "Enter" && inputValue !== "" ) {
189
+ const trimmedValue = inputValue . trim ( ) ;
190
+ if ( e . key === "Enter" && trimmedValue !== "" ) {
179
191
e . preventDefault ( ) ;
180
192
communicationSocket . emit ( CommunicationEvents . SEND_TEXT_MESSAGE , {
181
193
roomId : getMatchId ( ) ,
182
- message : inputValue ,
194
+ message : trimmedValue ,
183
195
username : user ?. username ,
184
196
createdTime : Date . now ( ) ,
185
197
} ) ;
0 commit comments