Skip to content

Commit 3ee517c

Browse files
committed
Change connected user status to fetch using provider awareness
1 parent 77ff847 commit 3ee517c

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

apps/frontend/src/app/collaboration/[id]/page.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default function CollaborationPage(props: CollaborationProps) {
5050
undefined
5151
);
5252
const [currentUser, setCurrentUser] = useState<string | undefined>(undefined);
53-
const [matchedUser, setMatchedUser] = useState<string | undefined>(undefined);
53+
const [matchedUser, setMatchedUser] = useState<string>("Not Connected");
5454
const [sessionDuration, setSessionDuration] = useState<number>(() => {
5555
const storedTime = localStorage.getItem("session-duration");
5656
return storedTime ? parseInt(storedTime) : 0;
@@ -121,7 +121,7 @@ export default function CollaborationPage(props: CollaborationProps) {
121121

122122
// Set states from localstorage
123123
setCollaborationId(collabId);
124-
setMatchedUser(matchedUser);
124+
// setMatchedUser(matchedUser); // TODO: remove after being handled in collabeditor
125125
setCurrentUser(currentUser);
126126

127127
// Fetch question and set question states
@@ -169,13 +169,10 @@ export default function CollaborationPage(props: CollaborationProps) {
169169
const handleCloseCollaboration = () => {
170170
// Stop stopwatch
171171
stopStopwatch();
172-
// Remove localstorage variable for stored session duration
173-
localStorage.removeItem("session-duration"); // TODO: Remove this after collaboration backend data stored
174-
175172
// Remove localstorage variables for collaboration
173+
localStorage.removeItem("session-duration"); // TODO: Remove this after collaboration backend data stored
176174
localStorage.removeItem("user");
177-
localStorage.removeItem("matchedUser");
178-
localStorage.removeItem("collaId");
175+
localStorage.removeItem("collabId");
179176
localStorage.removeItem("docRefId");
180177

181178
// Redirect back to matching page
@@ -260,6 +257,7 @@ export default function CollaborationPage(props: CollaborationProps) {
260257
user={currentUser}
261258
collaborationId={collaborationId}
262259
language={selectedLanguage}
260+
setMatchedUser={setMatchedUser}
263261
/>
264262
)}
265263
</div>

apps/frontend/src/app/matching/MatchingModal.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ const MatchingModal: React.FC<MatchingModalProps> = ({
9898
matchingState.ok();
9999
setClosedType("joined");
100100
localStorage.setItem("user", matchingState.info.myName);
101-
localStorage.setItem(
102-
"matchedUser",
103-
matchingState.info.partnerName
104-
);
105101
localStorage.setItem("collabId", matchingState.info.matchId);
106102
localStorage.setItem("docRefId", matchingState.info.docRefId);
107103

apps/frontend/src/components/CollaborativeEditor/CollaborativeEditor.tsx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Referenced from example in https://www.npmjs.com/package/y-codemirror.next
2-
import React, { useEffect, useRef, useState } from "react";
2+
import React, {
3+
Dispatch,
4+
SetStateAction,
5+
useEffect,
6+
useRef,
7+
useState,
8+
} from "react";
39
import * as Y from "yjs";
410
import { yCollab } from "y-codemirror.next";
511
import { WebrtcProvider } from "y-webrtc";
@@ -19,6 +25,7 @@ interface CollaborativeEditorProps {
1925
user: string;
2026
collaborationId: string;
2127
language: string;
28+
setMatchedUser: Dispatch<SetStateAction<string>>;
2229
}
2330

2431
export const usercolors = [
@@ -148,6 +155,7 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
148155
const ydoc = new Y.Doc();
149156
const provider = new WebrtcProvider(props.collaborationId, ydoc, {
150157
signaling: [process.env.NEXT_PUBLIC_SIGNALLING_SERVICE_URL],
158+
maxConns: 2,
151159
});
152160
const ytext = ydoc.getText("codemirror");
153161
const undoManager = new Y.UndoManager(ytext);
@@ -158,6 +166,26 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
158166
colorLight: userColor.light,
159167
});
160168

169+
// Check initial awareness states
170+
const states = provider.awareness.getStates();
171+
for (const [clientID, state] of Array.from(states)) {
172+
if (state.user && state.user.name !== props.user) {
173+
props.setMatchedUser(state.user.name);
174+
break;
175+
}
176+
}
177+
178+
// Listen for awareness changes
179+
provider.awareness.on("change", () => {
180+
const updatedStates = provider.awareness.getStates();
181+
for (const [clientID, state] of Array.from(updatedStates)) {
182+
if (state.user && state.user.name !== props.user) {
183+
props.setMatchedUser(state.user.name);
184+
break;
185+
}
186+
}
187+
});
188+
161189
const state = EditorState.create({
162190
doc: ytext.toString(),
163191
extensions: [
@@ -173,11 +201,6 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
173201
parent: editorRef.current || undefined,
174202
});
175203

176-
// viewRef.current = new EditorView({
177-
// state: state,
178-
// parent: editorRef.current || undefined,
179-
// });
180-
181204
return () => {
182205
// Cleanup on component unmount
183206
console.log("unmounting collaboration editor"); // TODO: remove

0 commit comments

Comments
 (0)