Skip to content

Commit bc7b1b0

Browse files
committed
Redirect to home if error joining collab room
1 parent 6a05682 commit bc7b1b0

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import classes from "./index.module.css";
1515
import { useCollab } from "../../contexts/CollabContext";
1616
import { useMatch } from "../../contexts/MatchContext";
1717
import {
18+
COLLAB_CONNECTION_ERROR,
1819
USE_COLLAB_ERROR_MESSAGE,
1920
USE_MATCH_ERROR_MESSAGE,
2021
} from "../../utils/constants";
2122
import { useEffect, useReducer, useState } from "react";
2223
import Loader from "../../components/Loader";
23-
import ServerError from "../../components/ServerError";
2424
import reducer, {
2525
getQuestionById,
2626
initialState,
@@ -32,6 +32,7 @@ import TabPanel from "../../components/TabPanel";
3232
import TestCase from "../../components/TestCase";
3333
import CodeEditor from "../../components/CodeEditor";
3434
import { CollabSessionData, join, leave } from "../../utils/collabSocket";
35+
import { toast } from "react-toastify";
3536

3637
// hardcode for now...
3738

@@ -58,10 +59,10 @@ const testcases: TestCase[] = [
5859
];
5960

6061
const CollabSandbox: React.FC = () => {
61-
const [showErrorScreen, setShowErrorScreen] = useState<boolean>(false);
6262
const [editorState, setEditorState] = useState<CollabSessionData | null>(
6363
null
6464
);
65+
const [isConnecting, setIsConnecting] = useState<boolean>(true);
6566

6667
const match = useMatch();
6768
if (!match) {
@@ -92,10 +93,6 @@ const CollabSandbox: React.FC = () => {
9293
const [selectedTestcase, setSelectedTestcase] = useState(0);
9394

9495
useEffect(() => {
95-
if (!partner) {
96-
return;
97-
}
98-
9996
verifyMatchStatus();
10097

10198
if (!questionId) {
@@ -114,10 +111,12 @@ const CollabSandbox: React.FC = () => {
114111
if (editorState.ready) {
115112
setEditorState(editorState);
116113
} else {
117-
setShowErrorScreen(true);
114+
toast.error(COLLAB_CONNECTION_ERROR);
115+
setIsConnecting(false);
118116
}
119117
} catch (error) {
120-
console.error("Error connecting to collab session: ", error);
118+
toast.error(COLLAB_CONNECTION_ERROR);
119+
setIsConnecting(false);
121120
}
122121
};
123122

@@ -128,37 +127,20 @@ const CollabSandbox: React.FC = () => {
128127
// eslint-disable-next-line react-hooks/exhaustive-deps
129128
}, []);
130129

131-
useEffect(() => {
132-
let timeout: number | undefined;
133-
134-
if (!selectedQuestion) {
135-
timeout = setTimeout(() => {
136-
setShowErrorScreen(true);
137-
}, 2000);
138-
} else {
139-
setShowErrorScreen(false);
140-
}
141-
142-
return () => clearTimeout(timeout);
143-
}, [selectedQuestion]);
144-
145130
if (loading) {
146131
return <Loader />;
147132
}
148133

149-
if (!matchUser || !partner || !matchCriteria || !getMatchId()) {
134+
if (
135+
!matchUser ||
136+
!partner ||
137+
!matchCriteria ||
138+
!getMatchId() ||
139+
!isConnecting
140+
) {
150141
return <Navigate to="/home" replace />;
151142
}
152143

153-
if (showErrorScreen) {
154-
return (
155-
<ServerError
156-
title="Oops, collaboration session ended..."
157-
subtitle="Unfortunately, the session has ended due to a connection loss 😥"
158-
/>
159-
);
160-
}
161-
162144
if (!selectedQuestion || !editorState) {
163145
return <Loader />;
164146
}
@@ -234,8 +216,7 @@ const CollabSandbox: React.FC = () => {
234216
width: "100%",
235217
minHeight: "44vh",
236218
maxHeight: "44vh",
237-
paddingTop: theme.spacing(2),
238-
paddingBottom: theme.spacing(2),
219+
paddingTop: theme.spacing(1),
239220
})}
240221
>
241222
<CodeEditor
@@ -256,12 +237,13 @@ const CollabSandbox: React.FC = () => {
256237
/>
257238
</Box>
258239
<Box
259-
sx={{
240+
sx={(theme) => ({
260241
flex: 1,
261242
maxHeight: "44vh",
262243
display: "flex",
263244
flexDirection: "column",
264-
}}
245+
paddingTop: theme.spacing(1),
246+
})}
265247
>
266248
<Tabs
267249
value={selectedTab}

frontend/src/utils/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ export const MATCH_CONNECTION_ERROR =
101101
export const QUESTION_DOES_NOT_EXIST_ERROR =
102102
"There are no questions with the specified complexity and category. Please try another combination.";
103103

104+
// Collab
105+
export const COLLAB_CONNECTION_ERROR =
106+
"Error connecting you to the collaboration session! Please try again.";
107+
104108
// Code execution
105109
export const FAILED_TESTCASE_MESSAGE =
106110
"Your code did not pass all the test cases.";

0 commit comments

Comments
 (0)