-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathchat-message-list.ts
More file actions
138 lines (119 loc) · 3.56 KB
/
chat-message-list.ts
File metadata and controls
138 lines (119 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { LitElement, html } from 'lit';
import { property } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
import { registerComponent } from '../common/definitions/register.js';
import IgcChatMessageComponent from './chat-message.js';
import { styles } from './themes/message-list.base.css.js';
import type { IgcMessage } from './types.js';
/**
*
* @element igc-chat-message-list
*
*/
export default class IgcChatMessageListComponent extends LitElement {
/** @private */
public static readonly tagName = 'igc-chat-message-list';
public static override styles = styles;
/* blazorSuppress */
public static register() {
registerComponent(IgcChatMessageListComponent, IgcChatMessageComponent);
}
@property({ reflect: true, attribute: false })
public messages: IgcMessage[] = [];
@property({ reflect: true, attribute: false })
public isAiResponding = false;
@property({ type: Boolean, attribute: 'disable-auto-scroll' })
public disableAutoScroll = false;
private formatDate(date: Date): string {
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
if (date.toDateString() === today.toDateString()) {
return 'Today';
}
if (date.toDateString() === yesterday.toDateString()) {
return 'Yesterday';
}
return date.toLocaleDateString('en-US', {
weekday: 'long',
month: 'short',
day: 'numeric',
});
}
private groupMessagesByDate(
messages: IgcMessage[]
): { date: string; messages: IgcMessage[] }[] {
const grouped: { [key: string]: IgcMessage[] } = {};
messages.forEach((message) => {
const dateStr = this.formatDate(message.timestamp);
if (!grouped[dateStr]) {
grouped[dateStr] = [];
}
grouped[dateStr].push(message);
});
return Object.keys(grouped).map((date) => ({
date,
messages: grouped[date],
}));
}
private scrollToBottom() {
requestAnimationFrame(() => {
const container = this.shadowRoot?.host as HTMLElement;
if (container) {
container.scrollTop = container.scrollHeight;
}
});
}
protected override updated() {
if (!this.disableAutoScroll) {
this.scrollToBottom();
}
}
protected override firstUpdated() {
if (!this.disableAutoScroll) {
this.scrollToBottom();
}
}
protected override render() {
const groupedMessages = this.groupMessagesByDate(this.messages);
return html`
<div class='message-container'></div>
<div class="message-list">
${repeat(
groupedMessages,
(group) => group.date,
(group) => html`
<div class="day-separator">${group.date}</div>
${repeat(
group.messages,
(message) => message.id,
(message) => html`
<igc-chat-message
.message=${message}
.isResponse=${message.isResponse}
></igc-chat-message>
`
)}
`
)}
${
this.isAiResponding
? html`
<div class="typing-indicator">
<div class="typing-dot"></div>
<div class="typing-dot"></div>
<div class="typing-dot"></div>
</div>
`
: ''
}
</div>
</div>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-chat-message-list': IgcChatMessageListComponent;
}
}