quivr/frontend/app/chat/[chatId]/components/ChatDialogueArea/utils/getMergedChatMessagesWithDoneStatusNotificationsReduced.ts
Mamadou DICKO 10af0c949a
feat: add notifications components (#1148)
* feat: rename ChatMessages to MessagesDialog

* feat: rename history to messages

* feat: add notifications to ChatContext

* feat: add getNotificationsFromChatHistory

* feat: add getMergedChatHistoryWithReducedNotifications

* refactor: update useActionBar

* refactor: update <ChatMessage />-n

* feat: update crawler and endpoint notifications content

* feat: display notifications

* test: update <MessageDialog /> tests

* feat: rename ChatMessage to QADisplay

* feat: rename ChatHistory to ChatMessage

* feat(upload): throw error when file missing

* feat: rename getMergedChatHistoryWithReducedNotifications to getMergedChatMessagesWithReducedNotifications

* feat: change history wording to message

* feat: move getFileIcon to lib

* refactor(NotificationDisplayer): move types to types.ts

* chore: improve ux

* feat: rename MessagesDialog to ChatDialogue
2023-09-12 17:44:15 +02:00

78 lines
2.3 KiB
TypeScript

import {
ChatItem,
ChatMessage,
ChatMessageItem,
Notification,
NotificationItem,
} from "../../../types";
import { ChatItemWithGroupedNotifications } from "../types";
// Function to create a ChatMessageItem from a message
const createChatMessageItem = (message: ChatMessage): ChatMessageItem => ({
item_type: "MESSAGE",
body: message,
});
// Function to create a NotificationItem from a notification
const createNotificationItem = (
notification: Notification
): NotificationItem => ({
item_type: "NOTIFICATION",
body: notification,
});
// Function to merge chat messages and notifications into a single array
const mergeChatMessagesAndNotifications = (
messages: ChatMessage[],
notifications: Notification[]
): ChatItem[] => [
...messages.map(createChatMessageItem),
...notifications.map(createNotificationItem),
];
// Function to compare two items by timestamp (message_time or datetime)
const compareItemsByTimestamp = (a: ChatItem, b: ChatItem): number => {
const timestampA =
a.item_type === "MESSAGE" ? a.body.message_time : a.body.datetime;
const timestampB =
b.item_type === "MESSAGE" ? b.body.message_time : b.body.datetime;
return Date.parse(timestampA) - Date.parse(timestampB);
};
// Main function to get merged chat messages with reduced notifications using reduce
export const getMergedChatMessagesWithDoneStatusNotificationsReduced = (
messages: ChatMessage[],
notifications: Notification[]
): ChatItemWithGroupedNotifications[] => {
const mergedChatItems = mergeChatMessagesAndNotifications(
messages,
notifications.filter((notification) => notification.status === "Done")
);
mergedChatItems.sort(compareItemsByTimestamp);
// Group notifications between messages
const groupedChatItemsByNotifications = mergedChatItems.reduce(
(result, item) => {
if (item.item_type === "MESSAGE") {
result.push(item);
} else {
const lastItem = result[result.length - 1];
if (lastItem !== undefined && lastItem.item_type === "NOTIFICATION") {
lastItem.body.push(item.body);
} else {
result.push({
item_type: "NOTIFICATION",
body: [item.body],
});
}
}
return result;
},
[] as ChatItemWithGroupedNotifications[]
);
return groupedChatItemsByNotifications;
};