Skip to content

Commit e9577ca

Browse files
fix: session end event handling
1 parent 492b3f2 commit e9577ca

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

feature-collaboration-ui/frontend/peerprep-collab/src/app/collab/page.tsx

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,21 @@ function CollabPage() {
132132
ws.onmessage = (e) => {
133133
const msg = JSON.parse(e.data);
134134
console.log("[CollabPage] Received message:", msg.type);
135+
// if (msg.type === "session:end") {
136+
// console.log("[CollabPage] Received session:end message:", msg);
137+
// document.body.style.transition = "opacity 0.4s ease";
138+
// document.body.style.opacity = "0";
139+
// setTimeout(() => {
140+
// const dashboardUrl =
141+
// process.env.NEXT_PUBLIC_DASHBOARD_URL || "http://localhost:3000/dashboard";
142+
// window.location.href = dashboardUrl;
143+
// }, 400);
144+
// }
135145
if (msg.type === "session:end") {
136146
console.log("[CollabPage] Received session:end message:", msg);
137-
document.body.style.transition = "opacity 0.4s ease";
138-
document.body.style.opacity = "0";
139-
setTimeout(() => {
140-
const dashboardUrl =
141-
process.env.NEXT_PUBLIC_DASHBOARD_URL || "http://localhost:3000/dashboard";
142-
window.location.href = dashboardUrl;
143-
}, 400);
147+
if ((window as any).__sessionEndDispatched) return;
148+
(window as any).__sessionEndDispatched = true;
149+
window.dispatchEvent(new CustomEvent("session-end", { detail: msg }));
144150
}
145151
};
146152

@@ -214,6 +220,27 @@ function CollabPage() {
214220
})();
215221
}, [sessionId, paramUserId]);
216222

223+
// handle session end event
224+
useEffect(() => {
225+
const onSessionEnd = () => {
226+
if ((window as any).__sessionEndHandled) return;
227+
(window as any).__sessionEndHandled = true;
228+
229+
alert("Your partner has ended the session. Returning to dashboard...");
230+
document.body.style.transition = "opacity 0.4s ease";
231+
document.body.style.opacity = "0";
232+
233+
setTimeout(() => {
234+
const dashboardUrl =
235+
process.env.NEXT_PUBLIC_DASHBOARD_URL || "http://localhost:3000/dashboard";
236+
window.location.href = dashboardUrl;
237+
}, 400);
238+
};
239+
240+
window.addEventListener("session-end", onSessionEnd);
241+
return () => window.removeEventListener("session-end", onSessionEnd);
242+
}, []);
243+
217244

218245
// Show loading state during hydration
219246
if (!isClient) {

feature-collaboration-ui/frontend/peerprep-collab/src/components/collab/ChatPane.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,18 @@ export default function ChatPane() {
4141
setIsAILoading(false);
4242
}
4343

44-
// Handle session end
44+
// // Handle session end
45+
// if (msg.type === "session:end") {
46+
// console.log("[ChatPane] Session ended, broadcasting to page");
47+
// // Trigger session end event that the collab page listens to
48+
// window.dispatchEvent(new CustomEvent('session-end', { detail: msg }));
49+
// }
50+
51+
// Handle session end (dispatch only, no alert/redirect)
4552
if (msg.type === "session:end") {
46-
console.log("[ChatPane] Session ended, broadcasting to page");
47-
// Trigger session end event that the collab page listens to
48-
window.dispatchEvent(new CustomEvent('session-end', { detail: msg }));
53+
if ((window as any).__sessionEndDispatched) return;
54+
(window as any).__sessionEndDispatched = true;
55+
window.dispatchEvent(new CustomEvent("session-end", { detail: msg }));
4956
}
5057

5158
// Initialize chat history
@@ -60,11 +67,11 @@ export default function ChatPane() {
6067
}
6168
}
6269

63-
if (msg.type === "session:end") {
64-
alert("Your partner has ended the session. Returning to dashboard...");
65-
const dashboardUrl = process.env.NEXT_PUBLIC_DASHBOARD_URL || "http://localhost:3000/dashboard";
66-
window.location.href = dashboardUrl;
67-
}
70+
// if (msg.type === "session:end") {
71+
// alert("Your partner has ended the session. Returning to dashboard...");
72+
// const dashboardUrl = process.env.NEXT_PUBLIC_DASHBOARD_URL || "http://localhost:3000/dashboard";
73+
// window.location.href = dashboardUrl;
74+
// }
6875
}
6976

7077
// Initialize WebSocket

feature-collaboration-ui/frontend/peerprep-collab/src/components/collab/CodePane.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,15 @@ export default function CodePane({ question }: { question: any }) {
219219
if (typeof evt.data === "string") {
220220
try {
221221
const msg = JSON.parse(evt.data);
222+
// if (msg.type === "session:end") {
223+
// console.log("[CodePane] Received session:end via YJS WebSocket");
224+
// window.dispatchEvent(new CustomEvent('session-end', { detail: msg }));
225+
// }
222226
if (msg.type === "session:end") {
223227
console.log("[CodePane] Received session:end via YJS WebSocket");
224-
window.dispatchEvent(new CustomEvent('session-end', { detail: msg }));
228+
if ((window as any).__sessionEndDispatched) return;
229+
(window as any).__sessionEndDispatched = true;
230+
window.dispatchEvent(new CustomEvent("session-end", { detail: msg }));
225231
}
226232
} catch (e) {
227233
// Not JSON, ignore

0 commit comments

Comments
 (0)