From 497976d88989dc36536143b6298274b46232f48e Mon Sep 17 00:00:00 2001 From: SahajSaliya <93511057+SahajSaliya@users.noreply.github.com> Date: Wed, 11 Oct 2023 19:30:49 +0530 Subject: [PATCH] Copy Code feature We have made changes to the code, and the code editor is now capable of copying code. --- EditorPage.js | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 EditorPage.js diff --git a/EditorPage.js b/EditorPage.js new file mode 100644 index 0000000..40b93da --- /dev/null +++ b/EditorPage.js @@ -0,0 +1,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 ; + } + + return ( +
+
+
+
+ logo +
+

Connected

+
+ {clients.map((client) => ( + + ))} +
+
+ {/* */} + Run code + + + +
+
+ { + codeRef.current = code; + }} + /> +
+
+ ); +}; + +export default EditorPage; \ No newline at end of file