Skip to content

Commit 1356a66

Browse files
committed
Shift end session logic to collab context
1 parent bc7b1b0 commit 1356a66

File tree

7 files changed

+37
-46
lines changed

7 files changed

+37
-46
lines changed

backend/collab-service/src/handlers/websocketHandler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ enum CollabEvents {
1818
DOCUMENT_READY = "document_ready",
1919
UPDATE = "updateV2",
2020
UPDATE_CURSOR = "update_cursor",
21-
// PARTNER_LEFT = "partner_left",
22-
// PARTNER_DISCONNECTED = "partner_disconnected",
21+
PARTNER_LEFT = "partner_left",
2322
}
2423

2524
const EXPIRY_TIME = 3600;
@@ -179,5 +178,7 @@ const handleUserLeave = (uid: string, roomId: string, socket: Socket) => {
179178
const room = io.sockets.adapter.rooms.get(roomId);
180179
if (!room || room.size === 0) {
181180
removeCollabSession(roomId);
181+
} else {
182+
io.to(roomId).emit(CollabEvents.PARTNER_LEFT);
182183
}
183184
};

backend/matching-service/src/handlers/websocketHandler.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ enum MatchEvents {
3232
MATCH_FOUND = "match_found",
3333
MATCH_SUCCESSFUL = "match_successful",
3434
MATCH_UNSUCCESSFUL = "match_unsuccessful",
35-
MATCH_ENDED = "match_ended",
3635
MATCH_REQUEST_EXISTS = "match_request_exists",
3736
MATCH_REQUEST_ERROR = "match_request_error",
3837
}
@@ -199,10 +198,7 @@ export const handleWebsocketMatchEvents = (socket: Socket) => {
199198

200199
socket.on(MatchEvents.MATCH_END_REQUEST, (uid: string, matchId: string) => {
201200
userConnections.delete(uid);
202-
const matchDeleted = handleMatchDelete(matchId);
203-
if (matchDeleted) {
204-
socket.to(matchId).emit(MatchEvents.MATCH_ENDED);
205-
}
201+
handleMatchDelete(matchId);
206202
});
207203

208204
socket.on(
@@ -257,7 +253,6 @@ const endMatchOnUserDisconnect = (socket: Socket, uid: string) => {
257253
const matchDeleted = handleMatchDelete(matchId);
258254
if (matchDeleted) {
259255
socket.to(matchId).emit(MatchEvents.MATCH_UNSUCCESSFUL); // on matching page
260-
socket.to(matchId).emit(MatchEvents.MATCH_ENDED); // on collab page
261256
}
262257
}
263258
};

frontend/src/contexts/CollabContext.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
FAILED_TESTCASE_MESSAGE,
77
SUCCESS_TESTCASE_MESSAGE,
88
FAILED_TO_SUBMIT_CODE_MESSAGE,
9+
COLLAB_ENDED_MESSAGE,
910
} from "../utils/constants";
1011
import { toast } from "react-toastify";
1112

@@ -14,9 +15,10 @@ import { codeExecutionClient } from "../utils/api";
1415
import { useReducer } from "react";
1516
import { updateQnHistoryById } from "../reducers/qnHistoryReducer";
1617
import qnHistoryReducer, { initialQHState } from "../reducers/qnHistoryReducer";
17-
import { leave } from "../utils/collabSocket";
18+
import { CollabEvents, collabSocket, leave } from "../utils/collabSocket";
1819
import { CommunicationEvents } from "../components/Chat";
1920
import { communicationSocket } from "../utils/communicationSocket";
21+
import useAppNavigate from "../components/UseAppNavigate";
2022

2123
type CompilerResult = {
2224
status: string;
@@ -35,14 +37,17 @@ type CollabContextType = {
3537
handleEndSessionClick: () => void;
3638
handleRejectEndSession: () => void;
3739
handleConfirmEndSession: () => void;
40+
checkPartnerStatus: () => void;
3841
setCode: React.Dispatch<React.SetStateAction<string>>;
3942
compilerResult: CompilerResult[];
43+
isEndSessionModalOpen: boolean;
4044
};
4145

4246
const CollabContext = createContext<CollabContextType | null>(null);
4347

4448
const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
4549
const { children } = props;
50+
const appNavigate = useAppNavigate();
4651

4752
const match = useMatch();
4853

@@ -58,7 +63,6 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
5863
stopMatch,
5964
questionId,
6065
qnHistoryId,
61-
setIsEndSessionModalOpen,
6266
} = match;
6367

