@@ -15,7 +15,7 @@ import {
15
15
import { Content } from "antd/es/layout/layout" ;
16
16
import "./styles.scss" ;
17
17
import { useRouter , useSearchParams } from "next/navigation" ;
18
- import { useEffect , useState } from "react" ;
18
+ import { useEffect , useRef , useState } from "react" ;
19
19
import { GetSingleQuestion , Question } from "@/app/services/question" ;
20
20
import {
21
21
ClockCircleOutlined ,
@@ -50,6 +50,11 @@ export default function CollaborationPage(props: CollaborationProps) {
50
50
) ;
51
51
const [ currentUser , setCurrentUser ] = useState < string | undefined > ( undefined ) ;
52
52
const [ matchedUser , setMatchedUser ] = useState < string | undefined > ( undefined ) ;
53
+ const [ sessionDuration , setSessionDuration ] = useState < number > ( ( ) => {
54
+ const storedTime = localStorage . getItem ( "session-duration" ) ;
55
+ return storedTime ? parseInt ( storedTime ) : 0 ;
56
+ } ) ; // State for count-up timer (TODO: currently using localstorage to store time, change to db stored time in the future)
57
+ const stopwatchRef = useRef < NodeJS . Timeout | null > ( null ) ;
53
58
54
59
// Chat states
55
60
const [ messageToSend , setMessageToSend ] = useState < string | undefined > (
@@ -61,8 +66,42 @@ export default function CollaborationPage(props: CollaborationProps) {
61
66
undefined
62
67
) ;
63
68
64
- // Retrieve the docRefId from query params during page navigation
65
- // const searchParams = useSearchParams();
69
+ // Stops the session duration stopwatch
70
+ const stopStopwatch = ( ) => {
71
+ if ( stopwatchRef . current ) {
72
+ clearInterval ( stopwatchRef . current ) ;
73
+ }
74
+ } ;
75
+
76
+ // Starts the session duration stopwatch
77
+ const startStopwatch = ( ) => {
78
+ if ( stopwatchRef . current ) {
79
+ clearInterval ( stopwatchRef . current ) ;
80
+ }
81
+
82
+ stopwatchRef . current = setInterval ( ( ) => {
83
+ setSessionDuration ( ( prevTime ) => {
84
+ const newTime = prevTime + 1 ;
85
+ localStorage . setItem ( "session-duration" , newTime . toString ( ) ) ;
86
+ return newTime ;
87
+ } ) ;
88
+ } , 1000 ) ;
89
+ } ;
90
+
91
+ // Convert seconds into time of format "hh:mm:ss"
92
+ const formatTime = ( seconds : number ) => {
93
+ const hours = Math . floor ( seconds / 3600 ) ;
94
+ const minutes = Math . floor ( ( seconds % 3600 ) / 60 ) ;
95
+ const secs = seconds % 60 ;
96
+
97
+ return (
98
+ ( hours > 9 ? hours : "0" + hours ) +
99
+ ":" +
100
+ ( minutes > 9 ? minutes : "0" + minutes ) +
101
+ ":" +
102
+ ( secs > 9 ? secs : "0" + secs )
103
+ ) ;
104
+ } ;
66
105
67
106
// Fetch the question on initialisation
68
107
useEffect ( ( ) => {
@@ -81,14 +120,19 @@ export default function CollaborationPage(props: CollaborationProps) {
81
120
setMatchedUser ( matchedUser ) ;
82
121
setCurrentUser ( currentUser ) ;
83
122
123
+ // Fetch question and set question states
84
124
GetSingleQuestion ( docRefId ) . then ( ( data : Question ) => {
85
125
setQuestionTitle ( `${ data . id } . ${ data . title } ` ) ;
86
126
setComplexity ( data . complexity ) ;
87
127
setCategories ( data . categories ) ;
88
128
setDescription ( data . description ) ;
89
129
} ) ;
130
+
131
+ // Start stopwatch
132
+ startStopwatch ( ) ;
90
133
} , [ ] ) ;
91
134
135
+ // Tabs component items for testcases
92
136
const items : TabsProps [ "items" ] = [
93
137
{
94
138
key : "1" ,
@@ -117,15 +161,21 @@ export default function CollaborationPage(props: CollaborationProps) {
117
161
} ,
118
162
] ;
119
163
164
+ // Handles the cleaning of localstorage variables, stopping the timer & signalling collab user on webrtc
120
165
const handleCloseCollaboration = ( ) => {
121
- // Remove localstorage variables for collaboration
122
- localStorage . removeItem ( "user" ) ;
123
- localStorage . removeItem ( "matchedUser" ) ;
124
- localStorage . removeItem ( "collaId" ) ;
125
- localStorage . removeItem ( "docRefId" ) ;
166
+ // Stop stopwatch
167
+ stopStopwatch ( ) ;
168
+ // Remove localstorage variable for stored session duration
169
+ localStorage . removeItem ( "session-duration" ) ; // TODO: Remove this after collaboration backend data stored
126
170
127
- // Redirect back to matching page
128
- router . push ( "/matching" ) ;
171
+ // // Remove localstorage variables for collaboration
172
+ // localStorage.removeItem("user");
173
+ // localStorage.removeItem("matchedUser");
174
+ // localStorage.removeItem("collaId");
175
+ // localStorage.removeItem("docRefId");
176
+
177
+ // // Redirect back to matching page
178
+ // router.push("/matching");
129
179
} ;
130
180
131
181
return (
@@ -170,7 +220,11 @@ export default function CollaborationPage(props: CollaborationProps) {
170
220
Test Cases
171
221
</ div >
172
222
{ /* TODO: Link to execution service for running code against test-cases */ }
173
- < Button icon = { < PlayCircleOutlined /> } iconPosition = "end" >
223
+ < Button
224
+ icon = { < PlayCircleOutlined /> }
225
+ iconPosition = "end"
226
+ className = "test-case-button"
227
+ >
174
228
Run Test Cases
175
229
</ Button >
176
230
</ div >
@@ -189,7 +243,11 @@ export default function CollaborationPage(props: CollaborationProps) {
189
243
Code
190
244
</ div >
191
245
{ /* TODO: Link to execution service for code submission */ }
192
- < Button icon = { < SendOutlined /> } iconPosition = "end" >
246
+ < Button
247
+ icon = { < SendOutlined /> }
248
+ iconPosition = "end"
249
+ className = "code-submit-button"
250
+ >
193
251
Submit
194
252
</ Button >
195
253
</ div >
@@ -221,15 +279,20 @@ export default function CollaborationPage(props: CollaborationProps) {
221
279
Session Details
222
280
</ div >
223
281
{ /* TODO: End the collaboration session, cleanup the localstorage variables */ }
224
- < Button danger onClick = { handleCloseCollaboration } >
282
+ < Button
283
+ danger
284
+ onClick = { handleCloseCollaboration }
285
+ className = "session-end-button"
286
+ >
225
287
End
226
288
</ Button >
227
289
</ div >
228
290
229
291
< div className = "session-duration" >
230
292
Duration:
231
- { /* TODO: Implement a count-up timer for session duration */ }
232
- < text className = "session-duration-timer" > 00:00:00</ text >
293
+ < text className = "session-duration-timer" >
294
+ { formatTime ( sessionDuration ) }
295
+ </ text >
233
296
</ div >
234
297
< div className = "session-matched-user-label" >
235
298
Matched User:
0 commit comments