@pubuduth-aplicy/chat-ui 2.1.27 → 2.1.28
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,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { useEffect, useRef } from "react";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
3
|
// import useGetMessages from "../../hooks/useGetMessages";
|
|
4
4
|
// import MessageSkeleton from "../skeletons/MessageSkeleton";
|
|
5
5
|
import Message from "./Message";
|
|
@@ -10,22 +10,47 @@ import useChatUIStore from "../../stores/Zustant";
|
|
|
10
10
|
|
|
11
11
|
const Messages = () => {
|
|
12
12
|
const { selectedConversation } = useChatUIStore()
|
|
13
|
-
const {userId}=useChatContext()
|
|
14
|
-
console.log(userId);
|
|
13
|
+
const {userId,socket}=useChatContext()
|
|
15
14
|
const { data: messages, isLoading, isError, error } = useMessages(selectedConversation?._id, userId);
|
|
16
|
-
|
|
17
|
-
console.log("isLoading",isLoading);
|
|
18
|
-
console.log("error",error);
|
|
19
|
-
console.log("messages",messages);
|
|
15
|
+
const [localMessages, setLocalMessages] = useState<any[]>([]);
|
|
20
16
|
|
|
21
17
|
const lastMessageRef = useRef<HTMLDivElement>(null);
|
|
22
18
|
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (messages?.messages) {
|
|
21
|
+
setLocalMessages(messages.messages);
|
|
22
|
+
}
|
|
23
|
+
}, [messages]);
|
|
24
|
+
|
|
25
|
+
// Listen for new messages from the server
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!socket) return;
|
|
28
|
+
|
|
29
|
+
const handleNewMessage = (newMessage: any) => {
|
|
30
|
+
setLocalMessages((prevMessages) => [...prevMessages, newMessage]);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
socket.on("newMessage", handleNewMessage);
|
|
34
|
+
|
|
35
|
+
return () => {
|
|
36
|
+
socket.off("newMessage", handleNewMessage);
|
|
37
|
+
};
|
|
38
|
+
}, [socket]);
|
|
39
|
+
|
|
23
40
|
useEffect(() => {
|
|
24
41
|
setTimeout(() => {
|
|
25
42
|
lastMessageRef.current?.scrollIntoView({ behavior: "smooth" });
|
|
26
43
|
}, 100);
|
|
27
44
|
}, [messages]);
|
|
28
45
|
|
|
46
|
+
if (isLoading) {
|
|
47
|
+
return <p>Loading messages...</p>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (isError) {
|
|
51
|
+
return <p>Error: {error?.message}</p>;
|
|
52
|
+
}
|
|
53
|
+
|
|
29
54
|
return (
|
|
30
55
|
// <div className='px-4 flex-1 overflow-auto'>
|
|
31
56
|
// {!loading &&
|
|
@@ -42,19 +67,33 @@ console.log(userId);
|
|
|
42
67
|
// )}
|
|
43
68
|
// </div>
|
|
44
69
|
|
|
45
|
-
<div className='chatMessages'>
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
</div>
|
|
70
|
+
// <div className='chatMessages'>
|
|
71
|
+
// {messages?.messages.length > 0 &&
|
|
72
|
+
// messages?.messages.map((message: any) => (
|
|
73
|
+
// <div key={message._id} ref={lastMessageRef}>
|
|
74
|
+
// <Message message={message} />
|
|
75
|
+
// </div>
|
|
76
|
+
// ))}
|
|
77
|
+
|
|
78
|
+
// {/* {loading && [...Array(3)].map((_, idx) => <MessageSkeleton key={idx} />)} */}
|
|
79
|
+
// {messages?.length === 0 && (
|
|
80
|
+
// <p style={{textAlign:"center"}}>Send a message to start the conversation</p>
|
|
81
|
+
// )}
|
|
82
|
+
// </div>
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
<div className="chatMessages">
|
|
86
|
+
{localMessages.length > 0 ? (
|
|
87
|
+
localMessages.map((message: any) => (
|
|
88
|
+
<div key={message._id} ref={lastMessageRef}>
|
|
89
|
+
<Message message={message} />
|
|
90
|
+
</div>
|
|
91
|
+
))
|
|
92
|
+
) : (
|
|
93
|
+
<p style={{ textAlign: "center" }}>Send a message to start the conversation</p>
|
|
94
|
+
)}
|
|
95
|
+
</div>
|
|
96
|
+
|
|
58
97
|
);
|
|
59
98
|
};
|
|
60
99
|
export default Messages;
|
|
@@ -8,7 +8,7 @@ export const useMessageMutation = () => {
|
|
|
8
8
|
mutationFn: sendMessage,
|
|
9
9
|
onSuccess: () => {
|
|
10
10
|
console.log("Service submitted successfully!", "success");
|
|
11
|
-
queryClient.invalidateQueries({ queryKey: ['
|
|
11
|
+
queryClient.invalidateQueries({ queryKey: ['messages'] });
|
|
12
12
|
},
|
|
13
13
|
onError: (error: any) => {
|
|
14
14
|
console.error("Failed to submit service data:", error);
|