6468
// eslint-disable-next-line
@@ -68,6 +72,8 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
6872
);
6973
const [code, setCode] = useState<string>("");
7074
const [compilerResult, setCompilerResult] = useState<CompilerResult[]>([]);
75+
const [isEndSessionModalOpen, setIsEndSessionModalOpen] =
76+
useState<boolean>(false);
7177

7278
const handleSubmitSessionClick = async (time: number) => {
7379
try {
@@ -135,8 +141,18 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
135141
partner?.username
136142
);
137143

138-
// End match
144+
// Delete match data
139145
stopMatch();
146+
appNavigate("/home");
147+
};
148+
149+
const checkPartnerStatus = () => {
150+
collabSocket.on(CollabEvents.PARTNER_LEFT, () => {
151+
toast.error(COLLAB_ENDED_MESSAGE);
152+
setIsEndSessionModalOpen(false);
153+
stopMatch();
154+
appNavigate("/home");
155+
});
140156
};
141157

142158
return (
@@ -146,8 +162,10 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
146162
handleEndSessionClick,
147163
handleRejectEndSession,
148164
handleConfirmEndSession,
165+
checkPartnerStatus,
149166
setCode,
150167
compilerResult,
168+
isEndSessionModalOpen,
151169
}}
152170
>
153171
{children}

frontend/src/contexts/MatchContext.tsx

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
ABORT_MATCH_PROCESS_CONFIRMATION_MESSAGE,
77
FAILED_MATCH_REQUEST_MESSAGE,
88
MATCH_CONNECTION_ERROR,
9-
MATCH_ENDED_MESSAGE,
109
MATCH_LOGIN_REQUIRED_MESSAGE,
1110
MATCH_REQUEST_EXISTS_MESSAGE,
1211
MATCH_UNSUCCESSFUL_MESSAGE,
@@ -18,9 +17,6 @@ import useAppNavigate from "../components/UseAppNavigate";
1817
import { UNSAFE_NavigationContext } from "react-router-dom";
1918
import { Action, type History, type Transition } from "history";
2019

21-
import { useReducer } from "react";
22-
import qnHistoryReducer, { initialQHState } from "../reducers/qnHistoryReducer";
23-
2420
let matchUserId: string;
2521
let partnerUserId: string;
2622

@@ -54,7 +50,6 @@ enum MatchEvents {
5450
MATCH_FOUND = "match_found",
5551
MATCH_SUCCESSFUL = "match_successful",
5652
MATCH_UNSUCCESSFUL = "match_unsuccessful",
57-
MATCH_ENDED = "match_ended",
5853
MATCH_REQUEST_EXISTS = "match_request_exists",
5954
MATCH_REQUEST_ERROR = "match_request_error",
6055

@@ -93,8 +88,6 @@ type MatchContextType = {
9388
partner: MatchUser | null;
9489
matchPending: boolean;
9590
loading: boolean;
96-
isEndSessionModalOpen: boolean;
97-
setIsEndSessionModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
9891
questionId: string | null;
9992
qnHistoryId: string | null;
10093
};
@@ -124,15 +117,6 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
124117
const [questionId, setQuestionId] = useState<string | null>(null);
125118
const [qnHistoryId, setQnHistoryId] = useState<string | null>(null);
126119

127-
const [isEndSessionModalOpen, setIsEndSessionModalOpen] =
128-
useState<boolean>(false);
129-
130-
// eslint-disable-next-line
131-
const [qnHistoryState, qnHistoryDispatch] = useReducer(
132-
qnHistoryReducer,
133-
initialQHState
134-
);
135-
136120
const navigator = useContext(UNSAFE_NavigationContext).navigator as History;
137121

138122
useEffect(() => {
@@ -233,9 +217,6 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
233217
case MatchPaths.MATCHED:
234218
initMatchedListeners();
235219
return;
236-
case MatchPaths.COLLAB:
237-
initCollabListeners();
238-
return;
239220
default:
240221
return;
241222
}
@@ -318,14 +299,6 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
318299
});
319300
};
320301

