@@ -18,6 +18,7 @@ function CollaborationPage() {
18
18
const isRemoteChange = useRef ( false ) ; // Flag to prevent infinite loop, true if change to editor is from remote (other user)
19
19
const timeoutRef = useRef ( null ) ; // Timeout reference for the read-only state of the editor
20
20
const countdownRef = useRef ( null ) ; // Timeout reference for the countdown when user gets kicked out
21
+ const messagesEndRef = useRef ( null ) ; // Reference to the end of the chat messages
21
22
22
23
const [ code , setCode ] = useState ( '' ) ;
23
24
const [ question , setQuestion ] = useState ( '' ) ;
@@ -29,6 +30,7 @@ function CollaborationPage() {
29
30
const [ userId , setUserId ] = useState ( '' ) ;
30
31
const [ chatMessages , setChatMessages ] = useState ( [ ] ) ;
31
32
const [ newMessage , setNewMessage ] = useState ( '' ) ;
33
+ const [ currentUsername , setCurrentUsername ] = useState ( '' ) ;
32
34
33
35
function formatQuestion ( question ) {
34
36
return `
@@ -50,6 +52,7 @@ ${question.questionDescription}
50
52
const data = await verifyToken ( token ) ;
51
53
const username = data . data . username ;
52
54
setUserId ( data . data . id ) ;
55
+ setCurrentUsername ( username ) ;
53
56
54
57
console . log ( "Username:" , username ) ;
55
58
@@ -110,11 +113,24 @@ ${question.questionDescription}
110
113
} ) ;
111
114
112
115
// Listen for chat messages
113
- collabService . onChatMessage ( ( data ) => {
116
+ const handleChatMessage = ( data ) => {
114
117
setChatMessages ( ( prevMessages ) => [ ...prevMessages , { sender : data . sender , message : data . message } ] ) ;
115
- } ) ;
118
+ } ;
119
+
120
+ collabService . onChatMessage ( handleChatMessage ) ;
121
+
122
+ return ( ) => {
123
+ collabService . offChatMessage ( handleChatMessage ) ;
124
+ } ;
116
125
} , [ ] ) ;
117
126
127
+ // Auto-scroll to the bottom when new messages are added
128
+ useEffect ( ( ) => {
129
+ if ( messagesEndRef . current ) {
130
+ messagesEndRef . current . scrollIntoView ( { behavior : 'smooth' } ) ;
131
+ }
132
+ } , [ chatMessages ] ) ;
133
+
118
134
// Kick user out if other user disconnects
119
135
useEffect ( ( ) => {
120
136
collabService . onOtherUserDisconnect ( ( ) => {
@@ -215,19 +231,27 @@ ${question.questionDescription}
215
231
216
232
{ /* Chatbox at the bottom */ }
217
233
< div className = "h-1/4 w-full border-t border-gray-700 flex flex-col bg-gray-800" >
218
- < div className = "flex-grow overflow-y-auto p-3" >
234
+ < div className = "flex-grow overflow-y-auto overflow-x-hidden p-3" >
219
235
{ chatMessages . map ( ( msg , index ) => (
220
- < div key = { index } className = { `mb-2 ${ msg . sender === 'You' ? 'text-right' : 'text-left' } ` } >
221
- < span className = "text-sm text-white font-semibold" > { msg . sender } :</ span >
222
- < p className = "text-base text-white" > { msg . message } </ p >
236
+ < div key = { index } className = { `flex flex-col mb-2 ${ msg . sender === 'You' ? 'items-end' : 'items-start' } ` } >
237
+ < div className = { `text-sm font-semibold text-white ${ msg . sender === 'You' ? 'text-right' : 'text-left' } ` } >
238
+ { msg . sender }
239
+ </ div >
240
+ < div
241
+ className = { `max-w-md p-2 rounded-lg break-words ${ msg . sender === 'You' ? 'bg-blue-600 text-white text-right' : 'bg-gray-700 text-white'
242
+ } `}
243
+ >
244
+ < p > { msg . message } </ p >
245
+ </ div >
223
246
</ div >
224
247
) ) }
248
+ < div ref = { messagesEndRef } />
225
249
</ div >
226
250
< div className = "p-3 border-t border-gray-700" >
227
251
< div className = "flex" >
228
252
< input
229
253
type = "text"
230
- className = "flex-grow bg-gray-900 text -white p-2 rounded-l-md focus:outline-none "
254
+ className = "input input-bordered w-full bg -white text-black rounded-l-full rounded-r-full "
231
255
placeholder = "Type a message..."
232
256
value = { newMessage }
233
257
onChange = { ( e ) => setNewMessage ( e . target . value ) }
@@ -238,7 +262,7 @@ ${question.questionDescription}
238
262
} }
239
263
/>
240
264
< button
241
- className = "bg-blue-600 text-white p -2 rounded-r-md "
265
+ className = "btn btn-primary ml -2 rounded-l-full rounded-r-full "
242
266
onClick = { handleSendMessage }
243
267
>
244
268
Send
0 commit comments