@scalemule/chat 0.0.4 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ChatClient-COmdEJ11.d.ts → ChatClient-DQPHdUHX.d.cts} +21 -2
- package/dist/{ChatClient-BoZaTtyM.d.cts → ChatClient-DtUKF-4c.d.ts} +21 -2
- package/dist/chat.embed.global.js +1 -1
- package/dist/chat.umd.global.js +288 -12
- package/dist/{chunk-ZLMMNFZL.js → chunk-5O5YLRJL.js} +386 -16
- package/dist/chunk-GTMAK3IA.js +285 -0
- package/dist/chunk-TRCELAZQ.cjs +287 -0
- package/dist/{chunk-YDLRISR7.cjs → chunk-W2PWFS3E.cjs} +386 -15
- package/dist/constants.d.ts +9 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/core/ChatClient.d.ts +96 -0
- package/dist/core/ChatClient.d.ts.map +1 -0
- package/dist/core/EventEmitter.d.ts +11 -0
- package/dist/core/EventEmitter.d.ts.map +1 -0
- package/dist/core/MessageCache.d.ts +18 -0
- package/dist/core/MessageCache.d.ts.map +1 -0
- package/dist/core/OfflineQueue.d.ts +19 -0
- package/dist/core/OfflineQueue.d.ts.map +1 -0
- package/dist/element.cjs +542 -51
- package/dist/element.d.ts +2 -2
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +541 -50
- package/dist/embed/index.d.ts +2 -0
- package/dist/embed/index.d.ts.map +1 -0
- package/dist/factory.d.ts +8 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/iframe.d.cts +1 -1
- package/dist/iframe.d.ts +3 -5
- package/dist/iframe.d.ts.map +1 -0
- package/dist/index.cjs +34 -5
- package/dist/index.d.cts +93 -4
- package/dist/index.d.ts +8 -77
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -4
- package/dist/react-components/ChatInput.d.ts +16 -0
- package/dist/react-components/ChatInput.d.ts.map +1 -0
- package/dist/react-components/ChatMessageItem.d.ts +13 -0
- package/dist/react-components/ChatMessageItem.d.ts.map +1 -0
- package/dist/react-components/ChatMessageList.d.ts +15 -0
- package/dist/react-components/ChatMessageList.d.ts.map +1 -0
- package/dist/react-components/ChatThread.d.ts +12 -0
- package/dist/react-components/ChatThread.d.ts.map +1 -0
- package/dist/react-components/ConversationList.d.ts +13 -0
- package/dist/react-components/ConversationList.d.ts.map +1 -0
- package/dist/react-components/EmojiPicker.d.ts +8 -0
- package/dist/react-components/EmojiPicker.d.ts.map +1 -0
- package/dist/react-components/index.d.ts +8 -0
- package/dist/react-components/index.d.ts.map +1 -0
- package/dist/react-components/theme.d.ts +19 -0
- package/dist/react-components/theme.d.ts.map +1 -0
- package/dist/react-components/utils.d.ts +4 -0
- package/dist/react-components/utils.d.ts.map +1 -0
- package/dist/react.cjs +1213 -53
- package/dist/react.d.cts +100 -4
- package/dist/react.d.ts +38 -15
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +1167 -18
- package/dist/shared/ChatController.d.ts +65 -0
- package/dist/shared/ChatController.d.ts.map +1 -0
- package/dist/shared/upload.d.ts +3 -0
- package/dist/shared/upload.d.ts.map +1 -0
- package/dist/support-widget.global.js +485 -157
- package/dist/support.d.ts +99 -0
- package/dist/support.d.ts.map +1 -0
- package/dist/transport/HttpTransport.d.ts +20 -0
- package/dist/transport/HttpTransport.d.ts.map +1 -0
- package/dist/transport/WebSocketTransport.d.ts +78 -0
- package/dist/transport/WebSocketTransport.d.ts.map +1 -0
- package/dist/{types-BmD7f1gV.d.cts → types-COPVrm3K.d.cts} +25 -1
- package/dist/{types-BmD7f1gV.d.ts → types-COPVrm3K.d.ts} +25 -1
- package/dist/types.d.ts +271 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/umd.d.ts +6 -0
- package/dist/umd.d.ts.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/widget/icons.d.ts +8 -0
- package/dist/widget/icons.d.ts.map +1 -0
- package/dist/widget/index.d.ts +2 -0
- package/dist/widget/index.d.ts.map +1 -0
- package/dist/widget/storage.d.ts +5 -0
- package/dist/widget/storage.d.ts.map +1 -0
- package/dist/widget/styles.d.ts +3 -0
- package/dist/widget/styles.d.ts.map +1 -0
- package/package.json +5 -2
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface QueuedMessage {
|
|
2
|
+
conversationId: string;
|
|
3
|
+
content: string;
|
|
4
|
+
message_type: string;
|
|
5
|
+
attachments?: unknown[];
|
|
6
|
+
timestamp: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class OfflineQueue {
|
|
9
|
+
private queue;
|
|
10
|
+
private enabled;
|
|
11
|
+
constructor(enabled?: boolean);
|
|
12
|
+
enqueue(conversationId: string, content: string, messageType?: string, attachments?: unknown[]): void;
|
|
13
|
+
drain(): QueuedMessage[];
|
|
14
|
+
get size(): number;
|
|
15
|
+
private save;
|
|
16
|
+
private load;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=OfflineQueue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OfflineQueue.d.ts","sourceRoot":"","sources":["../../src/core/OfflineQueue.ts"],"names":[],"mappings":"AAEA,UAAU,aAAa;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,OAAO,EAAE,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,qBAAa,YAAY;IACvB,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,OAAO,CAAU;gBAEb,OAAO,UAAO;IAO1B,OAAO,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,SAAS,EAAE,WAAW,CAAC,EAAE,OAAO,EAAE,GAAG,IAAI;IAYrG,KAAK,IAAI,aAAa,EAAE;IAOxB,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,OAAO,CAAC,IAAI;IAUZ,OAAO,CAAC,IAAI;CAYb"}
|
package/dist/element.cjs
CHANGED
|
@@ -1,17 +1,142 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkTRCELAZQ_cjs = require('./chunk-TRCELAZQ.cjs');
|
|
4
|
+
var chunkW2PWFS3E_cjs = require('./chunk-W2PWFS3E.cjs');
|
|
4
5
|
|
|
5
6
|
// src/element.ts
|
|
7
|
+
var REACTION_EMOJIS = ["\u{1F44D}", "\u2764\uFE0F", "\u{1F602}", "\u{1F389}", "\u{1F62E}", "\u{1F440}"];
|
|
8
|
+
function escapeHtml(value) {
|
|
9
|
+
return String(value ?? "").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
10
|
+
}
|
|
11
|
+
function sanitizeUrl(url) {
|
|
12
|
+
if (!url) return null;
|
|
13
|
+
try {
|
|
14
|
+
const parsed = new URL(url, typeof window !== "undefined" ? window.location.href : "https://scalemule.com");
|
|
15
|
+
if (["http:", "https:", "blob:"].includes(parsed.protocol)) {
|
|
16
|
+
return parsed.toString();
|
|
17
|
+
}
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
function sanitizeThemeColor(value) {
|
|
24
|
+
const candidate = value?.trim();
|
|
25
|
+
if (!candidate) return "#2563eb";
|
|
26
|
+
if (typeof document !== "undefined") {
|
|
27
|
+
const style = document.createElement("span").style;
|
|
28
|
+
style.color = "";
|
|
29
|
+
style.color = candidate;
|
|
30
|
+
if (style.color) {
|
|
31
|
+
return candidate;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (/^#[0-9a-f]{3,8}$/i.test(candidate)) {
|
|
35
|
+
return candidate;
|
|
36
|
+
}
|
|
37
|
+
return "#2563eb";
|
|
38
|
+
}
|
|
39
|
+
function formatDayLabel(value) {
|
|
40
|
+
return new Date(value).toLocaleDateString([], {
|
|
41
|
+
month: "short",
|
|
42
|
+
day: "numeric",
|
|
43
|
+
year: "numeric"
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function isSameDay(left, right) {
|
|
47
|
+
const leftDate = new Date(left);
|
|
48
|
+
const rightDate = new Date(right);
|
|
49
|
+
return leftDate.getFullYear() === rightDate.getFullYear() && leftDate.getMonth() === rightDate.getMonth() && leftDate.getDate() === rightDate.getDate();
|
|
50
|
+
}
|
|
51
|
+
function getUnreadIndex(messages, unreadSince) {
|
|
52
|
+
if (!unreadSince) return -1;
|
|
53
|
+
return messages.findIndex(
|
|
54
|
+
(message) => new Date(message.created_at).getTime() > new Date(unreadSince).getTime()
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
function renderAttachment(attachment) {
|
|
58
|
+
const fileName = escapeHtml(attachment.file_name);
|
|
59
|
+
const url = sanitizeUrl(attachment.presigned_url ?? void 0);
|
|
60
|
+
if (!url) {
|
|
61
|
+
return `<div class="attachment attachment-link">${fileName}</div>`;
|
|
62
|
+
}
|
|
63
|
+
if (attachment.mime_type.startsWith("image/")) {
|
|
64
|
+
return `<img class="attachment attachment-image" src="${escapeHtml(url)}" alt="${fileName}" />`;
|
|
65
|
+
}
|
|
66
|
+
if (attachment.mime_type.startsWith("video/")) {
|
|
67
|
+
return `<video class="attachment attachment-video" src="${escapeHtml(url)}" controls></video>`;
|
|
68
|
+
}
|
|
69
|
+
if (attachment.mime_type.startsWith("audio/")) {
|
|
70
|
+
return `<audio class="attachment attachment-audio" src="${escapeHtml(url)}" controls></audio>`;
|
|
71
|
+
}
|
|
72
|
+
return `<a class="attachment attachment-link" href="${escapeHtml(url)}" target="_blank" rel="noreferrer">${fileName}</a>`;
|
|
73
|
+
}
|
|
74
|
+
function renderMessage(message, currentUserId, showReactionPicker = false) {
|
|
75
|
+
const isOwn = Boolean(currentUserId && message.sender_id === currentUserId);
|
|
76
|
+
const attachments = (message.attachments ?? []).map(renderAttachment).join("");
|
|
77
|
+
const edited = message.is_edited ? '<span class="message-edited">edited</span>' : "";
|
|
78
|
+
const reactions = (message.reactions ?? []).map((reaction) => {
|
|
79
|
+
const reacted = Boolean(currentUserId && reaction.user_ids.includes(currentUserId));
|
|
80
|
+
return `
|
|
81
|
+
<button
|
|
82
|
+
class="reaction-badge ${reacted ? "reaction-badge-active" : ""}"
|
|
83
|
+
type="button"
|
|
84
|
+
data-action="toggle-reaction"
|
|
85
|
+
data-message-id="${escapeHtml(message.id)}"
|
|
86
|
+
data-emoji="${escapeHtml(reaction.emoji)}"
|
|
87
|
+
data-reacted="${reacted ? "true" : "false"}"
|
|
88
|
+
>
|
|
89
|
+
${escapeHtml(reaction.emoji)} ${reaction.count}
|
|
90
|
+
</button>
|
|
91
|
+
`;
|
|
92
|
+
}).join("");
|
|
93
|
+
const picker = showReactionPicker ? `
|
|
94
|
+
<div class="reaction-picker">
|
|
95
|
+
${REACTION_EMOJIS.map(
|
|
96
|
+
(emoji) => `
|
|
97
|
+
<button
|
|
98
|
+
class="reaction-picker-btn"
|
|
99
|
+
type="button"
|
|
100
|
+
data-action="add-reaction"
|
|
101
|
+
data-message-id="${escapeHtml(message.id)}"
|
|
102
|
+
data-emoji="${escapeHtml(emoji)}"
|
|
103
|
+
>
|
|
104
|
+
${escapeHtml(emoji)}
|
|
105
|
+
</button>
|
|
106
|
+
`
|
|
107
|
+
).join("")}
|
|
108
|
+
</div>
|
|
109
|
+
` : "";
|
|
110
|
+
return `
|
|
111
|
+
<div class="message ${isOwn ? "message-own" : "message-other"}">
|
|
112
|
+
<div class="message-bubble">
|
|
113
|
+
${message.content ? `<div class="message-content">${escapeHtml(message.content)}</div>` : ""}
|
|
114
|
+
${attachments}
|
|
115
|
+
</div>
|
|
116
|
+
<div class="message-meta">
|
|
117
|
+
<span>${new Date(message.created_at).toLocaleTimeString([], { hour: "numeric", minute: "2-digit" })}</span>
|
|
118
|
+
${edited}
|
|
119
|
+
<button class="message-action" type="button" data-action="toggle-picker" data-message-id="${escapeHtml(message.id)}">React</button>
|
|
120
|
+
</div>
|
|
121
|
+
${reactions ? `<div class="reactions">${reactions}</div>` : ""}
|
|
122
|
+
${picker}
|
|
123
|
+
</div>
|
|
124
|
+
`;
|
|
125
|
+
}
|
|
6
126
|
var ScaleMuleChatElement = class extends HTMLElement {
|
|
7
127
|
constructor() {
|
|
8
128
|
super();
|
|
9
129
|
this.client = null;
|
|
10
|
-
this.
|
|
130
|
+
this.controller = null;
|
|
131
|
+
this.cleanupFns = [];
|
|
132
|
+
this.pendingAttachments = [];
|
|
133
|
+
this.currentState = null;
|
|
134
|
+
this.openReactionMessageId = null;
|
|
135
|
+
this.didScrollToUnread = false;
|
|
11
136
|
this.shadow = this.attachShadow({ mode: "open" });
|
|
12
137
|
}
|
|
13
138
|
static get observedAttributes() {
|
|
14
|
-
return ["api-key", "conversation-id", "api-base-url", "embed-token"];
|
|
139
|
+
return ["api-key", "conversation-id", "api-base-url", "embed-token", "ws-url", "theme-color"];
|
|
15
140
|
}
|
|
16
141
|
connectedCallback() {
|
|
17
142
|
this.initialize();
|
|
@@ -28,78 +153,444 @@ var ScaleMuleChatElement = class extends HTMLElement {
|
|
|
28
153
|
const conversationId = this.getAttribute("conversation-id");
|
|
29
154
|
const apiBaseUrl = this.getAttribute("api-base-url") ?? void 0;
|
|
30
155
|
const embedToken = this.getAttribute("embed-token") ?? void 0;
|
|
31
|
-
|
|
156
|
+
const wsUrl = this.getAttribute("ws-url") ?? void 0;
|
|
157
|
+
const themeColor = sanitizeThemeColor(this.getAttribute("theme-color") ?? "#2563eb");
|
|
158
|
+
if (!apiKey && !embedToken || !conversationId) return;
|
|
32
159
|
const config = {
|
|
33
160
|
apiKey,
|
|
34
161
|
embedToken,
|
|
35
|
-
apiBaseUrl
|
|
162
|
+
apiBaseUrl,
|
|
163
|
+
wsUrl
|
|
36
164
|
};
|
|
37
|
-
this.client = new
|
|
165
|
+
this.client = new chunkW2PWFS3E_cjs.ChatClient(config);
|
|
166
|
+
this.controller = new chunkTRCELAZQ_cjs.ChatController(this.client, conversationId);
|
|
38
167
|
this.shadow.innerHTML = `
|
|
39
168
|
<style>
|
|
40
|
-
:host { display: block; width: 100%; height: 100%; }
|
|
41
|
-
.chat-container {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
169
|
+
:host { display: block; width: 100%; height: 100%; color: #111827; }
|
|
170
|
+
.chat-container {
|
|
171
|
+
--sm-primary: ${themeColor};
|
|
172
|
+
--sm-primary-muted: color-mix(in srgb, var(--sm-primary) 10%, white);
|
|
173
|
+
width: 100%;
|
|
174
|
+
height: 100%;
|
|
175
|
+
display: flex;
|
|
176
|
+
flex-direction: column;
|
|
177
|
+
font-family: system-ui, sans-serif;
|
|
178
|
+
background: #fff;
|
|
179
|
+
border: 1px solid #e5e7eb;
|
|
180
|
+
border-radius: 16px;
|
|
181
|
+
overflow: hidden;
|
|
182
|
+
}
|
|
183
|
+
.header {
|
|
184
|
+
display: flex;
|
|
185
|
+
align-items: center;
|
|
186
|
+
justify-content: space-between;
|
|
187
|
+
padding: 14px 16px;
|
|
188
|
+
border-bottom: 1px solid #e5e7eb;
|
|
189
|
+
background: #fff;
|
|
190
|
+
}
|
|
191
|
+
.header-title {
|
|
192
|
+
font-size: 15px;
|
|
193
|
+
font-weight: 700;
|
|
194
|
+
}
|
|
195
|
+
.header-status-copy {
|
|
196
|
+
font-size: 12px;
|
|
197
|
+
color: #6b7280;
|
|
198
|
+
margin-top: 2px;
|
|
199
|
+
}
|
|
200
|
+
.header-presence {
|
|
201
|
+
display: inline-flex;
|
|
202
|
+
align-items: center;
|
|
203
|
+
gap: 8px;
|
|
204
|
+
font-size: 12px;
|
|
205
|
+
color: #6b7280;
|
|
206
|
+
white-space: nowrap;
|
|
207
|
+
}
|
|
208
|
+
.header-presence-dot {
|
|
209
|
+
width: 10px;
|
|
210
|
+
height: 10px;
|
|
211
|
+
border-radius: 999px;
|
|
212
|
+
background: #94a3b8;
|
|
213
|
+
}
|
|
214
|
+
.header-presence-dot.online {
|
|
215
|
+
background: #22c55e;
|
|
216
|
+
}
|
|
217
|
+
.messages {
|
|
218
|
+
flex: 1;
|
|
219
|
+
overflow-y: auto;
|
|
220
|
+
padding: 16px;
|
|
221
|
+
background: #f8fafc;
|
|
222
|
+
display: flex;
|
|
223
|
+
flex-direction: column;
|
|
224
|
+
gap: 12px;
|
|
225
|
+
}
|
|
226
|
+
.message {
|
|
227
|
+
display: flex;
|
|
228
|
+
flex-direction: column;
|
|
229
|
+
gap: 6px;
|
|
230
|
+
max-width: 82%;
|
|
231
|
+
}
|
|
232
|
+
.message-own { align-self: flex-end; }
|
|
233
|
+
.message-other { align-self: flex-start; }
|
|
234
|
+
.message-bubble {
|
|
235
|
+
padding: 10px 12px;
|
|
236
|
+
border-radius: 16px;
|
|
237
|
+
background: #f3f4f6;
|
|
238
|
+
color: #111827;
|
|
239
|
+
white-space: pre-wrap;
|
|
240
|
+
word-break: break-word;
|
|
241
|
+
}
|
|
242
|
+
.message-own .message-bubble {
|
|
243
|
+
background: var(--sm-primary);
|
|
244
|
+
color: #fff;
|
|
245
|
+
}
|
|
246
|
+
.message-meta {
|
|
247
|
+
display: flex;
|
|
248
|
+
gap: 8px;
|
|
249
|
+
align-items: center;
|
|
250
|
+
font-size: 12px;
|
|
251
|
+
color: #6b7280;
|
|
252
|
+
}
|
|
253
|
+
.message-edited { text-transform: lowercase; }
|
|
254
|
+
.message-action {
|
|
255
|
+
border: none;
|
|
256
|
+
background: transparent;
|
|
257
|
+
color: inherit;
|
|
258
|
+
cursor: pointer;
|
|
259
|
+
font-size: 12px;
|
|
260
|
+
padding: 0;
|
|
261
|
+
}
|
|
262
|
+
.date-divider,
|
|
263
|
+
.unread-divider {
|
|
264
|
+
align-self: center;
|
|
265
|
+
font-size: 12px;
|
|
266
|
+
color: #6b7280;
|
|
267
|
+
}
|
|
268
|
+
.date-divider {
|
|
269
|
+
padding: 4px 10px;
|
|
270
|
+
border-radius: 999px;
|
|
271
|
+
background: rgba(148, 163, 184, 0.12);
|
|
272
|
+
}
|
|
273
|
+
.unread-divider {
|
|
274
|
+
width: 100%;
|
|
275
|
+
display: flex;
|
|
276
|
+
align-items: center;
|
|
277
|
+
gap: 10px;
|
|
278
|
+
color: var(--sm-primary);
|
|
279
|
+
font-weight: 600;
|
|
280
|
+
}
|
|
281
|
+
.unread-divider::before,
|
|
282
|
+
.unread-divider::after {
|
|
283
|
+
content: '';
|
|
284
|
+
flex: 1;
|
|
285
|
+
height: 1px;
|
|
286
|
+
background: color-mix(in srgb, var(--sm-primary) 28%, white);
|
|
287
|
+
}
|
|
288
|
+
.attachment {
|
|
289
|
+
display: block;
|
|
290
|
+
width: 100%;
|
|
291
|
+
margin-top: 8px;
|
|
292
|
+
border-radius: 12px;
|
|
293
|
+
}
|
|
294
|
+
.attachment-image,
|
|
295
|
+
.attachment-video { max-width: 320px; }
|
|
296
|
+
.attachment-link { color: inherit; }
|
|
297
|
+
.reactions {
|
|
298
|
+
display: flex;
|
|
299
|
+
gap: 6px;
|
|
300
|
+
flex-wrap: wrap;
|
|
301
|
+
}
|
|
302
|
+
.reaction-badge,
|
|
303
|
+
.reaction-picker-btn {
|
|
304
|
+
border: 1px solid #dbe3ef;
|
|
305
|
+
border-radius: 999px;
|
|
306
|
+
background: #fff;
|
|
307
|
+
padding: 4px 8px;
|
|
308
|
+
cursor: pointer;
|
|
309
|
+
font-size: 12px;
|
|
310
|
+
}
|
|
311
|
+
.reaction-badge-active {
|
|
312
|
+
border-color: color-mix(in srgb, var(--sm-primary) 40%, white);
|
|
313
|
+
background: color-mix(in srgb, var(--sm-primary) 10%, white);
|
|
314
|
+
}
|
|
315
|
+
.reaction-picker {
|
|
316
|
+
display: flex;
|
|
317
|
+
flex-wrap: wrap;
|
|
318
|
+
gap: 6px;
|
|
319
|
+
}
|
|
320
|
+
.typing {
|
|
321
|
+
min-height: 20px;
|
|
322
|
+
padding: 0 16px 10px;
|
|
323
|
+
font-size: 12px;
|
|
324
|
+
color: #6b7280;
|
|
325
|
+
}
|
|
326
|
+
.attachments {
|
|
327
|
+
display: flex;
|
|
328
|
+
flex-wrap: wrap;
|
|
329
|
+
gap: 8px;
|
|
330
|
+
padding: 0 12px 12px;
|
|
331
|
+
}
|
|
332
|
+
.attachment-chip {
|
|
333
|
+
display: inline-flex;
|
|
334
|
+
align-items: center;
|
|
335
|
+
gap: 8px;
|
|
336
|
+
padding: 6px 10px;
|
|
337
|
+
border-radius: 999px;
|
|
338
|
+
border: 1px solid #dbe3ef;
|
|
339
|
+
background: #f8fafc;
|
|
340
|
+
font-size: 12px;
|
|
341
|
+
}
|
|
342
|
+
.attachment-chip-error {
|
|
343
|
+
border-color: #fecaca;
|
|
344
|
+
background: #fef2f2;
|
|
345
|
+
color: #b91c1c;
|
|
346
|
+
}
|
|
347
|
+
.attachment-remove {
|
|
348
|
+
border: none;
|
|
349
|
+
background: transparent;
|
|
350
|
+
color: inherit;
|
|
351
|
+
cursor: pointer;
|
|
352
|
+
font: inherit;
|
|
353
|
+
}
|
|
354
|
+
.input-area {
|
|
355
|
+
display: flex;
|
|
356
|
+
gap: 10px;
|
|
357
|
+
padding: 12px;
|
|
358
|
+
border-top: 1px solid #e5e7eb;
|
|
359
|
+
background: #fff;
|
|
360
|
+
}
|
|
361
|
+
.input-area textarea {
|
|
362
|
+
flex: 1;
|
|
363
|
+
min-height: 44px;
|
|
364
|
+
resize: vertical;
|
|
365
|
+
border: 1px solid #d1d5db;
|
|
366
|
+
border-radius: 12px;
|
|
367
|
+
padding: 10px 12px;
|
|
368
|
+
font: inherit;
|
|
369
|
+
outline: none;
|
|
370
|
+
}
|
|
371
|
+
.input-area button {
|
|
372
|
+
border: none;
|
|
373
|
+
border-radius: 12px;
|
|
374
|
+
padding: 0 16px;
|
|
375
|
+
background: var(--sm-primary);
|
|
376
|
+
color: white;
|
|
377
|
+
cursor: pointer;
|
|
378
|
+
}
|
|
379
|
+
.input-area .attach-btn {
|
|
380
|
+
background: var(--sm-primary-muted);
|
|
381
|
+
color: #0f172a;
|
|
382
|
+
}
|
|
383
|
+
.empty {
|
|
384
|
+
color: #6b7280;
|
|
385
|
+
font-size: 14px;
|
|
386
|
+
padding: 32px 0;
|
|
387
|
+
text-align: center;
|
|
388
|
+
}
|
|
47
389
|
</style>
|
|
48
390
|
<div class="chat-container">
|
|
49
|
-
<div class="
|
|
391
|
+
<div class="header">
|
|
392
|
+
<div>
|
|
393
|
+
<div class="header-title">Chat</div>
|
|
394
|
+
<div class="header-status-copy" id="status-copy">Loading conversation\u2026</div>
|
|
395
|
+
</div>
|
|
396
|
+
<div class="header-presence">
|
|
397
|
+
<span class="header-presence-dot" id="presence-dot"></span>
|
|
398
|
+
<span id="presence-label">Away</span>
|
|
399
|
+
</div>
|
|
400
|
+
</div>
|
|
401
|
+
<div class="messages" id="messages" role="log" aria-live="polite"></div>
|
|
402
|
+
<div class="typing" id="typing" aria-live="polite"></div>
|
|
403
|
+
<div class="attachments" id="attachments" aria-live="polite"></div>
|
|
50
404
|
<div class="input-area">
|
|
51
|
-
<input type="
|
|
52
|
-
<button id="
|
|
405
|
+
<input type="file" id="file-input" hidden multiple accept="image/*,video/*,audio/*" aria-label="Attach files" />
|
|
406
|
+
<button class="attach-btn" id="attach" type="button" aria-label="Attach files">Attach</button>
|
|
407
|
+
<textarea placeholder="Type a message..." id="input" aria-label="Message"></textarea>
|
|
408
|
+
<button id="send" aria-label="Send message">Send</button>
|
|
53
409
|
</div>
|
|
54
410
|
</div>
|
|
55
411
|
`;
|
|
56
412
|
const messagesEl = this.shadow.getElementById("messages");
|
|
413
|
+
const typingEl = this.shadow.getElementById("typing");
|
|
414
|
+
const attachmentsEl = this.shadow.getElementById("attachments");
|
|
415
|
+
const statusCopyEl = this.shadow.getElementById("status-copy");
|
|
416
|
+
const presenceDotEl = this.shadow.getElementById("presence-dot");
|
|
417
|
+
const presenceLabelEl = this.shadow.getElementById("presence-label");
|
|
57
418
|
const inputEl = this.shadow.getElementById("input");
|
|
419
|
+
const fileInputEl = this.shadow.getElementById("file-input");
|
|
420
|
+
const attachBtn = this.shadow.getElementById("attach");
|
|
58
421
|
const sendBtn = this.shadow.getElementById("send");
|
|
59
|
-
|
|
60
|
-
this.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
422
|
+
const renderPendingAttachments = () => {
|
|
423
|
+
attachmentsEl.innerHTML = this.pendingAttachments.length ? this.pendingAttachments.map(
|
|
424
|
+
(attachment) => `
|
|
425
|
+
<div class="attachment-chip ${attachment.error ? "attachment-chip-error" : ""}">
|
|
426
|
+
<span>${escapeHtml(attachment.fileName)}</span>
|
|
427
|
+
<span>${escapeHtml(attachment.error ?? `${attachment.progress}%`)}</span>
|
|
428
|
+
<button class="attachment-remove" type="button" data-pending-id="${escapeHtml(attachment.id)}" aria-label="Remove attachment">x</button>
|
|
429
|
+
</div>
|
|
430
|
+
`
|
|
431
|
+
).join("") : "";
|
|
432
|
+
attachmentsEl.querySelectorAll("[data-pending-id]").forEach((button) => {
|
|
433
|
+
button.addEventListener("click", () => {
|
|
434
|
+
const id = button.dataset.pendingId;
|
|
435
|
+
if (!id) return;
|
|
436
|
+
this.pendingAttachments = this.pendingAttachments.filter((item) => item.id !== id);
|
|
437
|
+
renderPendingAttachments();
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
};
|
|
441
|
+
const renderState = (state) => {
|
|
442
|
+
this.currentState = state;
|
|
443
|
+
const currentUserId = this.client?.userId;
|
|
444
|
+
const activeMembers = state.members.filter((member) => member.userId !== currentUserId);
|
|
445
|
+
const online = activeMembers.some((member) => member.status === "online");
|
|
446
|
+
const onlineCount = activeMembers.filter((member) => member.status === "online").length;
|
|
447
|
+
const unreadSince = currentUserId ? state.readStatuses.find((status) => status.user_id === currentUserId)?.last_read_at : void 0;
|
|
448
|
+
const unreadIndex = getUnreadIndex(state.messages, unreadSince);
|
|
449
|
+
presenceDotEl.className = `header-presence-dot ${online ? "online" : ""}`;
|
|
450
|
+
presenceLabelEl.textContent = online ? "Online" : "Away";
|
|
451
|
+
statusCopyEl.textContent = state.error ? state.error : state.typingUsers.some((userId) => userId !== currentUserId) ? "Someone is typing\u2026" : onlineCount ? `${onlineCount} online` : "No one online";
|
|
452
|
+
if (!state.messages.length) {
|
|
453
|
+
messagesEl.innerHTML = `<div class="empty">${escapeHtml(state.error ?? (state.isLoading ? "Loading messages..." : "Start the conversation"))}</div>`;
|
|
454
|
+
} else {
|
|
455
|
+
messagesEl.innerHTML = state.messages.map((message, index) => {
|
|
456
|
+
const previousMessage = state.messages[index - 1];
|
|
457
|
+
const showDateDivider = !previousMessage || !isSameDay(previousMessage.created_at, message.created_at);
|
|
458
|
+
const showUnreadDivider = unreadIndex === index;
|
|
459
|
+
return `
|
|
460
|
+
${showDateDivider ? `<div class="date-divider">${escapeHtml(formatDayLabel(message.created_at))}</div>` : ""}
|
|
461
|
+
${showUnreadDivider ? `<div class="unread-divider" id="unread-divider">New messages</div>` : ""}
|
|
462
|
+
${renderMessage(message, currentUserId, this.openReactionMessageId === message.id)}
|
|
463
|
+
`;
|
|
464
|
+
}).join("");
|
|
465
|
+
}
|
|
466
|
+
typingEl.textContent = state.typingUsers.some((userId) => userId !== currentUserId) ? "Someone is typing..." : "";
|
|
467
|
+
const unreadDivider = messagesEl.querySelector("#unread-divider");
|
|
468
|
+
if (unreadDivider && !this.didScrollToUnread) {
|
|
469
|
+
unreadDivider.scrollIntoView({ block: "center" });
|
|
470
|
+
this.didScrollToUnread = true;
|
|
471
|
+
} else {
|
|
472
|
+
messagesEl.scrollTop = messagesEl.scrollHeight;
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
this.cleanupFns.push(
|
|
476
|
+
this.controller.on("state", (state) => {
|
|
477
|
+
renderState(state);
|
|
478
|
+
})
|
|
479
|
+
);
|
|
480
|
+
this.cleanupFns.push(
|
|
481
|
+
this.client.on("message", () => {
|
|
482
|
+
this.dispatchEvent(
|
|
483
|
+
new CustomEvent("chat-message", { composed: true, bubbles: true })
|
|
484
|
+
);
|
|
485
|
+
})
|
|
486
|
+
);
|
|
487
|
+
messagesEl.addEventListener("click", (event) => {
|
|
488
|
+
const target = event.target.closest("[data-action]");
|
|
489
|
+
if (!target || !this.controller || !this.currentState) return;
|
|
490
|
+
const action = target.dataset.action;
|
|
491
|
+
const messageId = target.dataset.messageId;
|
|
492
|
+
if (!messageId) return;
|
|
493
|
+
if (action === "toggle-picker") {
|
|
494
|
+
this.openReactionMessageId = this.openReactionMessageId === messageId ? null : messageId;
|
|
495
|
+
renderState(this.currentState);
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const emoji = target.dataset.emoji;
|
|
499
|
+
if (!emoji) return;
|
|
500
|
+
if (action === "add-reaction") {
|
|
501
|
+
this.openReactionMessageId = null;
|
|
502
|
+
void this.controller.addReaction(messageId, emoji);
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if (action === "toggle-reaction") {
|
|
506
|
+
const reacted = target.dataset.reacted === "true";
|
|
507
|
+
if (reacted) {
|
|
508
|
+
void this.controller.removeReaction(messageId, emoji);
|
|
509
|
+
} else {
|
|
510
|
+
void this.controller.addReaction(messageId, emoji);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
64
513
|
});
|
|
65
|
-
const
|
|
514
|
+
const handleFiles = async (files) => {
|
|
515
|
+
if (!this.controller) return;
|
|
516
|
+
for (const file of Array.from(files)) {
|
|
517
|
+
const pendingId = `${file.name}:${file.size}:${Date.now()}:${Math.random().toString(36).slice(2)}`;
|
|
518
|
+
this.pendingAttachments = [
|
|
519
|
+
...this.pendingAttachments,
|
|
520
|
+
{
|
|
521
|
+
id: pendingId,
|
|
522
|
+
fileName: file.name,
|
|
523
|
+
progress: 0
|
|
524
|
+
}
|
|
525
|
+
];
|
|
526
|
+
renderPendingAttachments();
|
|
527
|
+
const result = await this.controller.uploadAttachment(file, (progress) => {
|
|
528
|
+
this.pendingAttachments = this.pendingAttachments.map(
|
|
529
|
+
(attachment) => attachment.id === pendingId ? { ...attachment, progress } : attachment
|
|
530
|
+
);
|
|
531
|
+
renderPendingAttachments();
|
|
532
|
+
});
|
|
533
|
+
this.pendingAttachments = this.pendingAttachments.map((attachment) => {
|
|
534
|
+
if (attachment.id !== pendingId) return attachment;
|
|
535
|
+
if (result?.data) {
|
|
536
|
+
return {
|
|
537
|
+
...attachment,
|
|
538
|
+
progress: 100,
|
|
539
|
+
attachment: result.data
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
return {
|
|
543
|
+
...attachment,
|
|
544
|
+
error: result?.error?.message ?? "Upload failed"
|
|
545
|
+
};
|
|
546
|
+
});
|
|
547
|
+
renderPendingAttachments();
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
const send = async () => {
|
|
66
551
|
const content = inputEl.value.trim();
|
|
67
|
-
|
|
68
|
-
|
|
552
|
+
const readyAttachments = this.pendingAttachments.filter((attachment) => attachment.attachment).map((attachment) => attachment.attachment);
|
|
553
|
+
if (!content && !readyAttachments.length || !this.controller) return;
|
|
69
554
|
inputEl.value = "";
|
|
555
|
+
await this.controller.sendMessage(content, readyAttachments);
|
|
556
|
+
this.pendingAttachments = [];
|
|
557
|
+
renderPendingAttachments();
|
|
558
|
+
await this.controller.markRead();
|
|
70
559
|
};
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (e.key === "Enter") doSend();
|
|
560
|
+
attachBtn.addEventListener("click", () => {
|
|
561
|
+
fileInputEl.click();
|
|
74
562
|
});
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
el.className = "message";
|
|
94
|
-
el.textContent = message.content;
|
|
95
|
-
container.appendChild(el);
|
|
96
|
-
container.scrollTop = container.scrollHeight;
|
|
563
|
+
fileInputEl.addEventListener("change", () => {
|
|
564
|
+
if (fileInputEl.files) {
|
|
565
|
+
void handleFiles(fileInputEl.files);
|
|
566
|
+
fileInputEl.value = "";
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
sendBtn.addEventListener("click", () => {
|
|
570
|
+
void send();
|
|
571
|
+
});
|
|
572
|
+
inputEl.addEventListener("keydown", (event) => {
|
|
573
|
+
if (event.key === "Enter" && !event.shiftKey) {
|
|
574
|
+
event.preventDefault();
|
|
575
|
+
void send();
|
|
576
|
+
} else {
|
|
577
|
+
this.controller?.sendTyping(true);
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
void this.controller.init().then(() => this.controller?.markRead());
|
|
97
581
|
}
|
|
98
582
|
cleanup() {
|
|
99
|
-
this.
|
|
100
|
-
|
|
583
|
+
for (const cleanup of this.cleanupFns) {
|
|
584
|
+
cleanup();
|
|
585
|
+
}
|
|
586
|
+
this.cleanupFns = [];
|
|
587
|
+
this.controller?.destroy();
|
|
588
|
+
this.controller = null;
|
|
101
589
|
this.client?.destroy();
|
|
102
590
|
this.client = null;
|
|
591
|
+
this.currentState = null;
|
|
592
|
+
this.openReactionMessageId = null;
|
|
593
|
+
this.didScrollToUnread = false;
|
|
103
594
|
this.shadow.innerHTML = "";
|
|
104
595
|
}
|
|
105
596
|
};
|
package/dist/element.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export {};
|
|
2
|
+
//# sourceMappingURL=element.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"element.d.ts","sourceRoot":"","sources":["../src/element.ts"],"names":[],"mappings":""}
|