Skip to content

Commit

Permalink
refactoring messages code for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
OvidijusParsiunas committed Jan 4, 2025
1 parent cf6d5ed commit daa0617
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions component/src/views/chat/messages/html/htmlMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export class HTMLMessages {
}

// prettier-ignore
private static overwrite(messages: MessagesBase, html: string, role: string, messagesEls: MessageElements[]) {
private static overwrite(messages: MessagesBase, html: string, role: string, messageElementRefs: MessageElements[]) {
const {messageToElements: msgToEls} = messages;
const overwrittenElements = MessageUtils.overwriteMessage(
msgToEls, messagesEls, html, role, 'html', HTMLMessages.HTML_BUBBLE_CLASS);
msgToEls, messageElementRefs, html, role, 'html', HTMLMessages.HTML_BUBBLE_CLASS);
if (overwrittenElements) {
HTMLMessages.overwriteElements(messages, html, overwrittenElements);
}
Expand All @@ -49,9 +49,9 @@ export class HTMLMessages {
// prettier-ignore
public static add(
messages: MessagesBase, html: string, role: string,
messagesEls: MessageElements[], overwrite?: Overwrite, isTop = false) {
messageElementRefs: MessageElements[], overwrite?: Overwrite, isTop = false) {
if (overwrite?.status) {
const overwrittenElements = this.overwrite(messages, html, role, messagesEls);
const overwrittenElements = this.overwrite(messages, html, role, messageElementRefs);
if (overwrittenElements) return overwrittenElements;
overwrite.status = false;
}
Expand Down
14 changes: 7 additions & 7 deletions component/src/views/chat/messages/utils/messageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export class MessageUtils {
private static readonly POSITION_MIDDLE_MESSAGE_CLASS = 'deep-chat-middle-message';
private static readonly POSITION_BOTTOM_MESSAGE_CLASS = 'deep-chat-bottom-message';

public static getLastElementsByClass(messagesElements: MessageElements[], classes: string[], avoidedClasses?: string[]) {
for (let i = messagesElements.length - 1; i >= 0; i -= 1) {
const elements = messagesElements[i];
public static getLastElementsByClass(messageElementRefs: MessageElements[], classes: string[], avoidClasses?: string[]) {
for (let i = messageElementRefs.length - 1; i >= 0; i -= 1) {
const elements = messageElementRefs[i];
if (elements.bubbleElement.classList.contains(classes[0])) {
const notFound = classes.slice(1).find((className) => !elements.bubbleElement.classList.contains(className));
if (!notFound) {
if (avoidedClasses) {
const avoided = avoidedClasses.find((className) => elements.bubbleElement.classList.contains(className));
if (avoidClasses) {
const avoided = avoidClasses.find((className) => elements.bubbleElement.classList.contains(className));
if (!avoided) return elements;
} else {
return elements;
Expand Down Expand Up @@ -61,11 +61,11 @@ export class MessageUtils {
// IMPORTANT: If the overwrite message does not contain a role property it will look for the last 'ai' role message
// and if messages have custom roles, it will still look to update the last 'ai' role message
// prettier-ignore
public static overwriteMessage(messageToElements: MessageToElements, messagesElements: MessageElements[],
public static overwriteMessage(messageToElements: MessageToElements, messageElementRefs: MessageElements[],
content: string, role: string, contentType: 'text' | 'html', className: string) {
// not sure if LoadingStyle.LOADING_MESSAGE_TEXT_CLASS is needed
const elements = MessageUtils.getLastElementsByClass(
messagesElements, [MessageUtils.getRoleClass(role), className], [LoadingStyle.BUBBLE_CLASS]);
messageElementRefs, [MessageUtils.getRoleClass(role), className], [LoadingStyle.BUBBLE_CLASS]);
const lastMessage = MessageUtils.getLastMessage(messageToElements, role, contentType);
if (lastMessage) lastMessage[contentType] = content;
return elements;
Expand Down

0 comments on commit daa0617

Please sign in to comment.