1
1
import React , { useState , useCallback , useEffect , useRef } from "react" ;
2
- import { CodeEditorHandle } from "../../presentation/components/CodeEditor/CodeEditor" ;
3
2
import styles from "./CollaborationRoomPage.module.css" ;
4
3
import CodeEditor from "../../presentation/components/CodeEditor/CodeEditor" ;
5
4
import { QuestionDetail } from "../../presentation/components/QuestionDetail" ;
@@ -16,7 +15,6 @@ import { Room } from "../../domain/entities/Room";
16
15
import { useAuth } from "../../domain/context/AuthContext" ;
17
16
import { Spin } from "antd" ;
18
17
import { toast } from "react-toastify" ;
19
- import { historyUseCases } from "domain/usecases/HistoryUseCases" ;
20
18
21
19
const CollaborationRoomPage : React . FC = ( ) => {
22
20
const location = useLocation ( ) ;
@@ -25,14 +23,11 @@ const CollaborationRoomPage: React.FC = () => {
25
23
26
24
// State Definitions
27
25
const { urlRoomId } = useParams < { urlRoomId : string } > ( ) ;
28
- const [ hasUserConfirmedLeave , setHasUserConfirmedLeave ] = useState ( false ) ;
29
- const [ shouldSaveOnLeave , setShouldSaveOnLeave ] = useState ( true ) ;
30
- const [ room , setRoom ] = useState < Room > ( ) ;
26
+ const [ room , setRoom ] = useState < Room | null > ( null ) ;
31
27
const [ question , setQuestion ] = useState < Question | undefined > ( undefined ) ;
32
28
const [ showChat , setShowChat ] = useState ( false ) ;
33
29
const [ loading , setLoading ] = useState < boolean > ( true ) ;
34
30
const [ error , setError ] = useState < string | null > ( null ) ;
35
- const codeEditorRef = useRef < CodeEditorHandle > ( null ) ;
36
31
37
32
// Extract details from location.state if available
38
33
const { roomId, attemptStartedAt, matchUserId, questionId } = locationState || { } ;
@@ -94,65 +89,6 @@ const CollaborationRoomPage: React.FC = () => {
94
89
fetchQuestion ( ) ;
95
90
} , [ room ] ) ;
96
91
97
- useEffect ( ( ) => {
98
-
99
- const saveAttempt = async ( ) => {
100
- if ( hasUserConfirmedLeave ) {
101
- if ( ! shouldSaveOnLeave ) return ; // User chose not to save
102
- }
103
-
104
- // Attempt to save
105
- try {
106
- if ( ! room ) return ;
107
- await historyUseCases . createOrUpdateUserHistory (
108
- room . questionId ,
109
- room . roomId ,
110
- new Date ( room . attemptStartedAt ) . getTime ( ) . toString ( ) ,
111
- Date . now ( ) . toString ( ) ,
112
- room . userIdTwo ?. _id ,
113
- codeEditorRef . current ?. getEditorText ( ) || "" ,
114
- ) ;
115
- console . log ( "Attempt saved successfully." ) ;
116
- } catch ( error ) {
117
- console . error ( "Failed to save attempt on unmount:" , error ) ;
118
- }
119
- } ;
120
-
121
- return ( ) => {
122
- saveAttempt ( ) ;
123
- } ;
124
- } , [ hasUserConfirmedLeave , room , shouldSaveOnLeave ] ) ;
125
-
126
- useEffect ( ( ) => {
127
-
128
- const saveAttemptAsync = async ( ) => {
129
- if ( ! room ) return ;
130
- if ( hasUserConfirmedLeave ) {
131
- if ( ! shouldSaveOnLeave ) return ;
132
- }
133
-
134
- const editorContent = codeEditorRef . current ?. getEditorText ( ) || "" ;
135
- await historyUseCases . createOrUpdateUserHistory (
136
- room . questionId ,
137
- room . roomId ,
138
- new Date ( room . attemptStartedAt ) . getTime ( ) . toString ( ) ,
139
- Date . now ( ) . toString ( ) ,
140
- room . userIdTwo ?. _id ,
141
- editorContent ,
142
- ) ;
143
- } ;
144
-
145
- const handleBeforeUnload = async ( event : BeforeUnloadEvent ) => {
146
- await saveAttemptAsync ( ) ;
147
- } ;
148
-
149
- window . addEventListener ( 'beforeunload' , handleBeforeUnload ) ;
150
-
151
- return ( ) => {
152
- window . removeEventListener ( 'beforeunload' , handleBeforeUnload ) ;
153
- } ;
154
- } , [ hasUserConfirmedLeave , room , shouldSaveOnLeave ] ) ;
155
-
156
92
// Resizable Layout Configurations
157
93
const { position : questionPosition , separatorProps : verticalSeparatorProps } = useResizable ( {
158
94
axis : "x" ,
@@ -198,15 +134,10 @@ const CollaborationRoomPage: React.FC = () => {
198
134
< div className = { styles . editorAndOutputContainer } >
199
135
< div className = { styles . editorContainer } >
200
136
< CodeEditor
201
- ref = { codeEditorRef }
202
137
questionId = { room . questionId }
203
138
roomId = { room . roomId }
204
139
attemptStartedAt = { new Date ( room . attemptStartedAt ) }
205
140
collaboratorId = { room . userIdTwo ?. _id }
206
- onUserConfirmedLeave = { ( shouldSave : boolean ) => {
207
- setHasUserConfirmedLeave ( true ) ;
208
- setShouldSaveOnLeave ( shouldSave ) ;
209
- } }
210
141
/>
211
142
</ div >
212
143
0 commit comments