321-
const initCollabListeners = () => {
322-
matchSocket.on(MatchEvents.MATCH_ENDED, () => {
323-
toast.error(MATCH_ENDED_MESSAGE);
324-
setIsEndSessionModalOpen(false);
325-
appNavigate(MatchPaths.HOME);
326-
});
327-
};
328-
329302
const handleMatchFound = (
330303
matchId: string,
331304
user1: MatchUser,
@@ -409,7 +382,6 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
409382
return;
410383
case MatchPaths.COLLAB:
411384
matchSocket.emit(MatchEvents.MATCH_END_REQUEST, matchUser?.id, matchId);
412-
appNavigate(MatchPaths.HOME);
413385
return;
414386
default:
415387
return;
@@ -539,8 +511,6 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
539511
partner,
540512
matchPending,
541513
loading,
542-
isEndSessionModalOpen,
543-
setIsEndSessionModalOpen,
544514
questionId,
545515
qnHistoryId,
546516
}}

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ const CollabSandbox: React.FC = () => {
7676
partner,
7777
matchCriteria,
7878
loading,
79-
isEndSessionModalOpen,
8079
questionId,
8180
} = match;
8281

@@ -85,7 +84,12 @@ const CollabSandbox: React.FC = () => {
8584
throw new Error(USE_COLLAB_ERROR_MESSAGE);
8685
}
8786

88-
const { handleRejectEndSession, handleConfirmEndSession } = collab;
87+
const {
88+
handleRejectEndSession,
89+
handleConfirmEndSession,
90+
checkPartnerStatus,
91+
isEndSessionModalOpen,
92+
} = collab;
8993

9094
const [state, dispatch] = useReducer(reducer, initialState);
9195
const { selectedQuestion } = state;
@@ -110,6 +114,7 @@ const CollabSandbox: React.FC = () => {
110114
const editorState = await join(matchUser.id, matchId);
111115
if (editorState.ready) {
112116
setEditorState(editorState);
117+
checkPartnerStatus();
113118
} else {
114119
toast.error(COLLAB_CONNECTION_ERROR);
115120
setIsConnecting(false);

frontend/src/utils/collabSocket.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { updateCursor, Cursor } from "./collabCursor";
44
import { Doc, Text, applyUpdateV2 } from "yjs";
55
import { Awareness } from "y-protocols/awareness";
66

7-
enum CollabEvents {
7+
export enum CollabEvents {
88
// Send
99
JOIN = "join",
1010
LEAVE = "leave",
@@ -18,6 +18,7 @@ enum CollabEvents {
1818
DOCUMENT_READY = "document_ready",
1919
UPDATE = "updateV2",
2020
UPDATE_CURSOR = "update_cursor",
21+
PARTNER_LEFT = "partner_left",
2122
SOCKET_DISCONNECT = "disconnect",
2223
SOCKET_CLIENT_DISCONNECT = "io client disconnect",
2324
SOCKET_SERVER_DISCONNECT = "io server disconnect",
@@ -32,7 +33,7 @@ export type CollabSessionData = {
3233
};
3334

3435
const COLLAB_SOCKET_URL = "http://localhost:3003";
35-
const collabSocket = io(COLLAB_SOCKET_URL, {
36+
export const collabSocket = io(COLLAB_SOCKET_URL, {
3637
reconnectionAttempts: 3,
3738
autoConnect: false,
3839
});

frontend/src/utils/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export const FAILED_MATCH_REQUEST_MESSAGE =
9292
"Failed to send match request! Please try again from the home page.";
9393
export const MATCH_UNSUCCESSFUL_MESSAGE =
9494
"Unfortunately, your partner did not accept the match.";
95-
export const MATCH_ENDED_MESSAGE = "Your partner has left the match.";
9695
export const MATCH_LOGIN_REQUIRED_MESSAGE =
9796
"Please login first to find a match.";
9897
export const MATCH_OFFER_TIMEOUT_MESSAGE = "Match offer timeout!";
@@ -102,6 +101,8 @@ export const QUESTION_DOES_NOT_EXIST_ERROR =
102101
"There are no questions with the specified complexity and category. Please try another combination.";
103102

104103
// Collab
104+
export const COLLAB_ENDED_MESSAGE =
105+
"Your partner has left the collaboration session.";
105106
export const COLLAB_CONNECTION_ERROR =
106107
"Error connecting you to the collaboration session! Please try again.";
107108

0 commit comments

Comments
 (0)