@pubuduth-aplicy/chat-ui 2.2.20 → 2.2.21

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.2.20",
3
+ "version": "2.2.21",
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,7 +13,7 @@ const Messages = () => {
13
13
  const { selectedConversation, setMessages, messages } = useChatUIStore();
14
14
  const { userId, socket } = useChatContext();
15
15
  const { ref, inView } = useInView();
16
- const lastMessageRef = useRef<HTMLDivElement>(null);
16
+ const chatContainerRef = useRef<HTMLDivElement>(null);
17
17
 
18
18
  const { data, fetchNextPage, isFetchingNextPage } = useInfiniteQuery({
19
19
  queryKey: ["messages", selectedConversation?._id, userId],
@@ -173,17 +173,21 @@ const Messages = () => {
173
173
  };
174
174
  }, [socket, selectedConversation?._id, setMessages, userId]);
175
175
 
176
- // Scroll to bottom when messages change
176
+ // Scroll to bottom when a new message is appended (sender or receiver)
177
+ const lastMessageId = messages[messages.length - 1]?._id;
177
178
  useEffect(() => {
178
- if (messages.length > 0) {
179
- setTimeout(() => {
180
- lastMessageRef.current?.scrollIntoView({
179
+ if (!lastMessageId) return;
180
+ const timer = setTimeout(() => {
181
+ const container = chatContainerRef.current;
182
+ if (container) {
183
+ container.scrollTo({
184
+ top: container.scrollHeight,
181
185
  behavior: "smooth",
182
- block: "nearest",
183
186
  });
184
- }, 100);
185
- }
186
- }, [messages.length]);
187
+ }
188
+ }, 100);
189
+ return () => clearTimeout(timer);
190
+ }, [lastMessageId]);
187
191
 
188
192
  // Track message visibility for read receipts
189
193
  // useEffect(() => {
@@ -211,6 +215,7 @@ const Messages = () => {
211
215
 
212
216
  return (
213
217
  <div
218
+ ref={chatContainerRef}
214
219
  className="chatMessages"
215
220
  style={{ overflowY: "auto", height: "100%", position: "relative" }}
216
221
  >
@@ -223,7 +228,6 @@ const Messages = () => {
223
228
  message ? (
224
229
  <div
225
230
  key={message._id}
226
- ref={lastMessageRef}
227
231
  data-message-id={message._id}
228
232
  style={{ flex: 1, minHeight: 0, overflowY: "auto" }}
229
233
  >