a bit more functional way to group/separate chat list

This commit is contained in:
Henry Jameson 2026-07-07 20:07:17 +03:00
commit cab0e9a881
2 changed files with 37 additions and 67 deletions

View file

@ -98,13 +98,11 @@ const Chat = {
} }
}, },
chatViewItems() { chatViewItems() {
return chatService.getView(this.currentChatMessageService) return chatService.getView(this.currentChatMessageService?.messages)
}, },
newMessageCount() { newMessageCount() {
return ( return this.currentChatMessageService?.newMessageCount
this.currentChatMessageService &&
this.currentChatMessageService.newMessageCount
)
}, },
streamingEnabled() { streamingEnabled() {
return ( return (

View file

@ -160,81 +160,53 @@ const resetNewMessageCount = (storage) => {
} }
// Inserts date separators and marks the head and tail if it's the chain of messages made by the same user // Inserts date separators and marks the head and tail if it's the chain of messages made by the same user
const getView = (storage) => { const getView = (items = []) => {
if (!storage) { const messages = orderBy(items, ['pending', 'id'], ['asc', 'asc'])
return [] return messages.reduceRight((acc, message, index) => {
}
const result = []
const messages = orderBy(storage.messages, ['pending', 'id'], ['asc', 'asc'])
const firstMessage = messages[0]
let previousMessage = messages[messages.length - 1]
let currentMessageChainId
if (firstMessage) {
const date = new Date(firstMessage.created_at)
date.setHours(0, 0, 0, 0)
result.push({
type: 'date',
date,
id: date.getTime().toString(),
})
}
let afterDate = false
for (let i = 0; i < messages.length; i++) {
const message = messages[i]
const nextMessage = messages[i + 1]
const date = new Date(message.created_at) const date = new Date(message.created_at)
date.setHours(0, 0, 0, 0)
// insert date separator and start a new message chain const olderMessage = messages[index - 1]
if (previousMessage && previousMessage.date < date) { const newerMessage = messages[index + 1]
result.push({ const newerItem = acc[acc.length - 1]
type: 'date',
date,
id: date.getTime().toString(),
})
previousMessage.isTail = true const diff = message.created_at - (olderMessage?.created_at || 0)
currentMessageChainId = undefined const MAX_DIFF = 1000 * 60 // 5 minutes
afterDate = true
}
const object = { const chatItem = {
type: 'message', type: 'message',
data: message, data: message,
date, date,
id: message.id, id: message.id,
messageChainId: currentMessageChainId, isTail: true,
isHead: true,
} }
// end a message chian if (newerItem == null) {
if ((nextMessage && nextMessage.account_id) !== message.account_id) { chatItem.messageChainId = uniqueId()
object.isTail = true } else {
currentMessageChainId = undefined if (newerItem.type === 'date') {
chatItem.messageChainId = uniqueId()
} else if (newerItem.type === 'message') {
if (newerItem.data.account_id !== message.account_id) {
chatItem.messageChainId = uniqueId()
} else {
chatItem.messageChainId = newerItem.messageChainId
chatItem.isTail = false
newerItem.isHead = false
}
}
} }
// start a new message chain if (diff > MAX_DIFF || !olderMessage) {
if ( return [...acc, chatItem, {
(previousMessage && type: 'date',
previousMessage.data && date,
previousMessage.data.account_id) !== message.account_id || id: date.getTime().toString(),
afterDate }]
) { } else {
currentMessageChainId = uniqueId() return [...acc, chatItem]
object.isHead = true
object.messageChainId = currentMessageChainId
} }
}, []).reverse()
result.push(object)
previousMessage = object
afterDate = false
}
return result
} }
const ChatService = { const ChatService = {