Skip to content

Commit

Permalink
revert solution with cached last header date
Browse files Browse the repository at this point in the history
  • Loading branch information
Zedwag committed Oct 15, 2024
1 parent 64eaa6b commit 8c78826
Showing 1 changed file with 7 additions and 46 deletions.
53 changes: 7 additions & 46 deletions packages/devextreme/js/__internal/ui/chat/messagelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface Properties extends WidgetOptions<MessageList> {
class MessageList extends Widget<Properties> {
private _messageGroups?: MessageGroup[];

private _lastMessageDate?: null | string | number | Date;

private _containerClientHeight!: number;

private _scrollable!: Scrollable<unknown>;
Expand All @@ -55,6 +57,7 @@ class MessageList extends Widget<Properties> {
super._init();

this._messageGroups = [];
this._lastMessageDate = null;
}

_initMarkup(): void {
Expand Down Expand Up @@ -173,10 +176,7 @@ class MessageList extends Widget<Properties> {
});
}

_shouldAddDayHeader(
timestamp: undefined | string | number | Date,
currentMessageGroupItems: Message[] = [],
): boolean {
_shouldAddDayHeader(timestamp: undefined | string | number | Date): boolean {
const { showDayHeaders } = this.option();

if (!showDayHeaders) {
Expand All @@ -189,52 +189,14 @@ class MessageList extends Widget<Properties> {
return false;
}

const lastDayHeaderDate = this._getLastDayHeaderDate(currentMessageGroupItems);

return !dateUtils.sameDate(lastDayHeaderDate, deserializedDate);
}

_getLastDayHeaderDate(currentMessageGroupItems: Message[] = []): Date | null {
let lastDayHeaderDate = null;
const messageGroups = this._messageGroups ?? [];

let index = currentMessageGroupItems.length - 1;

while (index >= 0 && lastDayHeaderDate === null) {
const messageTimestamp = currentMessageGroupItems[index].timestamp;

if (messageTimestamp) {
lastDayHeaderDate = dateSerialization.deserializeDate(messageTimestamp);
}
index -= 1;
}

index = messageGroups.length - 1;

while (index >= 0 && lastDayHeaderDate === null) {
const { items } = messageGroups[index].option();

const messageGroupTimestampList: (Date | string | number)[] = [];
items.forEach((item: Message) => {
if (item.timestamp) {
messageGroupTimestampList.push(item.timestamp);
}
});

if (messageGroupTimestampList.length > 0) {
lastDayHeaderDate = dateSerialization.deserializeDate(messageGroupTimestampList.at(-1));
}

index -= 1;
}

return lastDayHeaderDate;
return !dateUtils.sameDate(this._lastMessageDate, deserializedDate);
}

_createDayHeader(timestamp: string | number | Date | undefined): void {
const deserializedDate = dateSerialization.deserializeDate(timestamp);
const today = new Date();
const yesterday = new Date(new Date().setDate(today.getDate() - 1));
this._lastMessageDate = deserializedDate;

let headerDate = deserializedDate.toLocaleDateString(undefined, {
day: '2-digit',
Expand Down Expand Up @@ -270,9 +232,8 @@ class MessageList extends Widget<Properties> {

items.forEach((item, index) => {
const newMessageGroupItem = item ?? {};
const { timestamp } = newMessageGroupItem;
const id = newMessageGroupItem.author?.id;
const shouldCreateDayHeader = this._shouldAddDayHeader(timestamp, currentMessageGroupItems);
const shouldCreateDayHeader = this._shouldAddDayHeader(newMessageGroupItem.timestamp);
const isTimeoutExceeded = this._isTimeoutExceeded(
currentMessageGroupItems[currentMessageGroupItems.length - 1] ?? {},
item,
Expand Down

0 comments on commit 8c78826

Please sign in to comment.