@@ -25,6 +25,7 @@ const CollaborationSpace = () => {
25
25
const [ provider , setProvider ] = useState ( null ) ;
26
26
const [ code , setCode ] = useState ( '' ) ;
27
27
const [ users , setUsers ] = useState ( [ ] ) ; // track users in the room
28
+ const [ outputLoading , setOutputLoading ] = useState ( false )
28
29
const [ userId , setUserId ] = useState ( "" ) ; // current user
29
30
const [ language , setLanguage ] = useState ( "python" ) // set default language to python
30
31
const [ output , setOutput ] = useState ( "" )
@@ -46,6 +47,17 @@ const CollaborationSpace = () => {
46
47
navigate ( "/home" ) ;
47
48
} ;
48
49
50
+ { /* State management for user join/leave toast */ }
51
+ const [ notifs , setNotifs ] = useState ( [ ] ) ;
52
+ const addNotif = ( message ) => {
53
+ const id = Date . now ( ) ; // unique id based on timestamp
54
+ setNotifs ( ( prevNotifs ) => [ ...prevNotifs , { id, message} ] ) ;
55
+ // Remove notif after 2 seconds
56
+ setTimeout ( ( ) => {
57
+ setNotifs ( ( prevNotifs ) => prevNotifs . filter ( ( notif ) => notif . id !== id ) )
58
+ } , 1500 ) ;
59
+ } ;
60
+
49
61
50
62
{ /* Set up websockets for room management on client side, and collaboration for Yjs */ }
51
63
useEffect ( ( ) => {
@@ -91,7 +103,6 @@ const CollaborationSpace = () => {
91
103
// on getting a reply from server
92
104
websocket . onmessage = ( event ) => {
93
105
const data = JSON . parse ( event . data ) ;
94
- console . log ( `[FRONTEND] data message is ${ JSON . stringify ( data ) } ` ) ;
95
106
switch ( data . type ) {
96
107
case 'usersListUpdate' :
97
108
setUsers ( data . users ) ; // Update the user list
@@ -107,6 +118,15 @@ const CollaborationSpace = () => {
107
118
console . log ( "adding message" , data . message )
108
119
setMessages ( ( prevMessages ) => [ ...prevMessages , data . message ] ) ;
109
120
break ;
121
+ case 'languageChange' :
122
+ addNotif ( `User ${ data . user } has changed the language to ${ data . language } ` ) ;
123
+ setLanguage ( data . language ) ;
124
+ break ;
125
+ case 'userJoin' :
126
+ addNotif ( `User ${ data . user } has joined.` )
127
+ break ;
128
+ case 'userLeft' :
129
+ addNotif ( `User ${ data . user } has left` )
110
130
default :
111
131
console . log ( "No messages received from room management server" ) ;
112
132
break ;
@@ -151,6 +171,7 @@ const CollaborationSpace = () => {
151
171
} ;
152
172
153
173
const handleCodeRun = ( ) => {
174
+ setOutputLoading ( true ) ;
154
175
const code_message = {
155
176
"language" : language ,
156
177
"files" : [
@@ -164,6 +185,7 @@ const CollaborationSpace = () => {
164
185
collabService . getCodeOutput ( code_message )
165
186
. then ( result => {
166
187
console . log ( result . data . run . output )
188
+ setOutputLoading ( false ) ;
167
189
setOutput ( result . data . run . output )
168
190
} )
169
191
. catch ( err => console . log ( err ) ) ;
@@ -180,6 +202,11 @@ const CollaborationSpace = () => {
180
202
websocketRef . current . send ( JSON . stringify ( { type : 'sendMessage' , roomId : roomId , message : message } ) ) ;
181
203
}
182
204
205
+ const handleLanguageChange = ( value ) => {
206
+ websocketRef . current . send ( JSON . stringify ( { type : 'languageChange' , roomId : roomId ,
207
+ user : userId , language : value } ) ) ;
208
+ }
209
+
183
210
if ( loading ) {
184
211
return (
185
212
< div style = { { textAlign : 'center' } } >
@@ -209,11 +236,21 @@ const CollaborationSpace = () => {
209
236
</ ToastContainer >
210
237
) : (
211
238
< >
239
+ { /* Toast Container for Join/Leave notifications */ }
240
+ < ToastContainer className = 'p-3' position = 'top-center' style = { { zIndex : 1050 } } >
241
+ { notifs . map ( ( notif ) => (
242
+ < Toast key = { notif . id } className = 'mb-2' style = { { backgroundColor : 'rgba(255, 255, 255, 0.8)' } } >
243
+ < Toast . Body >
244
+ < strong className = 'text-black' > { notif . message } </ strong >
245
+ </ Toast . Body >
246
+ </ Toast >
247
+ ) ) }
248
+ </ ToastContainer >
212
249
< CollabNavigationBar handleExit = { handleExit } handleCodeRun = { handleCodeRun } users = { users } setLanguage = { setLanguage } language = { language } />
213
250
< Container fluid style = { { marginTop : '20px' , height : 'calc(100vh - 60px)' , display : 'flex' , overflow : 'hidden' } } >
214
251
< Row style = { { flexGrow : 1 , width : '100%' , height : '100%' , overflow : 'hidden' } } >
215
252
< Col md = { 6 } style = { { display : 'flex' , flexDirection : 'column' , height : '100%' , overflow : 'hidden' } } >
216
- < CodeSpace handleEditorChange = { handleEditorChange } code = { code } language = { language } output = { output } />
253
+ < CodeSpace handleEditorChange = { handleEditorChange } loading = { outputLoading } code = { code } language = { language } output = { output } />
217
254
</ Col >
218
255
< Col md = { 6 } style = { { display : 'flex' , flexDirection : 'column' , height : '100%' , overflow : 'hidden' } } >
219
256
< div style = { { flex : '60%' , display : 'flex' , overflow : 'hidden' } } >
0 commit comments