Skip to content

Commit 6a05682

Browse files
committed
Fix loading code template bug and css
1 parent af11aee commit 6a05682

File tree

5 files changed

+46
-34
lines changed

5 files changed

+46
-34
lines changed

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,24 @@ export const handleWebsocketCollabEvents = (socket: Socket) => {
9292
}
9393
);
9494

95-
socket.on(CollabEvents.LEAVE, (uid: string, roomId: string) => {
96-
const connectionKey = `${uid}:${roomId}`;
97-
if (!userConnections.has(connectionKey)) {
98-
return;
99-
}
100-
101-
clearTimeout(userConnections.get(connectionKey)!);
95+
socket.on(
96+
CollabEvents.LEAVE,
97+
(uid: string, roomId: string, isImmediate: boolean) => {
98+
const connectionKey = `${uid}:${roomId}`;
99+
if (isImmediate || !userConnections.has(connectionKey)) {
100+
handleUserLeave(uid, roomId, socket);
101+
return;
102+
}
102103

103-
const connectionTimeout = setTimeout(() => {
104-
userConnections.delete(connectionKey);
105-
socket.leave(roomId);
106-
socket.disconnect();
104+
clearTimeout(userConnections.get(connectionKey)!);
107105

108-
const room = io.sockets.adapter.rooms.get(roomId);
109-
if (!room || room.size === 0) {
110-
removeCollabSession(roomId);
111-
}
112-
}, CONNECTION_DELAY);
106+
const connectionTimeout = setTimeout(() => {
107+
handleUserLeave(uid, roomId, socket);
108+
}, CONNECTION_DELAY);
113109

114-
userConnections.set(connectionKey, connectionTimeout);
115-
});
110+
userConnections.set(connectionKey, connectionTimeout);
111+
}
112+
);
116113

117114
socket.on(CollabEvents.RECONNECT_REQUEST, async (roomId: string) => {
118115
// TODO: Handle recconnection
@@ -168,3 +165,19 @@ const saveDocument = async (roomId: string, doc: Doc) => {
168165
EX: EXPIRY_TIME,
169166
});
170167
};
168+
169+
const handleUserLeave = (uid: string, roomId: string, socket: Socket) => {
170+
const connectionKey = `${uid}:${roomId}`;
171+
if (userConnections.has(connectionKey)) {
172+
clearTimeout(userConnections.get(connectionKey)!);
173+
userConnections.delete(connectionKey);
174+
}
175+
176+
socket.leave(roomId);
177+
socket.disconnect();
178+
179+
const room = io.sockets.adapter.rooms.get(roomId);
180+
if (!room || room.size === 0) {
181+
removeCollabSession(roomId);
182+
}
183+
};

frontend/src/components/CodeEditor/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const CodeEditor: React.FC<CodeEditorProps> = (props) => {
7676
return (
7777
<CodeMirror
7878
ref={onEditorReady}
79-
style={{ height: "100%", width: "100%" }}
79+
style={{ height: "100%", width: "100%", fontSize: "14px" }}
8080
height="100%"
8181
width="100%"
8282
basicSetup={false}
@@ -96,7 +96,12 @@ const CodeEditor: React.FC<CodeEditorProps> = (props) => {
9696
EditorView.editable.of(!isReadOnly && isDocumentLoaded),
9797
EditorState.readOnly.of(isReadOnly || !isDocumentLoaded),
9898
]}
99-
value={isReadOnly ? template : template ? "Loading code template..." : ""}
99+
value={isReadOnly ? template : undefined}
100+
placeholder={
101+
!isReadOnly && !isDocumentLoaded
102+
? "Loading code template..."
103+
: undefined
104+
}
100105
/>
101106
);
102107
};

frontend/src/contexts/CollabContext.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
6262
} = match;
6363

6464
// eslint-disable-next-line
65-
const [qnHistoryState, qnHistoryDispatch] = useReducer(
65+
const [_qnHistoryState, qnHistoryDispatch] = useReducer(
6666
qnHistoryReducer,
6767
initialQHState
6868
);
@@ -120,8 +120,8 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
120120
setIsEndSessionModalOpen(false);
121121

122122
// Leave collaboration room
123-
leave(matchUser?.id as string, getMatchId() as string);
124-
leave(partner?.id as string, getMatchId() as string);
123+
leave(matchUser?.id as string, getMatchId() as string, true);
124+
leave(partner?.id as string, getMatchId() as string, true);
125125

126126
// Leave chat room
127127
communicationSocket.emit(

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ const CollabSandbox: React.FC = () => {
232232
sx={(theme) => ({
233233
flex: 1,
234234
width: "100%",
235-
maxHeight: "50vh",
235+
minHeight: "44vh",
236+
maxHeight: "44vh",
236237
paddingTop: theme.spacing(2),
237238
paddingBottom: theme.spacing(2),
238239
})}
@@ -257,7 +258,7 @@ const CollabSandbox: React.FC = () => {
257258
<Box
258259
sx={{
259260
flex: 1,
260-
maxHeight: "50vh",
261+
maxHeight: "44vh",
261262
display: "flex",
262263
flexDirection: "column",
263264
}}

frontend/src/utils/collabSocket.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ export const initDocument = (uid: string, roomId: string, template: string) => {
8282
});
8383
};
8484

85-
export const leave = (uid: string, roomId: string) => {
85+
export const leave = (uid: string, roomId: string, isImmediate?: boolean) => {
8686
collabSocket.removeAllListeners();
8787
collabSocket.io.removeListener(CollabEvents.SOCKET_RECONNECT_SUCCESS);
8888
collabSocket.io.removeListener(CollabEvents.SOCKET_RECONNECT_FAILED);
89-
collabSocket.emit(CollabEvents.LEAVE, uid, roomId);
89+
collabSocket.emit(CollabEvents.LEAVE, uid, roomId, isImmediate);
9090
doc.destroy();
9191
};
9292

@@ -135,10 +135,3 @@ const initConnectionStatusListeners = (roomId: string) => {
135135
});
136136
}
137137
};
138-
139-
export const getDocumentContent = () => {
140-
if (!doc.isDestroyed) {
141-
return text.toString();
142-
}
143-
return "";
144-
};

0 commit comments

Comments
 (0)