@@ -2,16 +2,7 @@ import { useReactFlow, type XYPosition } from "@xyflow/react";
22import * as Y from "yjs" ;
33import { useCallback , useEffect , useRef , useState } from "react" ;
44import { 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" ;
156
167export interface AwarenessState {
178 cursor : XYPosition | null ;
@@ -33,10 +24,7 @@ export function useCollaborativeCursors({
3324 const [ cursors , setCursors ] = useState < Map < number , AwarenessState > > (
3425 new Map ( ) ,
3526 ) ;
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 ( ) ;
4028
4129 useEffect ( ( ) => {
4230 const wsProvider = new SocketIOProvider (
@@ -55,21 +43,18 @@ export function useCollaborativeCursors({
5543 ) ;
5644
5745 provider . current = wsProvider ;
58- clientId . current = wsProvider . awareness . clientID ;
5946
6047 wsProvider . awareness . setLocalState ( {
6148 cursor : null ,
62- color : userColor . current ,
63- clientId : wsProvider . awareness . clientID ,
49+ color : currentUser . color ,
50+ clientId : currentUser . clientId ,
6451 } ) ;
6552
6653 wsProvider . awareness . on ( "change" , ( ) => {
6754 const states = new Map (
6855 Array . from (
6956 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 ) ,
7358 ) ;
7459 setCursors ( states ) ;
7560 } ) ;
@@ -90,8 +75,8 @@ export function useCollaborativeCursors({
9075
9176 provider . current . awareness . setLocalState ( {
9277 cursor,
93- color : userColor . current ,
94- clientId : provider . current . awareness . clientID ,
78+ color : currentUser . color ,
79+ clientId : currentUser . clientId ,
9580 } ) ;
9681 } ,
9782 [ flowInstance ] ,
0 commit comments