1
1
/* eslint-disable react-refresh/only-export-components */
2
2
3
3
import { createContext , useContext , useState } from "react" ;
4
- import { useNavigate } from "react-router-dom" ;
5
4
import { matchSocket } from "../utils/matchSocket" ;
6
5
import { minMatchTimeout , USE_AUTH_ERROR_MESSAGE } from "../utils/constants" ;
7
6
import { useAuth } from "./AuthContext" ;
8
7
import { toast } from "react-toastify" ;
8
+ import { useAppNavigate } from "../components/NoDirectAccessRoutes" ;
9
9
10
10
type MatchUser = {
11
11
id : string ;
@@ -37,7 +37,6 @@ enum MatchEvents {
37
37
}
38
38
39
39
type MatchContextType = {
40
- closeConnection : ( path : string ) => void ;
41
40
findMatch : (
42
41
complexities : string [ ] ,
43
42
categories : string [ ] ,
@@ -47,18 +46,17 @@ type MatchContextType = {
47
46
retryMatch : ( ) => void ;
48
47
acceptMatch : ( ) => void ;
49
48
rematch : ( ) => void ;
50
- stopMatch : ( ) => void ;
49
+ stopMatch : ( path : string ) => void ;
51
50
matchUser : MatchUser | null ;
52
51
matchCriteria : MatchCriteria ;
53
- matchId : string | null ;
54
52
partner : MatchUser | null ;
55
53
} ;
56
54
57
55
const MatchContext = createContext < MatchContextType | null > ( null ) ;
58
56
59
57
const MatchProvider : React . FC < { children ?: React . ReactNode } > = ( props ) => {
60
58
const { children } = props ;
61
- const navigate = useNavigate ( ) ;
59
+ const appNavigate = useAppNavigate ( ) ;
62
60
63
61
const auth = useAuth ( ) ;
64
62
if ( ! auth ) {
@@ -84,18 +82,9 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
84
82
const [ matchId , setMatchId ] = useState < string | null > ( null ) ;
85
83
const [ partner , setPartner ] = useState < MatchUser | null > ( null ) ;
86
84
87
- const closeConnection = ( path : string ) => {
85
+ const closeConnection = ( ) => {
88
86
matchSocket . removeAllListeners ( ) ;
89
87
matchSocket . disconnect ( ) ;
90
- setMatchCriteria ( {
91
- complexities : [ ] ,
92
- categories : [ ] ,
93
- languages : [ ] ,
94
- timeout : minMatchTimeout ,
95
- } ) ;
96
- setMatchId ( null ) ;
97
- setPartner ( null ) ;
98
- navigate ( path , { replace : true } ) ;
99
88
} ;
100
89
101
90
const openConnection = ( ) => {
@@ -108,6 +97,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
108
97
matchSocket . on ( MatchEvents . MATCH_FOUND , ( { matchId, user1, user2 } ) => {
109
98
setMatchId ( matchId ) ;
110
99
matchUser ?. id === user1 . id ? setPartner ( user2 ) : setPartner ( user1 ) ;
100
+ appNavigate ( "/matching/matched" ) ;
111
101
} ) ;
112
102
}
113
103
@@ -119,20 +109,20 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
119
109
120
110
if ( ! matchSocket . hasListeners ( MatchEvents . MATCH_SUCCESSFUL ) ) {
121
111
matchSocket . on ( MatchEvents . MATCH_SUCCESSFUL , ( ) => {
122
- navigate ( "/collaboration" , { replace : true } ) ;
112
+ appNavigate ( "/collaboration" ) ;
123
113
} ) ;
124
114
}
125
115
126
116
if ( ! matchSocket . hasListeners ( MatchEvents . MATCH_UNSUCCESSFUL ) ) {
127
117
matchSocket . on ( MatchEvents . MATCH_UNSUCCESSFUL , ( ) => {
128
118
toast . error ( "Matching unsuccessful!" ) ;
129
- closeConnection ( "/home" ) ;
119
+ stopMatch ( "/home" ) ;
130
120
} ) ;
131
121
}
132
122
133
123
if ( ! matchSocket . hasListeners ( MatchEvents . MATCH_REQUEST_ERROR ) ) {
134
124
matchSocket . on ( MatchEvents . MATCH_REQUEST_ERROR , ( ) => {
135
- toast . error ( "Error sending match request! Please try again later." ) ;
125
+ toast . error ( "Failed to send match request! Please try again later." ) ;
136
126
} ) ;
137
127
}
138
128
@@ -153,7 +143,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
153
143
154
144
if ( ! matchSocket . io . hasListeners ( MatchEvents . SOCKET_RECONNECT_FAILED ) ) {
155
145
matchSocket . io . on ( MatchEvents . SOCKET_RECONNECT_FAILED , ( ) => {
156
- console . log ( "Oops, something went wrong ! Please try again later.") ;
146
+ toast . error ( "Failed to reconnect ! Please try again later.") ;
157
147
} ) ;
158
148
}
159
149
} ;
@@ -182,7 +172,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
182
172
languages,
183
173
timeout,
184
174
} ) ;
185
- navigate ( "/matching" , { replace : true } ) ;
175
+ appNavigate ( "/matching" ) ;
186
176
}
187
177
}
188
178
) ;
@@ -213,25 +203,32 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
213
203
214
204
setMatchId ( null ) ;
215
205
setPartner ( null ) ;
216
- navigate ( "/matching" , { replace : true } ) ;
206
+ appNavigate ( "/matching" ) ;
217
207
} ;
218
208
219
- const stopMatch = ( ) => {
220
- closeConnection ( "/home" ) ;
209
+ const stopMatch = ( path : string ) => {
210
+ closeConnection ( ) ;
211
+ setMatchCriteria ( {
212
+ complexities : [ ] ,
213
+ categories : [ ] ,
214
+ languages : [ ] ,
215
+ timeout : minMatchTimeout ,
216
+ } ) ;
217
+ setMatchId ( null ) ;
218
+ setPartner ( null ) ;
219
+ appNavigate ( path ) ;
221
220
} ;
222
221
223
222
return (
224
223
< MatchContext . Provider
225
224
value = { {
226
- closeConnection,
227
225
findMatch,
228
226
retryMatch,
229
227
acceptMatch,
230
228
rematch,
231
229
stopMatch,
232
230
matchUser,
233
231
matchCriteria,
234
- matchId,
235
232
partner,
236
233
} }
237
234
>
0 commit comments