@pubuduth-aplicy/chat-ui 2.1.29 → 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.29",
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": "",
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import React, { useCallback, useState } from "react";
2
+ import React, { useCallback, useEffect, useState } from "react";
3
3
  import { useMessageMutation } from "../../hooks/mutations/useSendMessage";
4
4
  import { useChatContext } from "../../providers/ChatProvider";
5
5
  import useChatUIStore from "../../stores/Zustant";
@@ -13,8 +13,60 @@ 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
+ const [isTyping, setIsTyping] = useState(false); // Track if the user is typing
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
+
50
+ useEffect(() => {
51
+ if (message) {
52
+ setIsTyping(true);
53
+ socket.emit("typing", {
54
+ chatId: selectedConversation?._id,
55
+ userId,
56
+ });
57
+ }
58
+
59
+ // Clear typing indicator when user stops typing
60
+ const typingTimeout = setTimeout(() => {
61
+ if (message === "") {
62
+ setIsTyping(false);
63
+ socket.emit("stopTyping", { chatId: selectedConversation?._id, userId });
64
+ }
65
+ }, 2000);
66
+
67
+ return () => clearTimeout(typingTimeout);
68
+ }, [message, socket, selectedConversation?._id, userId]);
69
+
18
70
 
19
71
  const handleSubmit = useCallback(
20
72
  async (e: any) => {
@@ -62,6 +114,11 @@ const MessageInput = () => {
62
114
  {/* {loading ? <div className='loading loading-spinner'></div> : <PaperPlaneRight />} Show loading spinner if loading */}
63
115
  </button>
64
116
  </div>
117
+
118
+ {isTyping && !isSending && (
119
+ <div className="typingIndicator">Someone is typing...</div>
120
+ )}
121
+
65
122
  </form>
66
123
  );
67
124
  };