@channelhub/widget 0.1.9 → 0.1.11
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/ChannelHubChannels.d.ts.map +1 -1
- package/dist/LiveChatWidget.d.ts.map +1 -1
- package/dist/index.cjs +6 -6
- package/dist/index.js +414 -341
- package/package.json +1 -1
- package/src/ChannelHubChannels.tsx +106 -24
- package/src/LiveChatWidget.tsx +113 -4
- package/src/api.ts +1 -1
package/package.json
CHANGED
|
@@ -53,9 +53,28 @@ function displayName(conn: ChannelConnection): string {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
function metaLine(conn: ChannelConnection): string {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
const m = conn.metadata;
|
|
57
|
+
switch (conn.provider) {
|
|
58
|
+
case 'facebook':
|
|
59
|
+
return `Fanpage · ID ${conn.externalAccountId}`;
|
|
60
|
+
case 'instagram': {
|
|
61
|
+
const ig = String(m['username'] ?? conn.externalAccountId);
|
|
62
|
+
return `@${ig}`;
|
|
63
|
+
}
|
|
64
|
+
case 'zalo':
|
|
65
|
+
return `Zalo OA · ID ${conn.externalAccountId}`;
|
|
66
|
+
case 'telegram': {
|
|
67
|
+
const tg = String(m['username'] ?? '');
|
|
68
|
+
return tg ? `@${tg}` : `Bot · ${conn.externalAccountId}`;
|
|
69
|
+
}
|
|
70
|
+
case 'livechat':
|
|
71
|
+
return `Widget ID: ${conn.id}`;
|
|
72
|
+
default: {
|
|
73
|
+
const label = PROVIDER_LABEL[conn.provider] ?? conn.provider;
|
|
74
|
+
const name = String(m['username'] ?? m['pageName'] ?? '');
|
|
75
|
+
return name ? `${label} · ${name}` : label;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
59
78
|
}
|
|
60
79
|
|
|
61
80
|
// ── Shared style tokens ────────────────────────────────────────────────────────
|
|
@@ -871,29 +890,92 @@ function ChannelAvatar({ conn, apiUrl, size = 40 }: { conn: ChannelConnection; a
|
|
|
871
890
|
|
|
872
891
|
function ChannelItem({ conn, apiUrl, onDisconnect }: { conn: ChannelConnection; apiUrl: string; onDisconnect: () => void }) {
|
|
873
892
|
const [hover, setHover] = useState(false);
|
|
893
|
+
const [showScript, setShowScript] = useState(false);
|
|
894
|
+
const [copied, setCopied] = useState(false);
|
|
895
|
+
|
|
896
|
+
const isLivechat = conn.provider === 'livechat';
|
|
897
|
+
const base = apiUrl.replace(/\/$/, '');
|
|
898
|
+
const embedScript = isLivechat
|
|
899
|
+
? `<script src="${base}/static/livechat.js" data-widget-id="${conn.id}" defer></script>`
|
|
900
|
+
: null;
|
|
901
|
+
|
|
902
|
+
const copyScript = () => {
|
|
903
|
+
if (!embedScript) return;
|
|
904
|
+
void navigator.clipboard.writeText(embedScript).then(() => {
|
|
905
|
+
setCopied(true);
|
|
906
|
+
setTimeout(() => setCopied(false), 2000);
|
|
907
|
+
});
|
|
908
|
+
};
|
|
909
|
+
|
|
874
910
|
return (
|
|
875
|
-
<div style={s.channelItem}>
|
|
876
|
-
<
|
|
877
|
-
|
|
878
|
-
<div style={s.
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
<
|
|
883
|
-
{conn.status === 'active' ?
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
911
|
+
<div style={{ ...s.channelItem, flexDirection: 'column', alignItems: 'stretch', gap: 0 }}>
|
|
912
|
+
<div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
|
|
913
|
+
<ChannelAvatar conn={conn} apiUrl={apiUrl} size={40} />
|
|
914
|
+
<div style={s.channelInfo}>
|
|
915
|
+
<div style={s.channelName}>{displayName(conn)}</div>
|
|
916
|
+
<div style={{ ...s.channelMeta, userSelect: 'text' as const }}>{metaLine(conn)}</div>
|
|
917
|
+
</div>
|
|
918
|
+
<div style={s.channelActions}>
|
|
919
|
+
<span style={conn.status === 'active' ? s.badgeActive : s.badgeInactive}>
|
|
920
|
+
{conn.status === 'active' ? 'Hoạt động' : conn.status}
|
|
921
|
+
</span>
|
|
922
|
+
{isLivechat && (
|
|
923
|
+
<button
|
|
924
|
+
style={{
|
|
925
|
+
...s.disconnectBtn,
|
|
926
|
+
...(showScript ? { background: '#ede9fe', borderColor: '#c4b5fd', color: '#7c3aed' } : {}),
|
|
927
|
+
}}
|
|
928
|
+
onClick={() => setShowScript(v => !v)}
|
|
929
|
+
>
|
|
930
|
+
Mã nhúng
|
|
931
|
+
</button>
|
|
932
|
+
)}
|
|
933
|
+
<button
|
|
934
|
+
style={{
|
|
935
|
+
...s.disconnectBtn,
|
|
936
|
+
...(hover ? { background: '#fef2f2', borderColor: '#fca5a5', color: '#dc2626' } : {}),
|
|
937
|
+
}}
|
|
938
|
+
onMouseEnter={() => setHover(true)}
|
|
939
|
+
onMouseLeave={() => setHover(false)}
|
|
940
|
+
onClick={onDisconnect}
|
|
941
|
+
>
|
|
942
|
+
Ngắt kết nối
|
|
943
|
+
</button>
|
|
944
|
+
</div>
|
|
896
945
|
</div>
|
|
946
|
+
|
|
947
|
+
{isLivechat && showScript && embedScript && (
|
|
948
|
+
<div style={{ marginTop: 10, borderTop: '1px solid #f1f5f9', paddingTop: 10 }}>
|
|
949
|
+
<div style={{ fontSize: 11, fontWeight: 600, color: '#7c3aed', marginBottom: 6, textTransform: 'uppercase' as const, letterSpacing: '0.05em' }}>
|
|
950
|
+
Mã nhúng — dán vào trước thẻ </body>
|
|
951
|
+
</div>
|
|
952
|
+
<div style={{ position: 'relative' as const }}>
|
|
953
|
+
<pre style={{
|
|
954
|
+
margin: 0, padding: '10px 12px', background: '#f8f7ff',
|
|
955
|
+
border: '1px solid #e9d5ff', borderRadius: 8,
|
|
956
|
+
fontSize: 12, color: '#3b0764', overflowX: 'auto' as const,
|
|
957
|
+
fontFamily: 'ui-monospace, SFMono-Regular, monospace',
|
|
958
|
+
lineHeight: 1.5, userSelect: 'text' as const,
|
|
959
|
+
}}>
|
|
960
|
+
{embedScript}
|
|
961
|
+
</pre>
|
|
962
|
+
<button
|
|
963
|
+
onClick={copyScript}
|
|
964
|
+
style={{
|
|
965
|
+
position: 'absolute' as const, top: 6, right: 6,
|
|
966
|
+
fontSize: 11, fontWeight: 600, padding: '3px 10px', borderRadius: 6,
|
|
967
|
+
border: '1px solid',
|
|
968
|
+
borderColor: copied ? '#059669' : '#c4b5fd',
|
|
969
|
+
background: copied ? '#d1fae5' : '#fff',
|
|
970
|
+
color: copied ? '#059669' : '#7c3aed',
|
|
971
|
+
cursor: 'pointer',
|
|
972
|
+
}}
|
|
973
|
+
>
|
|
974
|
+
{copied ? '✓ Đã sao chép' : 'Sao chép'}
|
|
975
|
+
</button>
|
|
976
|
+
</div>
|
|
977
|
+
</div>
|
|
978
|
+
)}
|
|
897
979
|
</div>
|
|
898
980
|
);
|
|
899
981
|
}
|
package/src/LiveChatWidget.tsx
CHANGED
|
@@ -57,6 +57,9 @@ export function LiveChatWidget({ apiUrl, widgetId, primaryColor = '#2563eb', gre
|
|
|
57
57
|
const [conversationId, setConversationId] = useState<string | null>(null);
|
|
58
58
|
const [unread, setUnread] = useState(0);
|
|
59
59
|
const [lightbox, setLightbox] = useState<{ url: string; kind: 'image' | 'video' } | null>(null);
|
|
60
|
+
const [pendingFiles, setPendingFiles] = useState<Array<{ file: File; previewUrl: string | null }>>([]);
|
|
61
|
+
const mediaInputRef = useRef<HTMLInputElement>(null);
|
|
62
|
+
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
60
63
|
const afterRef = useRef<string | undefined>(undefined);
|
|
61
64
|
const pollRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
62
65
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
|
@@ -136,19 +139,63 @@ export function LiveChatWidget({ apiUrl, widgetId, primaryColor = '#2563eb', gre
|
|
|
136
139
|
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
|
137
140
|
}, [messages]);
|
|
138
141
|
|
|
142
|
+
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
143
|
+
const files = Array.from(e.target.files ?? []);
|
|
144
|
+
const items = files.map(f => ({
|
|
145
|
+
file: f,
|
|
146
|
+
previewUrl: f.type.startsWith('image/') ? URL.createObjectURL(f) : null,
|
|
147
|
+
}));
|
|
148
|
+
setPendingFiles(prev => [...prev, ...items].slice(0, 5));
|
|
149
|
+
if (fileInputRef.current) fileInputRef.current.value = '';
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const removePendingFile = (idx: number) => {
|
|
153
|
+
setPendingFiles(prev => {
|
|
154
|
+
const item = prev[idx];
|
|
155
|
+
if (item?.previewUrl) URL.revokeObjectURL(item.previewUrl);
|
|
156
|
+
return prev.filter((_, i) => i !== idx);
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
|
|
139
160
|
const handleSend = async () => {
|
|
140
161
|
const text = inputText.trim();
|
|
141
|
-
if (!text || sending || !visitorToken) return;
|
|
162
|
+
if ((!text && pendingFiles.length === 0) || sending || !visitorToken) return;
|
|
142
163
|
setSending(true);
|
|
143
164
|
setInputText('');
|
|
165
|
+
const filesToSend = [...pendingFiles];
|
|
166
|
+
setPendingFiles([]);
|
|
167
|
+
|
|
168
|
+
// Upload files first
|
|
169
|
+
const attachments: Array<{ url: string; mimeType: string; type: string }> = [];
|
|
170
|
+
for (const { file } of filesToSend) {
|
|
171
|
+
try {
|
|
172
|
+
const form = new FormData();
|
|
173
|
+
form.append('widgetId', widgetId);
|
|
174
|
+
form.append('visitorToken', visitorToken);
|
|
175
|
+
form.append('file', file);
|
|
176
|
+
const up = await fetch(`${base}/v1/livechat/visitor/upload`, { method: 'POST', body: form });
|
|
177
|
+
if (up.ok) {
|
|
178
|
+
const d = await up.json() as { url: string; mimeType: string; type: string };
|
|
179
|
+
attachments.push(d);
|
|
180
|
+
}
|
|
181
|
+
} catch { /* skip failed upload */ }
|
|
182
|
+
}
|
|
183
|
+
|
|
144
184
|
const tempId = crypto.randomUUID();
|
|
145
|
-
const optimistic: ChatMessage = {
|
|
185
|
+
const optimistic: ChatMessage = {
|
|
186
|
+
id: tempId,
|
|
187
|
+
direction: 'inbound',
|
|
188
|
+
text: text || null,
|
|
189
|
+
attachments: attachments.map(a => ({ type: a.type as MessageAttachment['type'], url: a.url, mimeType: a.mimeType })),
|
|
190
|
+
occurredAt: new Date().toISOString(),
|
|
191
|
+
};
|
|
146
192
|
setMessages(prev => [...prev, optimistic]);
|
|
193
|
+
|
|
147
194
|
try {
|
|
148
195
|
const res = await fetch(`${base}/v1/livechat/visitor/message`, {
|
|
149
196
|
method: 'POST',
|
|
150
197
|
headers: { 'content-type': 'application/json' },
|
|
151
|
-
body: JSON.stringify({ widgetId, visitorToken, text }),
|
|
198
|
+
body: JSON.stringify({ widgetId, visitorToken, ...(text ? { text } : {}), attachments }),
|
|
152
199
|
});
|
|
153
200
|
if (res.ok) {
|
|
154
201
|
const data = await res.json() as { messageId: string; conversationId: string };
|
|
@@ -230,7 +277,56 @@ export function LiveChatWidget({ apiUrl, widgetId, primaryColor = '#2563eb', gre
|
|
|
230
277
|
})}
|
|
231
278
|
<div ref={messagesEndRef} />
|
|
232
279
|
</div>
|
|
280
|
+
{pendingFiles.length > 0 && (
|
|
281
|
+
<div style={{ display: 'flex', gap: 6, padding: '6px 12px 0', flexWrap: 'wrap' as const }}>
|
|
282
|
+
{pendingFiles.map((item, idx) => (
|
|
283
|
+
<div key={idx} style={{ position: 'relative', width: 52, height: 52, borderRadius: 8, overflow: 'hidden', border: '1px solid #e5e7eb', flexShrink: 0 }}>
|
|
284
|
+
{item.previewUrl
|
|
285
|
+
? <img src={item.previewUrl} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
|
|
286
|
+
: <div style={{ width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center', background: '#f3f4f6', fontSize: 20 }}>📎</div>
|
|
287
|
+
}
|
|
288
|
+
<button
|
|
289
|
+
onClick={() => removePendingFile(idx)}
|
|
290
|
+
style={{ position: 'absolute', top: 2, right: 2, width: 16, height: 16, borderRadius: '50%', background: 'rgba(0,0,0,.55)', border: 'none', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#fff', fontSize: 9, lineHeight: 1 }}
|
|
291
|
+
aria-label="Xóa"
|
|
292
|
+
>✕</button>
|
|
293
|
+
</div>
|
|
294
|
+
))}
|
|
295
|
+
</div>
|
|
296
|
+
)}
|
|
233
297
|
<div style={s.inputRow}>
|
|
298
|
+
<input
|
|
299
|
+
ref={mediaInputRef}
|
|
300
|
+
type="file"
|
|
301
|
+
accept="image/*,video/mp4"
|
|
302
|
+
multiple
|
|
303
|
+
style={{ display: 'none' }}
|
|
304
|
+
onChange={handleFileChange}
|
|
305
|
+
/>
|
|
306
|
+
<input
|
|
307
|
+
ref={fileInputRef}
|
|
308
|
+
type="file"
|
|
309
|
+
accept="audio/mpeg,audio/ogg,application/pdf"
|
|
310
|
+
multiple
|
|
311
|
+
style={{ display: 'none' }}
|
|
312
|
+
onChange={handleFileChange}
|
|
313
|
+
/>
|
|
314
|
+
<button
|
|
315
|
+
style={s.attachBtn}
|
|
316
|
+
onClick={() => mediaInputRef.current?.click()}
|
|
317
|
+
disabled={sending || pendingFiles.length >= 5}
|
|
318
|
+
aria-label="Gửi ảnh/video"
|
|
319
|
+
>
|
|
320
|
+
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/><circle cx="12" cy="13" r="4"/></svg>
|
|
321
|
+
</button>
|
|
322
|
+
<button
|
|
323
|
+
style={s.attachBtn}
|
|
324
|
+
onClick={() => fileInputRef.current?.click()}
|
|
325
|
+
disabled={sending || pendingFiles.length >= 5}
|
|
326
|
+
aria-label="Đính kèm tệp"
|
|
327
|
+
>
|
|
328
|
+
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"/></svg>
|
|
329
|
+
</button>
|
|
234
330
|
<textarea
|
|
235
331
|
style={s.textarea}
|
|
236
332
|
value={inputText}
|
|
@@ -240,7 +336,7 @@ export function LiveChatWidget({ apiUrl, widgetId, primaryColor = '#2563eb', gre
|
|
|
240
336
|
rows={1}
|
|
241
337
|
disabled={sending}
|
|
242
338
|
/>
|
|
243
|
-
<button style={s.sendBtn(primaryColor)} onClick={() => { void handleSend(); }} disabled={sending || !inputText.trim()} aria-label="Gửi">
|
|
339
|
+
<button style={s.sendBtn(primaryColor)} onClick={() => { void handleSend(); }} disabled={sending || (!inputText.trim() && pendingFiles.length === 0)} aria-label="Gửi">
|
|
244
340
|
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>
|
|
245
341
|
</button>
|
|
246
342
|
</div>
|
|
@@ -413,6 +509,19 @@ function styles(primaryColor: string) {
|
|
|
413
509
|
maxHeight: '80px',
|
|
414
510
|
overflowY: 'auto' as const,
|
|
415
511
|
},
|
|
512
|
+
attachBtn: {
|
|
513
|
+
background: 'none',
|
|
514
|
+
border: 'none',
|
|
515
|
+
cursor: 'pointer',
|
|
516
|
+
color: '#9ca3af',
|
|
517
|
+
display: 'flex',
|
|
518
|
+
alignItems: 'center',
|
|
519
|
+
justifyContent: 'center',
|
|
520
|
+
width: '32px',
|
|
521
|
+
height: '36px',
|
|
522
|
+
flexShrink: 0,
|
|
523
|
+
padding: 0,
|
|
524
|
+
},
|
|
416
525
|
sendBtn: (color: string) => ({
|
|
417
526
|
background: color,
|
|
418
527
|
border: 'none',
|
package/src/api.ts
CHANGED
|
@@ -24,7 +24,7 @@ export class ChannelHubClient {
|
|
|
24
24
|
|
|
25
25
|
assignConversation(conversationId: string, agent: ChannelHubAgent | null): Promise<Conversation> {
|
|
26
26
|
return this.request(`/v1/conversations/${encodeURIComponent(conversationId)}/assignee`, {
|
|
27
|
-
method: '
|
|
27
|
+
method: 'PUT',
|
|
28
28
|
body: JSON.stringify({ agent }),
|
|
29
29
|
});
|
|
30
30
|
}
|