11"use client"
22
33import { Game } from "@/draw/Game" ;
4- import { BgFill , canvasBgLight , Shape , StrokeEdge , StrokeFill , StrokeStyle , StrokeWidth , ToolType } from "@/types/canvas" ;
4+ import { BgFill , canvasBgLight , StrokeEdge , StrokeFill , StrokeStyle , StrokeWidth , ToolType } from "@/types/canvas" ;
55import React , { SetStateAction , useCallback , useEffect , useRef , useState } from "react" ;
66import { Scale } from "../Scale" ;
77import { MobileNavbar } from "../mobile-navbar" ;
@@ -22,7 +22,6 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
2222 roomName : string ; roomId : string ; userId : string ; userName : string ; token : string ;
2323} ) {
2424 const { theme } = useTheme ( )
25- const [ canvasColor , setCanvasColor ] = useState < string > ( canvasBgLight [ 0 ] ) ;
2625 const canvasRef = useRef < HTMLCanvasElement > ( null ) ;
2726 const paramsRef = useRef ( { roomId, roomName, userId, userName, token } ) ;
2827 const router = useRouter ( ) ;
@@ -56,14 +55,15 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
5655 } = useWebSocket ( paramsRef . current . roomId , paramsRef . current . roomName , paramsRef . current . userId , paramsRef . current . userName , paramsRef . current . token ) ;
5756
5857 useEffect ( ( ) => {
59- setCanvasColor ( canvasBgLight [ 0 ] ) ;
58+ setCanvasState ( prev => ( { ... prev , canvasColor : canvasBgLight [ 0 ] } ) ) ;
6059 } , [ theme ] )
6160
6261 useEffect ( ( ) => {
6362 const { game, scale } = canvasState ;
6463 if ( game ) {
6564 game . setScale ( scale ) ;
6665 }
66+ // eslint-disable-next-line react-hooks/exhaustive-deps
6767 } , [ canvasState . game , canvasState . scale ] ) ;
6868
6969 useEffect ( ( ) => {
@@ -97,37 +97,6 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
9797 }
9898 } , [ ] ) ;
9999
100- // eslint-disable-next-line @typescript-eslint/no-explicit-any
101- const processMessages = useCallback ( ( messages : any [ ] ) => {
102- const newExistingShapes : Shape [ ] = [ ] ;
103-
104- messages . forEach ( message => {
105- switch ( message . type ) {
106- case WsDataType . DRAW :
107- if ( ! newExistingShapes . some ( s => s . id === message . id ) ) {
108- newExistingShapes . push ( message . message ) ;
109- }
110- break ;
111- case WsDataType . UPDATE :
112- const index = newExistingShapes . findIndex ( s => s . id === message . id ) ;
113- if ( index !== - 1 ) {
114- newExistingShapes [ index ] = {
115- ...newExistingShapes [ index ] ,
116- ...message . message
117- } ;
118- }
119- break ;
120- case WsDataType . ERASER :
121- const filteredShapes = newExistingShapes . filter ( s => s . id !== message . id ) ;
122- newExistingShapes . length = 0 ;
123- newExistingShapes . push ( ...filteredShapes ) ;
124- break ;
125- }
126- } ) ;
127-
128- return newExistingShapes ;
129- } , [ ] ) ;
130-
131100 const initializeGame = useCallback ( ( ) => {
132101 if ( ! canvasRef . current || ! isConnected ) return null ;
133102
@@ -140,6 +109,9 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
140109 const game = new Game (
141110 canvasRef . current ,
142111 paramsRef . current . roomId ,
112+ paramsRef . current . roomName ,
113+ paramsRef . current . userId ,
114+ paramsRef . current . userName ,
143115 canvasState . canvasColor ,
144116 handleSendDrawing ,
145117 ( newScale ) => setCanvasState ( prev => ( { ...prev , scale : newScale } ) ) ,
@@ -177,11 +149,12 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
177149 } , [ initializeGame , handleKeyDown ] ) ;
178150
179151 useEffect ( ( ) => {
152+ console . log ( 'messages length = ' , messages . length )
180153 if ( messages . length > 0 && canvasState . game ) {
181- const processedShapes = processMessages ( messages ) ;
182- canvasState . game . updateShapes ( processedShapes ) ;
154+ canvasState . game . updateShapes ( messages ) ;
155+ console . log ( 'messages = ' , messages )
183156 }
184- } , [ messages , canvasState . game , processMessages ] ) ;
157+ } , [ messages , canvasState . game ] ) ;
185158
186159 useEffect ( ( ) => {
187160 if ( existingMsgs ?. message && canvasState . game ) {
@@ -215,8 +188,10 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
215188 < MainMenuStack
216189 isOpen = { canvasState . sidebarOpen }
217190 onClose = { ( ) => setCanvasState ( prev => ( { ...prev , sidebarOpen : false } ) ) }
218- canvasColor = { canvasColor }
219- setCanvasColor = { setCanvasColor }
191+ canvasColor = { canvasState . canvasColor }
192+ setCanvasColor = { ( newCanvasColor : SetStateAction < string > ) =>
193+ setCanvasState ( prev => ( { ...prev , canvasColor : typeof newCanvasColor === 'function' ? newCanvasColor ( prev . canvasColor ) : newCanvasColor } ) )
194+ }
220195 roomName = { roomName }
221196 onCloseRoom = { ( ) => {
222197 sendMessage (
@@ -300,8 +275,10 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
300275 < MobileNavbar
301276 sidebarOpen = { canvasState . sidebarOpen }
302277 setSidebarOpen = { ( ) => setCanvasState ( prev => ( { ...prev , sidebarOpen : ! prev . sidebarOpen } ) ) }
303- canvasColor = { canvasColor }
304- setCanvasColor = { setCanvasColor }
278+ canvasColor = { canvasState . canvasColor }
279+ setCanvasColor = { ( newCanvasColor : SetStateAction < string > ) =>
280+ setCanvasState ( prev => ( { ...prev , canvasColor : typeof newCanvasColor === 'function' ? newCanvasColor ( prev . canvasColor ) : newCanvasColor } ) )
281+ }
305282 scale = { canvasState . scale }
306283 setScale = { ( newScale : SetStateAction < number > ) =>
307284 setCanvasState ( prev => ( { ...prev , scale : typeof newScale === 'function' ? newScale ( prev . scale ) : newScale } ) )
0 commit comments