forked from codeaashu/claude-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePresence.ts
More file actions
154 lines (134 loc) · 4.6 KB
/
Copy pathusePresence.ts
File metadata and controls
154 lines (134 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use client";
import { useState, useEffect, useCallback, useRef } from "react";
import {
createPresenceState,
presenceAddUser,
presenceRemoveUser,
presenceSyncUsers,
presenceUpdateCursor,
presenceSetTyping,
} from "@/lib/collaboration/presence";
import type { PresenceState } from "@/lib/collaboration/presence";
import type { CollabSocket } from "@/lib/collaboration/socket";
import type { CollabUser } from "@/lib/collaboration/socket";
// ─── Options ──────────────────────────────────────────────────────────────────
export interface UsePresenceOptions {
socket: CollabSocket | null;
sessionId: string;
currentUser: CollabUser;
}
// ─── Hook ─────────────────────────────────────────────────────────────────────
export function usePresence({ socket, sessionId, currentUser }: UsePresenceOptions) {
const [presence, setPresence] = useState<PresenceState>(createPresenceState);
const typingTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const isTypingRef = useRef(false);
useEffect(() => {
if (!socket) return;
const cleanup: Array<() => void> = [];
cleanup.push(
socket.on("presence_sync", (e) => {
setPresence((s) => presenceSyncUsers(s, e.users));
})
);
cleanup.push(
socket.on("session_state", (e) => {
setPresence((s) => presenceSyncUsers(s, e.users));
})
);
cleanup.push(
socket.on("user_joined", (e) => {
setPresence((s) => presenceAddUser(s, e.user));
})
);
cleanup.push(
socket.on("user_left", (e) => {
setPresence((s) => presenceRemoveUser(s, e.user.id));
})
);
cleanup.push(
socket.on("cursor_update", (e) => {
if (e.userId === currentUser.id) return; // skip own cursor
setPresence((s) =>
presenceUpdateCursor(s, e.userId, e.position, e.selectionStart, e.selectionEnd)
);
})
);
cleanup.push(
socket.on("typing_start", (e) => {
if (e.userId === currentUser.id) return;
setPresence((s) => presenceSetTyping(s, e.user.id, true));
})
);
cleanup.push(
socket.on("typing_stop", (e) => {
if (e.userId === currentUser.id) return;
setPresence((s) => presenceSetTyping(s, e.user.id, false));
})
);
return () => cleanup.forEach((off) => off());
}, [socket, currentUser.id]);
// ─── Actions ─────────────────────────────────────────────────────────────
const sendCursorUpdate = useCallback(
(position: number, selectionStart?: number, selectionEnd?: number) => {
socket?.send({
type: "cursor_update",
sessionId,
userId: currentUser.id,
position,
selectionStart,
selectionEnd,
});
},
[socket, sessionId, currentUser.id]
);
// Call this whenever the user types — auto-sends typing_start + debounced typing_stop
const notifyTyping = useCallback(() => {
if (!isTypingRef.current) {
isTypingRef.current = true;
socket?.send({
type: "typing_start",
sessionId,
userId: currentUser.id,
user: currentUser,
});
}
if (typingTimerRef.current) clearTimeout(typingTimerRef.current);
typingTimerRef.current = setTimeout(() => {
isTypingRef.current = false;
socket?.send({
type: "typing_stop",
sessionId,
userId: currentUser.id,
user: currentUser,
});
}, 2_000);
}, [socket, sessionId, currentUser]);
const stopTyping = useCallback(() => {
if (typingTimerRef.current) clearTimeout(typingTimerRef.current);
if (isTypingRef.current) {
isTypingRef.current = false;
socket?.send({
type: "typing_stop",
sessionId,
userId: currentUser.id,
user: currentUser,
});
}
}, [socket, sessionId, currentUser]);
// Derived helpers
const otherUsers = Array.from(presence.users.values()).filter(
(u) => u.id !== currentUser.id
);
const typingUsers = Array.from(presence.typing)
.filter((id) => id !== currentUser.id)
.map((id) => presence.users.get(id))
.filter((u): u is CollabUser => u !== undefined);
return {
presence,
otherUsers,
typingUsers,
sendCursorUpdate,
notifyTyping,
stopTyping,
};
}