@pubuduth-aplicy/chat-ui 2.1.30 → 2.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -13,11 +13,41 @@ const MessageInput = () => {
|
|
|
13
13
|
const { selectedConversation } = useChatUIStore();
|
|
14
14
|
const [message, setMessage] = useState(""); // State for storing the message input
|
|
15
15
|
const { mutate: sendMessage } = useMessageMutation();
|
|
16
|
-
|
|
16
|
+
const [typingUser, setTypingUser] = useState<string | null>(null);
|
|
17
17
|
const [isSending, setIsSending] = useState(false);
|
|
18
|
-
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
19
19
|
const [isTyping, setIsTyping] = useState(false); // Track if the user is typing
|
|
20
20
|
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (selectedConversation?._id) {
|
|
23
|
+
socket.emit("joinChat", selectedConversation._id);
|
|
24
|
+
}
|
|
25
|
+
}, [selectedConversation, socket]);
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!socket) return;
|
|
29
|
+
|
|
30
|
+
const handleTyping = ({ userid }: { userid: string }) => {
|
|
31
|
+
if (userid !== userId) {
|
|
32
|
+
setTypingUser(userid);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const handleStopTyping = ({ userid }: { userid: string }) => {
|
|
37
|
+
if (userid === typingUser) {
|
|
38
|
+
setTypingUser(null);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
socket.on("typing", handleTyping);
|
|
43
|
+
socket.on("stopTyping", handleStopTyping);
|
|
44
|
+
|
|
45
|
+
return () => {
|
|
46
|
+
socket.off("typing", handleTyping);
|
|
47
|
+
socket.off("stopTyping", handleStopTyping);
|
|
48
|
+
};
|
|
49
|
+
}, [socket, typingUser, userId]);
|
|
50
|
+
|
|
21
51
|
useEffect(() => {
|
|
22
52
|
if (message) {
|
|
23
53
|
setIsTyping(true);
|
|
@@ -25,21 +55,19 @@ const MessageInput = () => {
|
|
|
25
55
|
chatId: selectedConversation?._id,
|
|
26
56
|
userId,
|
|
27
57
|
});
|
|
28
|
-
} else {
|
|
29
|
-
setIsTyping(false);
|
|
30
|
-
socket.emit("typing", { chatId: selectedConversation?._id, userId: null }); // Stop typing event
|
|
31
58
|
}
|
|
32
|
-
|
|
33
|
-
//
|
|
59
|
+
|
|
60
|
+
// Clear typing indicator when user stops typing
|
|
34
61
|
const typingTimeout = setTimeout(() => {
|
|
35
62
|
if (message === "") {
|
|
36
63
|
setIsTyping(false);
|
|
37
|
-
socket.emit("
|
|
64
|
+
socket.emit("stopTyping", { chatId: selectedConversation?._id, userId });
|
|
38
65
|
}
|
|
39
66
|
}, 2000);
|
|
40
|
-
|
|
67
|
+
|
|
41
68
|
return () => clearTimeout(typingTimeout);
|
|
42
69
|
}, [message, socket, selectedConversation?._id, userId]);
|
|
70
|
+
|
|
43
71
|
|
|
44
72
|
const handleSubmit = useCallback(
|
|
45
73
|
async (e: any) => {
|
|
@@ -88,7 +116,7 @@ const MessageInput = () => {
|
|
|
88
116
|
</button>
|
|
89
117
|
</div>
|
|
90
118
|
|
|
91
|
-
{
|
|
119
|
+
{typingUser && typingUser !== userId && !isSending &&(
|
|
92
120
|
<div className="typingIndicator">Someone is typing...</div>
|
|
93
121
|
)}
|
|
94
122
|
|