import { MESSAGE_TYPES } from './constants'; /** * Determines if a message should be grouped with the next message * @param {Number} index - Index of the current message * @param {Array} searchList - Array of messages to check * @returns {Boolean} - Whether the message should be grouped with next */ export default (index, searchList) => { if (index === searchList.length - 1) return false; const current = searchList[index]; const next = searchList[index + 1]; if (next.status === 'failed') return false; const nextSenderId = next.senderId ?? next.sender?.id; const currentSenderId = current.senderId ?? current.sender?.id; const hasSameSender = nextSenderId === currentSenderId; const nextMessageType = next.messageType; const currentMessageType = current.messageType; const areBothTemplates = nextMessageType === MESSAGE_TYPES.TEMPLATE && currentMessageType === MESSAGE_TYPES.TEMPLATE; if (!hasSameSender || areBothTemplates) return false; if (currentMessageType !== nextMessageType) return false; // Check if messages are in the same minute by rounding down to nearest minute return Math.floor(next.createdAt / 60) === Math.floor(current.createdAt / 60); };