Skip to content

Commit 679efbb

Browse files
committed
Resolve CollabSpace conflict
1 parent 693b546 commit 679efbb

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

Frontend/src/components/collab/CollaborationSpace.jsx

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const CollaborationSpace = () => {
2525
const [provider, setProvider] = useState(null);
2626
const [code, setCode] = useState('');
2727
const [users, setUsers] = useState([]); // track users in the room
28+
const [outputLoading, setOutputLoading] = useState(false)
2829
const [userId, setUserId] = useState(""); // current user
2930
const [language, setLanguage] = useState("python") // set default language to python
3031
const [output, setOutput] = useState("")
@@ -46,6 +47,17 @@ const CollaborationSpace = () => {
4647
navigate("/home");
4748
};
4849

50+
{/* State management for user join/leave toast */}
51+
const [notifs, setNotifs] = useState([]);
52+
const addNotif = (message) => {
53+
const id = Date.now(); // unique id based on timestamp
54+
setNotifs((prevNotifs) => [...prevNotifs, {id, message}]);
55+
// Remove notif after 2 seconds
56+
setTimeout(() => {
57+
setNotifs((prevNotifs) => prevNotifs.filter((notif) => notif.id !== id))
58+
}, 1500);
59+
};
60+
4961

5062
{/* Set up websockets for room management on client side, and collaboration for Yjs */}
5163
useEffect(() => {
@@ -91,7 +103,6 @@ const CollaborationSpace = () => {
91103
// on getting a reply from server
92104
websocket.onmessage = (event) => {
93105
const data = JSON.parse(event.data);
94-
console.log(`[FRONTEND] data message is ${JSON.stringify(data)}`);
95106
switch (data.type) {
96107
case 'usersListUpdate':
97108
setUsers(data.users); // Update the user list
@@ -107,6 +118,15 @@ const CollaborationSpace = () => {
107118
console.log("adding message", data.message)
108119
setMessages((prevMessages) => [...prevMessages, data.message]);
109120
break;
121+
case 'languageChange':
122+
addNotif(`User ${data.user} has changed the language to ${data.language}`);
123+
setLanguage(data.language);
124+
break;
125+
case 'userJoin':
126+
addNotif(`User ${data.user} has joined.`)
127+
break;
128+
case 'userLeft':
129+
addNotif(`User ${data.user} has left`)
110130
default:
111131
console.log("No messages received from room management server");
112132
break;
@@ -151,6 +171,7 @@ const CollaborationSpace = () => {
151171
};
152172

153173
const handleCodeRun = () => {
174+
setOutputLoading(true);
154175
const code_message = {
155176
"language": language,
156177
"files": [
@@ -164,6 +185,7 @@ const CollaborationSpace = () => {
164185
collabService.getCodeOutput(code_message)
165186
.then(result => {
166187
console.log(result.data.run.output)
188+
setOutputLoading(false);
167189
setOutput(result.data.run.output)
168190
})
169191
.catch(err => console.log(err));
@@ -180,6 +202,11 @@ const CollaborationSpace = () => {
180202
websocketRef.current.send(JSON.stringify({ type: 'sendMessage', roomId: roomId, message: message}));
181203
}
182204

205+
const handleLanguageChange = (value) => {
206+
websocketRef.current.send(JSON.stringify({ type: 'languageChange', roomId: roomId,
207+
user: userId, language: value }));
208+
}
209+
183210
if (loading) {
184211
return (
185212
<div style={{ textAlign: 'center' }}>
@@ -209,11 +236,21 @@ const CollaborationSpace = () => {
209236
</ToastContainer>
210237
) : (
211238
<>
239+
{/* Toast Container for Join/Leave notifications */}
240+
<ToastContainer className='p-3' position='top-center' style={{ zIndex: 1050 }}>
241+
{notifs.map((notif) => (
242+
<Toast key={notif.id} className='mb-2' style={{ backgroundColor: 'rgba(255, 255, 255, 0.8)' }}>
243+
<Toast.Body>
244+
<strong className='text-black'>{notif.message}</strong>
245+
</Toast.Body>
246+
</Toast>
247+
))}
248+
</ToastContainer>
212249
<CollabNavigationBar handleExit={handleExit} handleCodeRun={handleCodeRun} users={users} setLanguage={setLanguage} language={language}/>
213250
<Container fluid style={{ marginTop: '20px', height: 'calc(100vh - 60px)', display: 'flex', overflow: 'hidden' }}>
214251
<Row style={{ flexGrow: 1, width: '100%', height: '100%', overflow: 'hidden' }}>
215252
<Col md={6} style={{ display: 'flex', flexDirection: 'column', height: '100%', overflow: 'hidden' }}>
216-
<CodeSpace handleEditorChange={handleEditorChange} code={code} language={language} output={output}/>
253+
<CodeSpace handleEditorChange={handleEditorChange} loading={outputLoading} code={code} language={language} output={output}/>
217254
</Col>
218255
<Col md={6} style={{ display: 'flex', flexDirection: 'column', height: '100%', overflow: 'hidden' }}>
219256
<div style={{ flex: '60%', display: 'flex', overflow: 'hidden' }}>

0 commit comments

Comments
 (0)