6
6
FAILED_TESTCASE_MESSAGE ,
7
7
SUCCESS_TESTCASE_MESSAGE ,
8
8
FAILED_TO_SUBMIT_CODE_MESSAGE ,
9
+ COLLAB_ENDED_MESSAGE ,
9
10
} from "../utils/constants" ;
10
11
import { toast } from "react-toastify" ;
11
12
@@ -14,9 +15,10 @@ import { codeExecutionClient } from "../utils/api";
14
15
import { useReducer } from "react" ;
15
16
import { updateQnHistoryById } from "../reducers/qnHistoryReducer" ;
16
17
import qnHistoryReducer , { initialQHState } from "../reducers/qnHistoryReducer" ;
17
- import { leave } from "../utils/collabSocket" ;
18
+ import { CollabEvents , collabSocket , leave } from "../utils/collabSocket" ;
18
19
import { CommunicationEvents } from "../components/Chat" ;
19
20
import { communicationSocket } from "../utils/communicationSocket" ;
21
+ import useAppNavigate from "../components/UseAppNavigate" ;
20
22
21
23
type CompilerResult = {
22
24
status : string ;
@@ -35,14 +37,17 @@ type CollabContextType = {
35
37
handleEndSessionClick : ( ) => void ;
36
38
handleRejectEndSession : ( ) => void ;
37
39
handleConfirmEndSession : ( ) => void ;
40
+ checkPartnerStatus : ( ) => void ;
38
41
setCode : React . Dispatch < React . SetStateAction < string > > ;
39
42
compilerResult : CompilerResult [ ] ;
43
+ isEndSessionModalOpen : boolean ;
40
44
} ;
41
45
42
46
const CollabContext = createContext < CollabContextType | null > ( null ) ;
43
47
44
48
const CollabProvider : React . FC < { children ?: React . ReactNode } > = ( props ) => {
45
49
const { children } = props ;
50
+ const appNavigate = useAppNavigate ( ) ;
46
51
47
52
const match = useMatch ( ) ;
48
53
@@ -58,16 +63,17 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
58
63
stopMatch,
59
64
questionId,
60
65
qnHistoryId,
61
- setIsEndSessionModalOpen,
62
66
} = match ;
63
67
64
68
// eslint-disable-next-line
65
- const [ qnHistoryState , qnHistoryDispatch ] = useReducer (
69
+ const [ _qnHistoryState , qnHistoryDispatch ] = useReducer (
66
70
qnHistoryReducer ,
67
71
initialQHState
68
72
) ;
69
73
const [ code , setCode ] = useState < string > ( "" ) ;
70
74
const [ compilerResult , setCompilerResult ] = useState < CompilerResult [ ] > ( [ ] ) ;
75
+ const [ isEndSessionModalOpen , setIsEndSessionModalOpen ] =
76
+ useState < boolean > ( false ) ;
71
77
72
78
const handleSubmitSessionClick = async ( time : number ) => {
73
79
try {
@@ -121,8 +127,8 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
121
127
setIsEndSessionModalOpen ( false ) ;
122
128
123
129
// Leave collaboration room
124
- leave ( matchUser ?. id as string , getMatchId ( ) as string ) ;
125
- leave ( partner ?. id as string , getMatchId ( ) as string ) ;
130
+ leave ( matchUser ?. id as string , getMatchId ( ) as string , true ) ;
131
+ leave ( partner ?. id as string , getMatchId ( ) as string , true ) ;
126
132
127
133
// Leave chat room
128
134
communicationSocket . emit (
@@ -136,8 +142,18 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
136
142
partner ?. username
137
143
) ;
138
144
139
- // End match
145
+ // Delete match data
140
146
stopMatch ( ) ;
147
+ appNavigate ( "/home" ) ;
148
+ } ;
149
+
150
+ const checkPartnerStatus = ( ) => {
151
+ collabSocket . on ( CollabEvents . PARTNER_LEFT , ( ) => {
152
+ toast . error ( COLLAB_ENDED_MESSAGE ) ;
153
+ setIsEndSessionModalOpen ( false ) ;
154
+ stopMatch ( ) ;
155
+ appNavigate ( "/home" ) ;
156
+ } ) ;
141
157
} ;
142
158
143
159
return (
@@ -147,8 +163,10 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
147
163
handleEndSessionClick,
148
164
handleRejectEndSession,
149
165
handleConfirmEndSession,
166
+ checkPartnerStatus,
150
167
setCode,
151
168
compilerResult,
169
+ isEndSessionModalOpen,
152
170
} }
153
171
>
154
172
{ children }
0 commit comments