@pubuduth-aplicy/chat-ui 2.1.30 → 2.1.31

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pubuduth-aplicy/chat-ui",
3
- "version": "2.1.30",
3
+ "version": "2.1.31",
4
4
  "description": "This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.",
5
5
  "license": "ISC",
6
6
  "author": "",
@@ -13,11 +13,40 @@ 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
-
19
18
  const [isTyping, setIsTyping] = useState(false); // Track if the user is typing
20
19
 
20
+ useEffect(() => {
21
+ if (selectedConversation?._id) {
22
+ socket.emit("joinChat", selectedConversation._id);
23
+ }
24
+ }, [selectedConversation, socket]);
25
+
26
+ useEffect(() => {
27
+ if (!socket) return;
28
+
29
+ const handleTyping = ({ userid }: { userid: string }) => {
30
+ if (userid !== userId) {
31
+ setTypingUser(userid);
32
+ }
33
+ };
34
+
35
+ const handleStopTyping = ({ userid }: { userid: string }) => {
36
+ if (userid === typingUser) {
37
+ setTypingUser(null);
38
+ }
39
+ };
40
+
41
+ socket.on("typing", handleTyping);
42
+ socket.on("stopTyping", handleStopTyping);
43
+
44
+ return () => {
45
+ socket.off("typing", handleTyping);
46
+ socket.off("stopTyping", handleStopTyping);
47
+ };
48
+ }, [socket, typingUser, userId]);
49
+
21
50
  useEffect(() => {
22
51
  if (message) {
23
52
  setIsTyping(true);
@@ -25,21 +54,19 @@ const MessageInput = () => {
25
54
  chatId: selectedConversation?._id,
26
55
  userId,
27
56
  });
28
- } else {
29
- setIsTyping(false);
30
- socket.emit("typing", { chatId: selectedConversation?._id, userId: null }); // Stop typing event
31
57
  }
32
-
33
- // Cleanup: Stop typing after 2 seconds of inactivity
58
+
59
+ // Clear typing indicator when user stops typing
34
60
  const typingTimeout = setTimeout(() => {
35
61
  if (message === "") {
36
62
  setIsTyping(false);
37
- socket.emit("typing", { chatId: selectedConversation?._id, userId: null });
63
+ socket.emit("stopTyping", { chatId: selectedConversation?._id, userId });
38
64
  }
39
65
  }, 2000);
40
-
66
+
41
67
  return () => clearTimeout(typingTimeout);
42
68
  }, [message, socket, selectedConversation?._id, userId]);
69
+
43
70
 
44
71
  const handleSubmit = useCallback(
45
72
  async (e: any) => {