fix new message send stay bottom

This commit is contained in:
PhilReact 2024-12-14 18:06:40 +02:00
parent 1da9fb161e
commit dc7e0db0d8
4 changed files with 37 additions and 28 deletions

View File

@ -92,21 +92,6 @@ export const MessageQueueProvider = ({ children }) => {
// Remove the message from the queue after successful sending // Remove the message from the queue after successful sending
messageQueueRef.current.shift(); messageQueueRef.current.shift();
// Remove the message from queueChats
setQueueChats((prev) => {
const updatedChats = { ...prev };
if (updatedChats[groupDirectId]) {
updatedChats[groupDirectId] = updatedChats[groupDirectId].filter(
(item) => item.identifier !== identifier
);
// If no more chats for this group, delete the groupDirectId entry
if (updatedChats[groupDirectId].length === 0) {
delete updatedChats[groupDirectId];
}
}
return updatedChats;
});
} catch (error) { } catch (error) {
console.error('Message sending failed', error); console.error('Message sending failed', error);
@ -142,15 +127,25 @@ export const MessageQueueProvider = ({ children }) => {
// Method to process with new messages and groupDirectId // Method to process with new messages and groupDirectId
const processWithNewMessages = (newMessages, groupDirectId) => { const processWithNewMessages = (newMessages, groupDirectId) => {
let updatedNewMessages = newMessages
if (newMessages.length > 0) { if (newMessages.length > 0) {
messageQueueRef.current = messageQueueRef.current.filter((msg) => {
return !newMessages.some(newMsg => newMsg?.specialId === msg?.specialId);
});
// Remove corresponding entries in queueChats for the provided groupDirectId // Remove corresponding entries in queueChats for the provided groupDirectId
setQueueChats((prev) => { setQueueChats((prev) => {
const updatedChats = { ...prev }; const updatedChats = { ...prev };
if (updatedChats[groupDirectId]) { if (updatedChats[groupDirectId]) {
updatedNewMessages = newMessages?.map((msg)=> {
const findTempMsg = updatedChats[groupDirectId]?.find((msg2)=> msg2?.message?.specialId === msg?.specialId)
if(findTempMsg){
return {
...msg,
tempSignature: findTempMsg?.signature
}
}
return msg
})
updatedChats[groupDirectId] = updatedChats[groupDirectId].filter((chat) => { updatedChats[groupDirectId] = updatedChats[groupDirectId].filter((chat) => {
return !newMessages.some(newMsg => newMsg?.specialId === chat?.message?.specialId); return !newMessages.some(newMsg => newMsg?.specialId === chat?.message?.specialId);
}); });
@ -167,8 +162,23 @@ export const MessageQueueProvider = ({ children }) => {
} }
return updatedChats; return updatedChats;
}); });
} }
setTimeout(() => {
if(!messageQueueRef.current.find((msg) => msg?.groupDirectId === groupDirectId)){
setQueueChats((prev) => {
const updatedChats = { ...prev };
if (updatedChats[groupDirectId]) {
delete updatedChats[groupDirectId]
}
return updatedChats
}
)
}
}, 300);
return updatedNewMessages
}; };
return ( return (

View File

@ -118,9 +118,9 @@ export const ChatDirect = ({ myAddress, isNewChat, selectedDirect, setSelectedDi
data: encryptedMessages, data: encryptedMessages,
involvingAddress: selectedDirect?.address, involvingAddress: selectedDirect?.address,
}) })
.then((response) => { .then((decryptResponse) => {
if (!response?.error) { if (!decryptResponse?.error) {
processWithNewMessages(response, selectedDirect?.address); const response = processWithNewMessages(decryptResponse, selectedDirect?.address);
res(response); res(response);
if (isInitiated) { if (isInitiated) {

View File

@ -193,9 +193,9 @@ const [messageSize, setMessageSize] = useState(0)
const filterUIMessages = encryptedMessages.filter((item) => !isExtMsg(item.data)); const filterUIMessages = encryptedMessages.filter((item) => !isExtMsg(item.data));
const decodedUIMessages = decodeBase64ForUIChatMessages(filterUIMessages); const decodedUIMessages = decodeBase64ForUIChatMessages(filterUIMessages);
const combineUIAndExtensionMsgs = [...decodedUIMessages, ...response]; const combineUIAndExtensionMsgsBefore = [...decodedUIMessages, ...response];
processWithNewMessages( const combineUIAndExtensionMsgs = processWithNewMessages(
combineUIAndExtensionMsgs.map((item) => ({ combineUIAndExtensionMsgsBefore.map((item) => ({
...item, ...item,
...(item?.decryptedData || {}), ...(item?.decryptedData || {}),
})), })),

View File

@ -39,11 +39,10 @@ export const ChatList = ({
const scrollingIntervalRef = useRef(null); const scrollingIntervalRef = useRef(null);
const lastSeenUnreadMessageTimestamp = useRef(null); const lastSeenUnreadMessageTimestamp = useRef(null);
// Initialize the virtualizer // Initialize the virtualizer
const rowVirtualizer = useVirtualizer({ const rowVirtualizer = useVirtualizer({
count: messages.length, count: messages.length,
getItemKey: (index) => messages[index].signature, getItemKey: (index) => messages[index]?.tempSignature || messages[index].signature,
getScrollElement: () => parentRef?.current, getScrollElement: () => parentRef?.current,
estimateSize: useCallback(() => 80, []), // Provide an estimated height of items, adjust this as needed estimateSize: useCallback(() => 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 overscan: 10, // Number of items to render outside the visible area to improve smoothness