Skip to content

Commit e885ad2

Browse files
committed
feat: 커서에 clientId 표시
1 parent 9f024fb commit e885ad2

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

apps/frontend/src/components/CursorView.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useReactFlow } from "@xyflow/react";
33
import { type AwarenessState } from "@/hooks/useCursor";
44
import Cursor from "./cursor";
55
import { useMemo } from "react";
6+
import useUserStore from "@/store/useUserStore";
67

78
interface CollaborativeCursorsProps {
89
cursors: Map<number, AwarenessState>;
@@ -11,8 +12,15 @@ interface CollaborativeCursorsProps {
1112
export function CollaborativeCursors({ cursors }: CollaborativeCursorsProps) {
1213
const { flowToScreenPosition } = useReactFlow();
1314

15+
const { currentUser } = useUserStore();
16+
1417
const validCursors = useMemo(
15-
() => Array.from(cursors.values()).filter((cursor) => cursor.cursor),
18+
() =>
19+
Array.from(cursors.values()).filter(
20+
(cursor) =>
21+
cursor.cursor &&
22+
(cursor.clientId as unknown as string) !== currentUser.clientId,
23+
),
1624
[cursors],
1725
);
1826

@@ -26,6 +34,7 @@ export function CollaborativeCursors({ cursors }: CollaborativeCursorsProps) {
2634
y: cursor.cursor!.y,
2735
})}
2836
color={cursor.color}
37+
clientId={cursor.clientId.toString()}
2938
/>
3039
))}
3140
</Panel>

apps/frontend/src/components/cursor/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ export interface Coors {
77

88
interface CursorProps {
99
coors: Coors;
10+
clientId: string;
1011
color?: string;
1112
}
1213

13-
export default function Cursor({ coors, color = "#ffb8b9" }: CursorProps) {
14+
export default function Cursor({
15+
coors,
16+
color = "#ffb8b9",
17+
clientId,
18+
}: CursorProps) {
1419
const { x, y } = coors;
1520

1621
return (
@@ -35,6 +40,9 @@ export default function Cursor({ coors, color = "#ffb8b9" }: CursorProps) {
3540
strokeWidth="4"
3641
/>
3742
</svg>
43+
<p className="absolute px-1" style={{ backgroundColor: color }}>
44+
{clientId}
45+
</p>
3846
</motion.div>
3947
);
4048
}

apps/frontend/src/hooks/useCursor.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useReactFlow, type XYPosition } from "@xyflow/react";
22
import * as Y from "yjs";
33
import { useCallback, useEffect, useRef, useState } from "react";
44
import { SocketIOProvider } from "y-socket.io";
5+
import useUserStore from "@/store/useUserStore";
56

67
const CURSOR_COLORS = [
78
"#7d7b94",
@@ -33,10 +34,7 @@ export function useCollaborativeCursors({
3334
const [cursors, setCursors] = useState<Map<number, AwarenessState>>(
3435
new Map(),
3536
);
36-
const clientId = useRef<number | null>(null);
37-
const userColor = useRef(
38-
CURSOR_COLORS[Math.floor(Math.random() * CURSOR_COLORS.length)],
39-
);
37+
const { currentUser } = useUserStore();
4038

4139
useEffect(() => {
4240
const wsProvider = new SocketIOProvider(
@@ -55,21 +53,18 @@ export function useCollaborativeCursors({
5553
);
5654

5755
provider.current = wsProvider;
58-
clientId.current = wsProvider.awareness.clientID;
5956

6057
wsProvider.awareness.setLocalState({
6158
cursor: null,
62-
color: userColor.current,
63-
clientId: wsProvider.awareness.clientID,
59+
color: currentUser.color,
60+
clientId: currentUser.clientId,
6461
});
6562

6663
wsProvider.awareness.on("change", () => {
6764
const states = new Map(
6865
Array.from(
6966
wsProvider.awareness.getStates() as Map<number, AwarenessState>,
70-
).filter(
71-
([key, state]) => key !== clientId.current && state.cursor !== null,
72-
),
67+
).filter(([key, state]) => state.cursor !== null),
7368
);
7469
setCursors(states);
7570
});
@@ -90,8 +85,8 @@ export function useCollaborativeCursors({
9085

9186
provider.current.awareness.setLocalState({
9287
cursor,
93-
color: userColor.current,
94-
clientId: provider.current.awareness.clientID,
88+
color: currentUser.color,
89+
clientId: currentUser.clientId,
9590
});
9691
},
9792
[flowInstance],

0 commit comments

Comments
 (0)