@progress/kendo-angular-conversational-ui
Version:
Kendo UI for Angular Conversational UI components
81 lines (80 loc) • 2.65 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { isDevMode } from '@angular/core';
/**
* @hidden
*/
export const isAuthor = (user, msg) => user && msg.author && user.id === msg.author.id;
const last = (arr) => arr[arr.length - 1];
const dateChanged = (curr, prev) => (curr && prev) && (prev.getDate() !== curr.getDate() ||
prev.getMonth() !== curr.getMonth() ||
prev.getFullYear() !== curr.getFullYear());
const addDateMarker = (acc, msg) => {
const timestamp = msg.timestamp;
const lastItem = last(acc);
if (!timestamp) {
return;
}
if (!lastItem || dateChanged(timestamp, lastItem.timestamp)) {
const dateMarker = {
type: 'date-marker',
timestamp: timestamp,
trackBy: timestamp.getTime()
};
acc.push(dateMarker);
}
};
const groupMessages = (acc, msg, isLastMessage) => {
const lastItem = last(acc);
let messages;
if (isDevMode() && !msg.author) {
throw new Error('Author must be set for message: ' + JSON.stringify(msg));
}
if (msg.typing && !isLastMessage) {
return;
}
if (lastItem && lastItem.type === 'message-group') {
messages = lastItem.messages;
}
if (messages && isAuthor(msg.author, last(messages))) {
messages.push(msg);
}
else {
acc.push({
type: 'message-group',
messages: [msg],
author: msg.author,
timestamp: msg.timestamp,
trackBy: msg
});
}
};
const groupItems = (total) => (acc, msg, index) => {
const isLastMessage = index === total - 1;
addDateMarker(acc, msg);
groupMessages(acc, msg, isLastMessage);
if (msg.attachments && msg.attachments.length > 1) {
acc.push({
type: 'attachment-group',
attachments: msg.attachments,
attachmentLayout: msg.attachmentLayout,
timestamp: msg.timestamp,
trackBy: msg
});
}
if (msg.suggestedActions && isLastMessage) {
acc.push({
type: 'action-group',
actions: msg.suggestedActions,
timestamp: msg.timestamp,
trackBy: msg
});
}
return acc;
};
/**
* @hidden
*/
export const chatView = (messages) => messages.reduce(groupItems(messages.length), []);