1
- import { useState , useEffect } from 'react' ;
1
+ import { useState , useEffect , useMemo } from 'react' ;
2
2
import { useChannel } from '@ably-labs/react-hooks' ;
3
3
import { Button , Box , Flex , Text , Spinner , useToast , Textarea } from '@chakra-ui/react' ;
4
4
import { trpc } from '../../utils/trpc' ;
@@ -130,39 +130,50 @@ const Chat = (props: ChatProps) => {
130
130
} ) ;
131
131
} ;
132
132
133
- const allMessages = messages . map ( ( message , index : number ) => {
134
- const { content, sentByName, sentByUserId, sentByUserRole } = message ;
135
- const amISender = sentByUserId === ( session ?. user ?. id ?? 'Unknown' ) ;
136
-
137
- return (
138
- < Flex
139
- key = { index }
140
- data-author = { sentByName }
141
- backgroundColor = { amISender ? 'blue.600' : 'gray' }
142
- p = { 3 }
143
- alignSelf = { amISender ? 'flex-end' : 'flex-start' }
144
- borderRadius = { 5 }
145
- borderBottomRightRadius = { amISender ? 0 : 5 }
146
- borderBottomLeftRadius = { amISender ? 5 : 0 }
147
- color = 'white'
148
- >
149
- < Text mr = { 2 } fontWeight = 'bold' hidden = { amISender } >
150
- { canSeeName || sentByUserRole !== UserRole . STUDENT
151
- ? sentByName
152
- : 'Anonymous' + ' (' + uppercaseFirstLetter ( sentByUserRole ) + ')' }
153
- </ Text >
154
- { content }
155
- </ Flex >
156
- ) ;
157
- } ) ;
133
+ const allMessages = useMemo (
134
+ ( ) =>
135
+ messages . map ( ( message , index ) => {
136
+ const { content, sentByName, sentByUserId, sentByUserRole } = message ;
137
+ const amISender = sentByUserId === ( session ?. user ?. id ?? 'Unknown' ) ;
138
+
139
+ return (
140
+ < Flex
141
+ key = { index }
142
+ data-author = { sentByName }
143
+ backgroundColor = { amISender ? 'blue.600' : 'gray' }
144
+ p = { 3 }
145
+ alignSelf = { amISender ? 'flex-end' : 'flex-start' }
146
+ borderRadius = { 5 }
147
+ borderBottomRightRadius = { amISender ? 0 : 5 }
148
+ borderBottomLeftRadius = { amISender ? 5 : 0 }
149
+ color = 'white'
150
+ >
151
+ < Text mr = { 2 } fontWeight = 'bold' hidden = { amISender } >
152
+ { canSeeName || sentByUserRole !== UserRole . STUDENT
153
+ ? sentByName
154
+ : 'Anonymous' + ' (' + uppercaseFirstLetter ( sentByUserRole ) + ')' }
155
+ </ Text >
156
+ { content }
157
+ </ Flex >
158
+ ) ;
159
+ } ) ,
160
+ [ messages ] ,
161
+ ) ;
158
162
159
163
useEffect ( ( ) => {
160
164
if ( messageEnd ) {
161
- // Bug: This causes scroll to bottom of page
162
- // messageEnd.scrollIntoView({ behaviour: 'smooth' });
165
+ messageEnd . scrollIntoView ( { behaviour : 'smooth' } ) ;
163
166
}
164
167
} , [ messages , messageEnd ] ) ;
165
168
169
+ /** Pressing Enter should call handleFormSubmission */
170
+ const handleKeyPress = ( event : React . KeyboardEvent < HTMLTextAreaElement > ) => {
171
+ if ( event . key === 'Enter' && ! event . shiftKey ) {
172
+ event . preventDefault ( ) ;
173
+ handleFormSubmission ( event as any ) ;
174
+ }
175
+ } ;
176
+
166
177
return (
167
178
< >
168
179
{ ! isChatLoaded && < Spinner /> }
@@ -180,6 +191,7 @@ const Chat = (props: ChatProps) => {
180
191
value = { messageText }
181
192
placeholder = 'Type a message...'
182
193
onChange = { e => setMessageText ( e . target . value ) }
194
+ onKeyDown = { handleKeyPress }
183
195
mr = { 4 }
184
196
maxLength = { 1000 }
185
197
/>
0 commit comments