-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathuseTypingStatus.js
More file actions
56 lines (48 loc) · 1.71 KB
/
useTypingStatus.js
File metadata and controls
56 lines (48 loc) · 1.71 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
import { useRef, useCallback } from 'react';
/**
* useTypingStatus - A hook to manage the 'User is typing...' status.
* Decouples typing-indicator side-effects from the main ChatInput view.
*/
const useTypingStatus = (RCInstance, username) => {
const typingRef = useRef(false);
const timerRef = useRef(null);
const sendTypingStop = useCallback(async () => {
try {
if (typingRef.current) {
typingRef.current = false;
if (timerRef.current) {
clearTimeout(timerRef.current);
}
await RCInstance.sendTypingStatus(username, false);
}
} catch (e) {
console.error('Typing indicator error (stop):', e);
}
}, [RCInstance, username]);
const sendTypingStart = useCallback(async (currentMessageValue) => {
try {
// If we are already typing and still have message content, do nothing (to avoid spamming the server).
if (typingRef.current && currentMessageValue?.length) {
return;
}
if (currentMessageValue?.length) {
typingRef.current = true;
// Reset the typing timeout (typing status is valid for 15 seconds by default in RC).
if (timerRef.current) {
clearTimeout(timerRef.current);
}
timerRef.current = setTimeout(() => {
typingRef.current = false;
}, 15000);
await RCInstance.sendTypingStatus(username, true);
} else {
// If the user clears the input, stop the typing status immediately.
await sendTypingStop();
}
} catch (e) {
console.error('Typing indicator error (start):', e);
}
}, [RCInstance, username, sendTypingStop]);
return { sendTypingStart, sendTypingStop };
};
export default useTypingStatus;