@@ -2,13 +2,14 @@ import React, { useEffect, useState, useRef } from 'react';
2
2
import { useParams , useNavigate } from 'react-router-dom' ;
3
3
import * as Y from 'yjs' ;
4
4
import { WebsocketProvider } from 'y-websocket' ;
5
- import { getUserFromToken } from '../user/userAvatarBox ' ;
5
+ import { getUserFromToken } from '../user/utils/authUtils ' ;
6
6
import QuestionDisplay from './QuestionDisplay' ;
7
7
import Chat from './Chat' ;
8
8
import CollabNavigationBar from './CollabNavigationBar' ;
9
9
import CodeSpace from './CodeSpace' ;
10
10
import { Container , Row , Col } from 'react-bootstrap' ;
11
11
import collabService from '../../services/collab' ;
12
+ import historyService from '../../services/history' ;
12
13
import Toast from 'react-bootstrap/Toast' ;
13
14
import ToastContainer from 'react-bootstrap/ToastContainer' ;
14
15
import Spinner from 'react-bootstrap/Spinner' ;
@@ -21,7 +22,9 @@ const CollaborationSpace = () => {
21
22
const [ provider , setProvider ] = useState ( null ) ;
22
23
const [ code , setCode ] = useState ( '' ) ;
23
24
const [ users , setUsers ] = useState ( [ ] ) ; // track users in the room
25
+ const [ usersSave , setUsersSave ] = useState ( [ ] ) ; // for saving users in the room; getting matched user's username
24
26
const [ userId , setUserId ] = useState ( "" ) ; // current user
27
+ const [ username , setUsername ] = useState ( "" ) ; // current user
25
28
const [ language , setLanguage ] = useState ( "python" ) // set default language to python
26
29
const [ output , setOutput ] = useState ( "" )
27
30
const [ messages , setMessages ] = useState ( [ ] )
@@ -62,7 +65,8 @@ const CollaborationSpace = () => {
62
65
const fetchUser = async ( ) => {
63
66
const user = await getUserFromToken ( ) ;
64
67
if ( user !== "No User" ) {
65
- setUserId ( user . username ) ; // Set the username in state
68
+ setUserId ( user . userId ) ; // Set the username in state
69
+ setUsername ( user . username ) ; // Set the username in state
66
70
initiateWebSocket ( user . username ) ;
67
71
} else {
68
72
setUserId ( "Guest" ) ; // Fallback in case no user is found
@@ -85,6 +89,12 @@ const CollaborationSpace = () => {
85
89
console . log ( "Messages state updated:" , messages ) ;
86
90
} , [ messages ] ) ;
87
91
92
+ useEffect ( ( ) => {
93
+ if ( users . length == 2 ) {
94
+ setUsersSave ( users )
95
+ }
96
+ } , [ users ] )
97
+
88
98
const initiateWebSocket = ( userId ) => {
89
99
if ( websocketRef . current ) return ; // Prevent duplicate connections
90
100
@@ -156,6 +166,31 @@ const CollaborationSpace = () => {
156
166
} ;
157
167
158
168
const handleExit = ( ) => {
169
+ try {
170
+ // Filter out the current user from the usersSave array to find the matched user
171
+ const matchedUser = usersSave . filter ( user => user !== username ) [ 0 ] ; // Assuming usersSave contains objects with userId property
172
+
173
+ if ( ! matchedUser ) {
174
+ throw new Error ( "No matched user found" ) ;
175
+ }
176
+ const sessionData = {
177
+ user : userId ,
178
+ matchedUsername : matchedUser , // Assuming user[1] is the matched user
179
+ questionTitle : 'BFS' , // This ID should be available in context or passed down
180
+ // questionTitle: questionTitle, // This ID should be available in context or passed down
181
+ // startTime: roomCreationTime,
182
+ startTime : new Date ( ) ,
183
+ // duration: new Date().getTime() - new Date(roomCreationTime).getTime(),
184
+ duration : 10 ,
185
+ code : '1'
186
+ }
187
+
188
+ historyService . createHistoryAttempt ( sessionData )
189
+ // Navigate away or perform any additional actions on success
190
+ } catch ( error ) {
191
+ console . error ( "Failed to save session history:" , error )
192
+ }
193
+
159
194
if ( websocketRef . current ) websocketRef . current . send ( JSON . stringify ( { type : 'leaveRoom' , roomId, userId } ) ) ;
160
195
161
196
// Clean up Yjs document and provider before going back to home
0 commit comments