Skip to content

Commit

Permalink
Chat: Add renderMessage typification
Browse files Browse the repository at this point in the history
  • Loading branch information
marker-dao authored Aug 23, 2024
1 parent 18cac35 commit fcb3613
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
13 changes: 7 additions & 6 deletions packages/devextreme/js/__internal/ui/chat/chat_message_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { dxElementWrapper } from '@js/core/renderer';
import $ from '@js/core/renderer';
import { hasWindow } from '@js/core/utils/window';
import type { Message, User } from '@js/ui/chat';
import type { Message } from '@js/ui/chat';
import type dxScrollable from '@js/ui/scroll_view/ui.scrollable';
import Scrollable from '@js/ui/scroll_view/ui.scrollable';
import type { WidgetOptions } from '@js/ui/widget/ui.widget';
Expand Down Expand Up @@ -115,15 +115,17 @@ class MessageList extends Widget<MessageListOptions> {
});
}

_renderMessage(message: Message, newItems: Message[], sender: User): void {
_renderMessage(message: Message, newItems: Message[]): void {
const sender = message.author;

this._setOptionWithoutOptionChange('items', newItems);

const lastMessageGroup = this._messageGroups?.[this._messageGroups.length - 1];

if (lastMessageGroup) {
const lastMessageGroupUserId = lastMessageGroup.option('items')[0].author?.id;

if (sender.id === lastMessageGroupUserId) {
if (sender?.id === lastMessageGroupUserId) {
lastMessageGroup._renderMessage(message);

this._scrollContentToLastMessageGroup();
Expand All @@ -132,7 +134,7 @@ class MessageList extends Widget<MessageListOptions> {
}
}

this._createMessageGroupComponent([message], sender.id);
this._createMessageGroupComponent([message], sender?.id);
this._scrollContentToLastMessageGroup();
}

Expand Down Expand Up @@ -182,8 +184,7 @@ class MessageList extends Widget<MessageListOptions> {
} else {
const newMessage = value[value.length - 1];

// @ts-expect-error
this._renderMessage(newMessage, value, newMessage.author);
this._renderMessage(newMessage, value);
}
}

Expand Down
9 changes: 8 additions & 1 deletion packages/devextreme/js/ui/chat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,14 @@ export interface dxChatOptions extends WidgetOptions<dxChat> {
* @namespace DevExpress.ui
* @public
*/
export default class dxChat extends Widget<Properties> { }
export default class dxChat extends Widget<Properties> {
/**
* @docid
* @publicName renderMessage(message)
* @public
*/
renderMessage(message: Message): void;
}

/** @public */
export type ExplicitTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ QUnit.module('renderMessage', moduleConfig, () => {
text: 'NEW MESSAGE',
};

this.instance.renderMessage(newMessage, author);
this.instance.renderMessage(newMessage);

const { items } = this.instance.option();
const lastItem = items[items.length - 1];
Expand All @@ -245,7 +245,7 @@ QUnit.module('renderMessage', moduleConfig, () => {
text: 'NEW MESSAGE',
};

this.instance.renderMessage(newMessage, author);
this.instance.renderMessage(newMessage);

const { items } = this.instance._messageList.option();
const lastItem = items[items.length - 1];
Expand All @@ -270,7 +270,7 @@ QUnit.module('renderMessage', moduleConfig, () => {

assert.strictEqual($(lastMessageGroupElement).find(`.${CHAT_MESSAGE_BUBBLE_CLASS}`).length, 1);

this.instance.renderMessage(newMessage, author);
this.instance.renderMessage(newMessage);

const { items: messages } = lastMessageGroup.option();
const lastMessage = messages[messages.length - 1];
Expand All @@ -296,7 +296,7 @@ QUnit.module('renderMessage', moduleConfig, () => {
assert.strictEqual(messageGroups.length, 2);
assert.strictEqual(getMessageGroupElements().length, 2);

this.instance.renderMessage(newMessage, author);
this.instance.renderMessage(newMessage);

assert.strictEqual(messageGroups.length, 3);
assert.strictEqual(getMessageGroupElements().length, 3);
Expand All @@ -319,7 +319,7 @@ QUnit.module('renderMessage', moduleConfig, () => {

assert.strictEqual(getMessageGroups().length, 0);

this.instance.renderMessage(newMessage, author);
this.instance.renderMessage(newMessage);

assert.strictEqual(getMessageGroups().length, 1);
});
Expand All @@ -346,7 +346,7 @@ QUnit.module('renderMessage', moduleConfig, () => {
let $bubbles = getLastMessageGroupBubbles();
assert.strictEqual($bubbles.eq($bubbles.length - 1).hasClass(CHAT_MESSAGE_BUBBLE_LAST_CLASS), true);

this.instance.renderMessage(newMessage, author);
this.instance.renderMessage(newMessage);

$bubbles = getLastMessageGroupBubbles();
assert.strictEqual($bubbles.eq($bubbles.length - 2).hasClass(CHAT_MESSAGE_BUBBLE_LAST_CLASS), false);
Expand All @@ -361,7 +361,7 @@ QUnit.module('renderMessage', moduleConfig, () => {
text,
};

this.instance.renderMessage(newMessage, author);
this.instance.renderMessage(newMessage);

const messageGroups = this.$element.find(`.${CHAT_MESSAGE_GROUP_CLASS}`);
const lastMessageGroup = messageGroups[messageGroups.length - 1];
Expand Down Expand Up @@ -697,7 +697,7 @@ QUnit.module('Scrolling', moduleConfig, () => {
assert.strictEqual($item, lastMessageGroup);
};

this.instance.renderMessage(newMessage, author);
this.instance.renderMessage(newMessage);
});
});
});
7 changes: 6 additions & 1 deletion packages/devextreme/ts/dx.all.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9415,7 +9415,12 @@ declare module DevExpress.ui {
/**
* [descr:dxChat]
*/
export class dxChat extends Widget<DevExpress.ui.dxChat.Properties> {}
export class dxChat extends Widget<DevExpress.ui.dxChat.Properties> {
/**
* [descr:dxChat.renderMessage(message)]
*/
renderMessage(message: DevExpress.ui.dxChat.Message): void;
}
module dxChat {
/**
* [descr:_ui_chat_DisposingEvent]
Expand Down

0 comments on commit fcb3613

Please sign in to comment.