|
| 1 | +import { useReactFlow, type XYPosition } from "@xyflow/react"; |
| 2 | +import * as Y from "yjs"; |
| 3 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 4 | +import { WebsocketProvider } from "y-websocket"; |
| 5 | + |
| 6 | +const CURSOR_COLORS = [ |
| 7 | + "#7d7b94", |
| 8 | + "#41c76d", |
| 9 | + "#f86e7e", |
| 10 | + "#f6b8b8", |
| 11 | + "#f7d353", |
| 12 | + "#3b5bf7", |
| 13 | + "#59cbf7", |
| 14 | +] as const; |
| 15 | + |
| 16 | +export interface AwarenessState { |
| 17 | + cursor: XYPosition | null; |
| 18 | + color: string; |
| 19 | + clientId: number; |
| 20 | +} |
| 21 | + |
| 22 | +interface CollaborativeCursorsProps { |
| 23 | + ydoc: Y.Doc; |
| 24 | + roomName?: string; |
| 25 | +} |
| 26 | + |
| 27 | +export function useCollaborativeCursors({ |
| 28 | + ydoc, |
| 29 | + roomName = "cursor-room", |
| 30 | +}: CollaborativeCursorsProps) { |
| 31 | + const flowInstance = useReactFlow(); |
| 32 | + const provider = useRef<WebsocketProvider>(); |
| 33 | + const [cursors, setCursors] = useState<Map<number, AwarenessState>>( |
| 34 | + new Map(), |
| 35 | + ); |
| 36 | + const clientId = useRef<number | null>(null); |
| 37 | + const userColor = useRef( |
| 38 | + CURSOR_COLORS[Math.floor(Math.random() * CURSOR_COLORS.length)], |
| 39 | + ); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + const wsProvider = new WebsocketProvider( |
| 43 | + import.meta.env.VITE_WS_URL, |
| 44 | + roomName, |
| 45 | + ydoc, |
| 46 | + ); |
| 47 | + |
| 48 | + provider.current = wsProvider; |
| 49 | + clientId.current = wsProvider.awareness.clientID; |
| 50 | + |
| 51 | + wsProvider.awareness.setLocalState({ |
| 52 | + cursor: null, |
| 53 | + color: userColor.current, |
| 54 | + clientId: wsProvider.awareness.clientID, |
| 55 | + }); |
| 56 | + |
| 57 | + wsProvider.awareness.on("change", () => { |
| 58 | + const states = new Map( |
| 59 | + Array.from( |
| 60 | + wsProvider.awareness.getStates() as Map<number, AwarenessState>, |
| 61 | + ).filter( |
| 62 | + ([key, state]) => key !== clientId.current && state.cursor !== null, |
| 63 | + ), |
| 64 | + ); |
| 65 | + setCursors(states); |
| 66 | + }); |
| 67 | + |
| 68 | + return () => { |
| 69 | + wsProvider.destroy(); |
| 70 | + }; |
| 71 | + }, [ydoc, roomName]); |
| 72 | + |
| 73 | + const updateCursorPosition = useCallback( |
| 74 | + (x: number | null, y: number | null) => { |
| 75 | + if (!provider.current?.awareness) return; |
| 76 | + |
| 77 | + const cursor = |
| 78 | + x !== null && y !== null |
| 79 | + ? flowInstance?.screenToFlowPosition({ x, y }) |
| 80 | + : null; |
| 81 | + |
| 82 | + provider.current.awareness.setLocalState({ |
| 83 | + cursor, |
| 84 | + color: userColor.current, |
| 85 | + clientId: provider.current.awareness.clientID, |
| 86 | + }); |
| 87 | + }, |
| 88 | + [flowInstance], |
| 89 | + ); |
| 90 | + |
| 91 | + const handleMouseMove = useCallback( |
| 92 | + (e: React.MouseEvent) => { |
| 93 | + updateCursorPosition(e.clientX, e.clientY); |
| 94 | + }, |
| 95 | + [updateCursorPosition], |
| 96 | + ); |
| 97 | + |
| 98 | + const handleNodeDrag = useCallback( |
| 99 | + (e: React.MouseEvent) => { |
| 100 | + updateCursorPosition(e.clientX, e.clientY); |
| 101 | + }, |
| 102 | + [updateCursorPosition], |
| 103 | + ); |
| 104 | + |
| 105 | + const handleMouseLeave = useCallback(() => { |
| 106 | + updateCursorPosition(null, null); |
| 107 | + }, [updateCursorPosition]); |
| 108 | + |
| 109 | + return { |
| 110 | + cursors, |
| 111 | + handleMouseMove, |
| 112 | + handleNodeDrag, |
| 113 | + handleMouseLeave, |
| 114 | + }; |
| 115 | +} |
0 commit comments