1
1
"use client"
2
2
3
3
import { 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" ;
5
5
import React , { SetStateAction , useCallback , useEffect , useRef , useState } from "react" ;
6
6
import { Scale } from "../Scale" ;
7
7
import { MobileNavbar } from "../mobile-navbar" ;
@@ -22,7 +22,6 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
22
22
roomName : string ; roomId : string ; userId : string ; userName : string ; token : string ;
23
23
} ) {
24
24
const { theme } = useTheme ( )
25
- const [ canvasColor , setCanvasColor ] = useState < string > ( canvasBgLight [ 0 ] ) ;
26
25
const canvasRef = useRef < HTMLCanvasElement > ( null ) ;
27
26
const paramsRef = useRef ( { roomId, roomName, userId, userName, token } ) ;
28
27
const router = useRouter ( ) ;
@@ -56,14 +55,15 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
56
55
} = useWebSocket ( paramsRef . current . roomId , paramsRef . current . roomName , paramsRef . current . userId , paramsRef . current . userName , paramsRef . current . token ) ;
57
56
58
57
useEffect ( ( ) => {
59
- setCanvasColor ( canvasBgLight [ 0 ] ) ;
58
+ setCanvasState ( prev => ( { ... prev , canvasColor : canvasBgLight [ 0 ] } ) ) ;
60
59
} , [ theme ] )
61
60
62
61
useEffect ( ( ) => {
63
62
const { game, scale } = canvasState ;
64
63
if ( game ) {
65
64
game . setScale ( scale ) ;
66
65
}
66
+ // eslint-disable-next-line react-hooks/exhaustive-deps
67
67
} , [ canvasState . game , canvasState . scale ] ) ;
68
68
69
69
useEffect ( ( ) => {
@@ -97,37 +97,6 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
97
97
}
98
98
} , [ ] ) ;
99
99
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
-
131
100
const initializeGame = useCallback ( ( ) => {
132
101
if ( ! canvasRef . current || ! isConnected ) return null ;
133
102
@@ -140,6 +109,9 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
140
109
const game = new Game (
141
110
canvasRef . current ,
142
111
paramsRef . current . roomId ,
112
+ paramsRef . current . roomName ,
113
+ paramsRef . current . userId ,
114
+ paramsRef . current . userName ,
143
115
canvasState . canvasColor ,
144
116
handleSendDrawing ,
145
117
( newScale ) => setCanvasState ( prev => ( { ...prev , scale : newScale } ) ) ,
@@ -177,11 +149,12 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
177
149
} , [ initializeGame , handleKeyDown ] ) ;
178
150
179
151
useEffect ( ( ) => {
152
+ console . log ( 'messages length = ' , messages . length )
180
153
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 )
183
156
}
184
- } , [ messages , canvasState . game , processMessages ] ) ;
157
+ } , [ messages , canvasState . game ] ) ;
185
158
186
159
useEffect ( ( ) => {
187
160
if ( existingMsgs ?. message && canvasState . game ) {
@@ -215,8 +188,10 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
215
188
< MainMenuStack
216
189
isOpen = { canvasState . sidebarOpen }
217
190
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
+ }
220
195
roomName = { roomName }
221
196
onCloseRoom = { ( ) => {
222
197
sendMessage (
@@ -300,8 +275,10 @@ export default function CanvasSheet({ roomName, roomId, userId, userName, token
300
275
< MobileNavbar
301
276
sidebarOpen = { canvasState . sidebarOpen }
302
277
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
+ }
305
282
scale = { canvasState . scale }
306
283
setScale = { ( newScale : SetStateAction < number > ) =>
307
284
setCanvasState ( prev => ( { ...prev , scale : typeof newScale === 'function' ? newScale ( prev . scale ) : newScale } ) )
0 commit comments