@@ -2,16 +2,7 @@ import { useReactFlow, type XYPosition } from "@xyflow/react";
2
2
import * as Y from "yjs" ;
3
3
import { useCallback , useEffect , useRef , useState } from "react" ;
4
4
import { SocketIOProvider } from "y-socket.io" ;
5
-
6
- const CURSOR_COLORS = [
7
- "#7d7b94" ,
8
- "#41c76d" ,
9
- "#f86e7e" ,
10
- "#f6b8b8" ,
11
- "#f7d353" ,
12
- "#3b5bf7" ,
13
- "#59cbf7" ,
14
- ] as const ;
5
+ import useUserStore from "@/store/useUserStore" ;
15
6
16
7
export interface AwarenessState {
17
8
cursor : XYPosition | null ;
@@ -33,10 +24,7 @@ export function useCollaborativeCursors({
33
24
const [ cursors , setCursors ] = useState < Map < number , AwarenessState > > (
34
25
new Map ( ) ,
35
26
) ;
36
- const clientId = useRef < number | null > ( null ) ;
37
- const userColor = useRef (
38
- CURSOR_COLORS [ Math . floor ( Math . random ( ) * CURSOR_COLORS . length ) ] ,
39
- ) ;
27
+ const { currentUser } = useUserStore ( ) ;
40
28
41
29
useEffect ( ( ) => {
42
30
const wsProvider = new SocketIOProvider (
@@ -55,21 +43,18 @@ export function useCollaborativeCursors({
55
43
) ;
56
44
57
45
provider . current = wsProvider ;
58
- clientId . current = wsProvider . awareness . clientID ;
59
46
60
47
wsProvider . awareness . setLocalState ( {
61
48
cursor : null ,
62
- color : userColor . current ,
63
- clientId : wsProvider . awareness . clientID ,
49
+ color : currentUser . color ,
50
+ clientId : currentUser . clientId ,
64
51
} ) ;
65
52
66
53
wsProvider . awareness . on ( "change" , ( ) => {
67
54
const states = new Map (
68
55
Array . from (
69
56
wsProvider . awareness . getStates ( ) as Map < number , AwarenessState > ,
70
- ) . filter (
71
- ( [ key , state ] ) => key !== clientId . current && state . cursor !== null ,
72
- ) ,
57
+ ) . filter ( ( [ , state ] ) => state . cursor !== null ) ,
73
58
) ;
74
59
setCursors ( states ) ;
75
60
} ) ;
@@ -90,8 +75,8 @@ export function useCollaborativeCursors({
90
75
91
76
provider . current . awareness . setLocalState ( {
92
77
cursor,
93
- color : userColor . current ,
94
- clientId : provider . current . awareness . clientID ,
78
+ color : currentUser . color ,
79
+ clientId : currentUser . clientId ,
95
80
} ) ;
96
81
} ,
97
82
[ flowInstance ] ,
0 commit comments