diff --git a/src/components/Chat/ChatDirect.tsx b/src/components/Chat/ChatDirect.tsx index 916e7ff..14470a7 100644 --- a/src/components/Chat/ChatDirect.tsx +++ b/src/components/Chat/ChatDirect.tsx @@ -376,6 +376,7 @@ const clearEditorContent = () => { const onReply = useCallback((message)=> { setReplyMessage(message) + editorRef?.current?.chain().focus() }, []) diff --git a/src/components/Chat/ChatList.tsx b/src/components/Chat/ChatList.tsx index 6c990cc..a8a9a9c 100644 --- a/src/components/Chat/ChatList.tsx +++ b/src/components/Chat/ChatList.tsx @@ -1,10 +1,25 @@ -import React, { useCallback, useState, useEffect, useRef, useMemo } from 'react'; -import { useVirtualizer } from '@tanstack/react-virtual'; -import { MessageItem } from './MessageItem'; -import { subscribeToEvent, unsubscribeFromEvent } from '../../utils/events'; -import { useInView } from 'react-intersection-observer' +import React, { + useCallback, + useState, + useEffect, + useRef, + useMemo, +} from "react"; +import { useVirtualizer } from "@tanstack/react-virtual"; +import { MessageItem } from "./MessageItem"; +import { subscribeToEvent, unsubscribeFromEvent } from "../../utils/events"; +import { useInView } from "react-intersection-observer"; -export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onReply, handleReaction, chatReferences, tempChatReferences }) => { +export const ChatList = ({ + initialMessages, + myAddress, + tempMessages, + chatId, + onReply, + handleReaction, + chatReferences, + tempChatReferences, +}) => { const parentRef = useRef(); const [messages, setMessages] = useState(initialMessages); const [showScrollButton, setShowScrollButton] = useState(false); @@ -16,7 +31,7 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR // useEffect(() => { // if (inView) { - + // } // }, [inView]) // Update message list with unique signatures and tempMessages @@ -30,9 +45,9 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR } }); - const uniqueInitialMessages = Array.from(uniqueInitialMessagesMap.values()).sort( - (a, b) => a.timestamp - b.timestamp - ); + const uniqueInitialMessages = Array.from( + uniqueInitialMessagesMap.values() + ).sort((a, b) => a.timestamp - b.timestamp); const totalMessages = [...uniqueInitialMessages, ...(tempMessages || [])]; if (totalMessages.length === 0) return; @@ -40,10 +55,12 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR setMessages(totalMessages); setTimeout(() => { - const hasUnreadMessages = totalMessages.some((msg) => msg.unread && !msg?.chatReference); + const hasUnreadMessages = totalMessages.some( + (msg) => msg.unread && !msg?.chatReference + ); if (parentRef.current) { const { scrollTop, scrollHeight, clientHeight } = parentRef.current; - const atBottom = scrollTop + clientHeight >= scrollHeight - 10; // Adjust threshold as needed + const atBottom = scrollTop + clientHeight >= scrollHeight - 10; // Adjust threshold as needed if (!atBottom && hasUnreadMessages) { setShowScrollButton(hasUnreadMessages); } else { @@ -60,12 +77,11 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR const scrollToBottom = (initialMsgs) => { const index = initialMsgs ? initialMsgs.length - 1 : messages.length - 1; if (rowVirtualizer) { - rowVirtualizer.scrollToIndex(index, { align: 'end' }); + rowVirtualizer.scrollToIndex(index, { align: "end" }); } - handleMessageSeen() + handleMessageSeen(); }; - const handleMessageSeen = useCallback(() => { setMessages((prevMessages) => prevMessages.map((msg) => ({ @@ -73,7 +89,7 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR unread: false, })) ); - setShowScrollButton(false) + setShowScrollButton(false); }, []); // const scrollToBottom = (initialMsgs) => { @@ -83,24 +99,22 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR // } // }; - const sentNewMessageGroupFunc = useCallback(() => { scrollToBottom(); }, [messages]); useEffect(() => { - subscribeToEvent('sent-new-message-group', sentNewMessageGroupFunc); + subscribeToEvent("sent-new-message-group", sentNewMessageGroupFunc); return () => { - unsubscribeFromEvent('sent-new-message-group', sentNewMessageGroupFunc); + unsubscribeFromEvent("sent-new-message-group", sentNewMessageGroupFunc); }; }, [sentNewMessageGroupFunc]); - const lastSignature = useMemo(()=> { - if(!messages || messages?.length === 0) return null - const lastIndex = messages.length - 1 - return messages[lastIndex]?.signature - }, [messages]) - + const lastSignature = useMemo(() => { + if (!messages || messages?.length === 0) return null; + const lastIndex = messages.length - 1; + return messages[lastIndex]?.signature; + }, [messages]); // Initialize the virtualizer const rowVirtualizer = useVirtualizer({ @@ -108,125 +122,147 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR getScrollElement: () => parentRef.current, estimateSize: () => 80, // Provide an estimated height of items, adjust this as needed overscan: 10, // Number of items to render outside the visible area to improve smoothness - measureElement: - typeof window !== 'undefined' && - navigator.userAgent.indexOf('Firefox') === -1 - ? element => { - return element?.getBoundingClientRect().height - } - : undefined, + getItemKey: React.useCallback( + (index) => messages[index].signature, + [messages] + ), }); return ( -