Skip to content

Commit 05569ac

Browse files
committed
Revert Attempt at Using ImperativeHandle
1 parent d5c32ff commit 05569ac

File tree

3 files changed

+6
-91
lines changed

3 files changed

+6
-91
lines changed

frontend/src/presentation/components/CodeEditor/CodeEditor.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, useRef, useImperativeHandle, forwardRef } from "react";
1+
import React, { useState, useEffect, useRef } from "react";
22
import styles from "./CodeEditor.module.css";
33
import Editor from "@monaco-editor/react";
44
import { Button, Spin, Modal } from "antd";
@@ -14,10 +14,6 @@ interface CodeEditorProps {
1414
roomId: string;
1515
attemptStartedAt: Date;
1616
collaboratorId: string;
17-
onUserConfirmedLeave: (shouldSave: boolean) => void; // New prop
18-
}
19-
export interface CodeEditorHandle {
20-
getEditorText: () => string;
2117
}
2218

2319
function usePrevious<T>(value: T): T | undefined {
@@ -28,25 +24,19 @@ function usePrevious<T>(value: T): T | undefined {
2824
return ref.current;
2925
}
3026

31-
const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(({
27+
const CodeEditor: React.FC<CodeEditorProps> = ({
3228
questionId,
3329
roomId,
3430
attemptStartedAt,
35-
collaboratorId,
36-
onUserConfirmedLeave,
37-
}, ref) => {
31+
collaboratorId
32+
}) => {
3833
const { onEditorIsMounted, isExecuting, setRoomId, connectedUsers } = useCollaboration();
3934
const [theme, setTheme] = useState("vs-light");
4035
const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null);
4136
const containerRef = useRef<HTMLDivElement>(null);
4237
const [isModalVisible, setIsModalVisible] = useState(false);
4338
const [leftUser, setLeftUser] = useState<string | null>(null);
4439

45-
// Expose the getEditorText method using useImperativeHandle
46-
useImperativeHandle(ref, () => ({
47-
getEditorText: () => editorRef.current?.getValue() || "",
48-
}));
49-
5040
const prevConnectedUsers = usePrevious(connectedUsers);
5141

5242
useEffect(() => {
@@ -161,7 +151,6 @@ const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(({
161151
roomId={roomId}
162152
attemptStartedAt={attemptStartedAt}
163153
collaboratorId={collaboratorId}
164-
onUserConfirmedLeave={onUserConfirmedLeave}
165154
/>
166155
</div>
167156
</div>
@@ -179,6 +168,6 @@ const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(({
179168
</div>
180169
</div>
181170
);
182-
});
171+
};
183172

184173
export default CodeEditor;

frontend/src/presentation/components/CodeEditor/LeaveButton.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ interface LeaveButtonProps {
1111
roomId: string;
1212
attemptStartedAt: Date;
1313
collaboratorId: string;
14-
onUserConfirmedLeave: (shouldSave: boolean) => void;
1514
}
1615

1716
const LeaveButton: React.FC<LeaveButtonProps> = ({
@@ -20,7 +19,6 @@ const LeaveButton: React.FC<LeaveButtonProps> = ({
2019
roomId,
2120
attemptStartedAt,
2221
collaboratorId,
23-
onUserConfirmedLeave,
2422
}) => {
2523
const navigate = useNavigate();
2624
const [isModalVisible, setIsModalVisible] = useState(false);
@@ -36,10 +34,8 @@ const LeaveButton: React.FC<LeaveButtonProps> = ({
3634
setIsModalVisible(false);
3735
};
3836

39-
4037
const handleLeaveWithoutSaving = () => {
4138
setIsModalVisible(false);
42-
onUserConfirmedLeave(false);
4339
navigate('/');
4440
};
4541

@@ -54,7 +50,6 @@ const LeaveButton: React.FC<LeaveButtonProps> = ({
5450
getEditorText(),
5551
);
5652
message.success("Your work has been saved successfully.");
57-
onUserConfirmedLeave(false);
5853
navigate('/');
5954
} catch (error) {
6055
if (error instanceof Error) {

frontend/src/presentation/pages/CollaborationRoomPage.tsx

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useState, useCallback, useEffect, useRef } from "react";
2-
import { CodeEditorHandle } from "../../presentation/components/CodeEditor/CodeEditor";
32
import styles from "./CollaborationRoomPage.module.css";
43
import CodeEditor from "../../presentation/components/CodeEditor/CodeEditor";
54
import { QuestionDetail } from "../../presentation/components/QuestionDetail";
@@ -16,7 +15,6 @@ import { Room } from "../../domain/entities/Room";
1615
import { useAuth } from "../../domain/context/AuthContext";
1716
import { Spin } from "antd";
1817
import { toast } from "react-toastify";
19-
import { historyUseCases } from "domain/usecases/HistoryUseCases";
2018

2119
const CollaborationRoomPage: React.FC = () => {
2220
const location = useLocation();
@@ -25,14 +23,11 @@ const CollaborationRoomPage: React.FC = () => {
2523

2624
// State Definitions
2725
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);
3127
const [question, setQuestion] = useState<Question | undefined>(undefined);
3228
const [showChat, setShowChat] = useState(false);
3329
const [loading, setLoading] = useState<boolean>(true);
3430
const [error, setError] = useState<string | null>(null);
35-
const codeEditorRef = useRef<CodeEditorHandle>(null);
3631

3732
// Extract details from location.state if available
3833
const { roomId, attemptStartedAt, matchUserId, questionId } = locationState || {};
@@ -94,65 +89,6 @@ const CollaborationRoomPage: React.FC = () => {
9489
fetchQuestion();
9590
}, [room]);
9691

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-
15692
// Resizable Layout Configurations
15793
const { position: questionPosition, separatorProps: verticalSeparatorProps } = useResizable({
15894
axis: "x",
@@ -198,15 +134,10 @@ const CollaborationRoomPage: React.FC = () => {
198134
<div className={styles.editorAndOutputContainer}>
199135
<div className={styles.editorContainer}>
200136
<CodeEditor
201-
ref={codeEditorRef}
202137
questionId={room.questionId}
203138
roomId={room.roomId}
204139
attemptStartedAt={new Date(room.attemptStartedAt)}
205140
collaboratorId={room.userIdTwo?._id}
206-
onUserConfirmedLeave={(shouldSave: boolean) => {
207-
setHasUserConfirmedLeave(true);
208-
setShouldSaveOnLeave(shouldSave);
209-
}}
210141
/>
211142
</div>
212143

0 commit comments

Comments
 (0)