-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEditorPage.js
More file actions
141 lines (128 loc) · 4.67 KB
/
Copy pathEditorPage.js
File metadata and controls
141 lines (128 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, { useState, useRef, useEffect } from 'react';
import toast from 'react-hot-toast';
import ACTIONS from '../Actions';
import Client from '../components/Client';
import Editor from '../components/Editor';
import { initSocket } from '../socket';
import {
useLocation,
useNavigate,
Navigate,
useParams,
} from 'react-router-dom';
const EditorPage = () => {
const socketRef = useRef(null);
const codeRef = useRef(null);
const location = useLocation();
const { roomId } = useParams();
const reactNavigator = useNavigate();
const [clients, setClients] = useState([]);
useEffect(() => {
const init = async () => {
socketRef.current = await initSocket();
socketRef.current.on('connect_error', (err) => handleErrors(err));
socketRef.current.on('connect_failed', (err) => handleErrors(err));
function handleErrors(e) {
console.log('socket error', e);
toast.error('Socket connection failed, try again later.');
reactNavigator('/');
}
socketRef.current.emit(ACTIONS.JOIN, {
roomId,
username: location.state?.username,
});
// Listening for joined event
socketRef.current.on(
ACTIONS.JOINED,
({ clients, username, socketId }) => {
if (username !== location.state?.username) {
toast.success(`${username} joined the room.`);
console.log(`${username} joined`);
}
setClients(clients);
socketRef.current.emit(ACTIONS.SYNC_CODE, {
code: codeRef.current,
socketId,
});
}
);
// Listening for disconnected
socketRef.current.on(
ACTIONS.DISCONNECTED,
({ socketId, username }) => {
toast.success(`${username} left the room.`);
setClients((prev) => {
return prev.filter(
(client) => client.socketId !== socketId
);
});
}
);
};
init();
return () => {
//socketRef.current.disconnect();
//socketRef.current.off(ACTIONS.JOINED);
//socketRef.current.off(ACTIONS.DISCONNECTED);
};
}, []);
async function copyRoomId() {
try {
await navigator.clipboard.writeText(roomId);
toast.success('Room ID has been copied to your clipboard');
} catch (err) {
toast.error('Could not copy the Room ID');
console.error(err);
}
}
function leaveRoom() {
reactNavigator('/');
}
if (!location.state) {
return <Navigate to="/" />;
}
return (
<div className="mainWrap">
<div className="aside">
<div className="asideInner">
<div className="logo">
<img
className="logoImage"
src="/code-sync2.jpg"
alt="logo"
/>
</div>
<h3>Connected</h3>
<div className="clientsList">
{clients.map((client) => (
<Client
key={client.socketId}
username={client.username}
/>
))}
</div>
</div>
{/* <button className="btn copyCode">
Copy Code
</button> */}
<a href="https://onecompiler.com/cpp" className='runNewBtn'>Run code</a>
<button className="btn copyBtn" onClick={copyRoomId}>
Copy ROOM ID
</button>
<button className="btn leaveBtn" onClick={leaveRoom}>
Leave
</button>
</div>
<div className="editorWrap">
<Editor
socketRef={socketRef}
roomId={roomId}
onCodeChange={(code) => {
codeRef.current = code;
}}
/>
</div>
</div>
);
};
export default EditorPage;