Skip to content

Commit

Permalink
update Room component and joining logic
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshidwivedi committed Jun 22, 2024
1 parent 9fe85b0 commit abaf32c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
36 changes: 32 additions & 4 deletions modules/room/components/Canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { CANVAS_SIZE } from "@/common/constants/canvasSize";

import { useViewportSize } from "@/common/hooks/useViewportSize";

import { useMotionValue, motion } from "framer-motion";

import { useRef, useState, useEffect } from "react";

import { useKeyPressEvent } from "react-use";

import { useDraw, useSocketDraw } from "../hooks/Canvas.hooks";

import { socket } from "@/common/lib/socket";

import Minimap from "./Minimap";

import room, { useRoom } from "@/common/recoil/room";
import { drawAllMoves } from "../helpers/Canvas.helpers";


const Canvas = () => {
const room= useRoom();
const canvasRef = useRef<HTMLCanvasElement>(null);
const smallCanvasRef = useRef<HTMLCanvasElement>(null);

const [ctx, setCtx] = useState<CanvasRenderingContext2D | undefined>();
const [ctx, setCtx] = useState<CanvasRenderingContext2D>();
const [dragging, setDragging] = useState(false);
const [, setMovedMiniMap] = useState(false);

Expand Down Expand Up @@ -44,10 +56,11 @@ const Canvas = () => {

const { handleDraw, handleEndDrawing, handleStartDrawing, drawing, handleUndo } = useDraw(
ctx,
dragging,
copyCanvasToSmall
dragging
);

useSocketDraw(ctx,drawing);

useEffect(() => {
const newCtx = canvasRef.current?.getContext("2d");
if (newCtx) setCtx(newCtx);
Expand All @@ -63,7 +76,18 @@ const Canvas = () => {
}
}, [dragging]);

useSocketDraw(ctx,drawing, copyCanvasToSmall);
useEffect(()=> {
if(ctx) socket.emit("joined_room");
},[ctx]);

useEffect(()=> {
if(ctx){
drawAllMoves(ctx, room);
copyCanvasToSmall();
};
}, [ctx, room]);


return (
<div className="relative h-full w-full overflow-hidden">
<button className="absolute top-0" onClick={handleUndo}>
Expand Down Expand Up @@ -123,3 +147,7 @@ const Canvas = () => {
};

export default Canvas;
function copyCanvasToSmall() {
throw new Error("Function not implemented.");
}

16 changes: 9 additions & 7 deletions modules/room/components/MouseRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { socket } from "@/common/lib/socket";
import { useEffect, useState } from "react";
import { useRoom } from "@/common/recoil/room";

import UserMouse from "./UserMouse";
import { useUserIds } from "@/common/recoil/users";

export const MouseRenderer = () => {
const userIds = useUserIds();

export const MouseRenderer = () => {

const room = useRoom ();
return (
<>
{userIds.map((userId) => (
<UserMouse userId={userId} key={userId} />
))}
{[...room.users.keys()].map((userId) => {
if (userId === socket.id) return null;
return <UserMouse userId={userId} key={userId} />
})}
</>
);
};
Expand Down
34 changes: 31 additions & 3 deletions modules/room/components/Room.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
import { useRoomId } from "@/common/recoil/room";
import { useEffect } from "react";

import { useRouter } from "next/router";

import { socket } from "@/common/lib/socket";
import { useRoom, useSetRoomId } from "@/common/recoil/room";

import RoomContextProvider from "../context/Room.context";
import Canvas from "./Canvas";
import { MousePosition } from "./MousePosition";
import MouseRenderer from "./MouseRenderer";
import { ToolBar } from "./ToolBar";




const Room = () => {
const roomId = useRoomId();
const room = useRoom();
const setRoomId= useSetRoomId();
const router= useRouter();

useEffect(()=> {
const handleJoined = (roomIdFromServer: string, failed?:boolean) => {
if(failed) router.push("/");
else setRoomId(roomIdFromServer);
};
socket.on("joined", handleJoined);

return () => {
socket.off("joined", handleJoined);
};
}, [router, setRoomId]);

if(!room.id) {
const dynamicRoomId= router.query.roomId?.toString();
if( dynamicRoomId) socket.emit("join_room", dynamicRoomId);
return null;

if(!roomId) return <div> No Room ID</div>
}

return (
<RoomContextProvider>
Expand Down

0 comments on commit abaf32c

Please sign in to comment.