@channelhub/widget 0.1.3 โ†’ 0.1.5

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.
@@ -10,10 +10,17 @@ interface LiveChatWidgetProps {
10
10
  hmac?: string;
11
11
  }
12
12
 
13
+ interface MessageAttachment {
14
+ type: 'image' | 'video' | 'audio' | 'file' | 'sticker';
15
+ url: string;
16
+ mimeType?: string;
17
+ }
18
+
13
19
  interface ChatMessage {
14
20
  id: string;
15
21
  direction: 'inbound' | 'outbound';
16
22
  text: string | null;
23
+ attachments?: MessageAttachment[];
17
24
  occurredAt: string;
18
25
  }
19
26
 
@@ -49,12 +56,16 @@ export function LiveChatWidget({ apiUrl, widgetId, primaryColor = '#2563eb', gre
49
56
  const [visitorToken, setVisitorToken] = useState<string | null>(null);
50
57
  const [conversationId, setConversationId] = useState<string | null>(null);
51
58
  const [unread, setUnread] = useState(0);
59
+ const [lightbox, setLightbox] = useState<{ url: string; kind: 'image' | 'video' } | null>(null);
52
60
  const afterRef = useRef<string | undefined>(undefined);
53
61
  const pollRef = useRef<ReturnType<typeof setInterval> | null>(null);
54
62
  const messagesEndRef = useRef<HTMLDivElement>(null);
55
63
 
56
64
  const base = apiUrl.replace(/\/$/, '');
57
65
 
66
+ const resolveUrl = (url: string): string =>
67
+ url.startsWith('/v1/media/proxy') ? `${base}${url}&tok=${encodeURIComponent(visitorToken ?? '')}` : url;
68
+
58
69
  const initSession = useCallback(async () => {
59
70
  try {
60
71
  const storedToken = getStoredVisitorToken(widgetId);
@@ -173,13 +184,50 @@ export function LiveChatWidget({ apiUrl, widgetId, primaryColor = '#2563eb', gre
173
184
  {messages.length === 0 && (
174
185
  <div style={s.greeting}>{greeting}</div>
175
186
  )}
176
- {messages.map(msg => (
177
- <div key={msg.id} style={msg.direction === 'inbound' ? s.msgOut : s.msgIn}>
178
- <div style={msg.direction === 'inbound' ? s.bubbleOut(primaryColor) : s.bubbleIn}>
179
- {msg.text}
187
+ {messages.map(msg => {
188
+ const isOut = msg.direction === 'inbound';
189
+ const align = isOut ? 'flex-end' : 'flex-start';
190
+ const images = msg.attachments?.filter(a => a.type === 'image') ?? [];
191
+ const stickers = msg.attachments?.filter(a => a.type === 'sticker') ?? [];
192
+ const videos = msg.attachments?.filter(a => a.type === 'video') ?? [];
193
+ const audios = msg.attachments?.filter(a => a.type === 'audio') ?? [];
194
+ const files = msg.attachments?.filter(a => a.type === 'file') ?? [];
195
+ const hasBubble = !!(msg.text || audios.length || files.length);
196
+ return (
197
+ <div key={msg.id} style={{ display: 'flex', flexDirection: 'column', alignItems: align, gap: 2 }}>
198
+ {stickers.map((a, i) => (
199
+ <img key={`st${i}`} src={resolveUrl(a.url)} alt="sticker" style={{ maxWidth: 100, maxHeight: 100, objectFit: 'contain' }} />
200
+ ))}
201
+ {images.map((a, i) => (
202
+ <img key={`img${i}`} src={resolveUrl(a.url)} alt="" onClick={() => setLightbox({ url: resolveUrl(a.url), kind: 'image' })}
203
+ style={{ display: 'block', maxWidth: 220, maxHeight: 220, borderRadius: 10, objectFit: 'cover', cursor: 'zoom-in' }} />
204
+ ))}
205
+ {videos.map((a, i) => (
206
+ <div key={`vid${i}`} onClick={() => setLightbox({ url: resolveUrl(a.url), kind: 'video' })}
207
+ style={{ position: 'relative', cursor: 'pointer', borderRadius: 10, overflow: 'hidden', maxWidth: 220 }}>
208
+ <video src={resolveUrl(a.url)} style={{ display: 'block', maxWidth: 220, maxHeight: 160, objectFit: 'cover' }} />
209
+ <div style={{ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center', background: 'rgba(0,0,0,.25)' }}>
210
+ <div style={{ width: 36, height: 36, borderRadius: '50%', background: 'rgba(255,255,255,.9)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
211
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="#172033"><polygon points="5 3 19 12 5 21 5 3"/></svg>
212
+ </div>
213
+ </div>
214
+ </div>
215
+ ))}
216
+ {hasBubble && (
217
+ <div style={isOut ? s.bubbleOut(primaryColor) : s.bubbleIn}>
218
+ {audios.map((a, i) => <audio key={i} src={resolveUrl(a.url)} controls style={{ display: 'block', width: '100%', marginBottom: 4 }} />)}
219
+ {files.map((a, i) => (
220
+ <a key={i} href={resolveUrl(a.url)} target="_blank" rel="noreferrer"
221
+ style={{ display: 'block', fontSize: 12, color: isOut ? '#fff' : '#2563eb', textDecoration: 'underline', marginBottom: 4 }}>
222
+ ๐Ÿ“Ž {a.mimeType ?? 'file'}
223
+ </a>
224
+ ))}
225
+ {msg.text}
226
+ </div>
227
+ )}
180
228
  </div>
181
- </div>
182
- ))}
229
+ );
230
+ })}
183
231
  <div ref={messagesEndRef} />
184
232
  </div>
185
233
  <div style={s.inputRow}>
@@ -199,6 +247,19 @@ export function LiveChatWidget({ apiUrl, widgetId, primaryColor = '#2563eb', gre
199
247
  {!conversationId && initialized && (
200
248
  <div style={s.poweredBy}>Powered by <a href="https://channelhub.io" target="_blank" rel="noopener noreferrer" style={s.poweredLink}>ChannelHub</a></div>
201
249
  )}
250
+ {lightbox && (
251
+ <div onClick={() => setLightbox(null)}
252
+ style={{ position: 'fixed', inset: 0, zIndex: 1000000, background: 'rgba(0,0,0,.88)', display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'zoom-out' }}>
253
+ {lightbox.kind === 'image'
254
+ ? <img src={lightbox.url} alt="" onClick={e => e.stopPropagation()} style={{ maxWidth: '90vw', maxHeight: '90vh', borderRadius: 8, objectFit: 'contain', boxShadow: '0 8px 40px rgba(0,0,0,.5)', cursor: 'default' }} />
255
+ : <video src={lightbox.url} controls autoPlay onClick={e => e.stopPropagation()} style={{ maxWidth: '90vw', maxHeight: '90vh', borderRadius: 8, boxShadow: '0 8px 40px rgba(0,0,0,.5)', cursor: 'default' }} />
256
+ }
257
+ <button onClick={() => setLightbox(null)}
258
+ style={{ position: 'absolute', top: 16, right: 16, background: 'rgba(255,255,255,.15)', border: 'none', borderRadius: '50%', width: 36, height: 36, cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#fff', fontSize: 18 }}>
259
+ โœ•
260
+ </button>
261
+ </div>
262
+ )}
202
263
  </div>
203
264
  )}
204
265
  <button style={s.bubble(primaryColor)} onClick={() => setOpen(o => !o)} aria-label={open ? 'ฤรณng chat' : 'MแปŸ chat'}>
package/src/api.ts CHANGED
@@ -48,6 +48,29 @@ export class ChannelHubClient {
48
48
  if (!response.ok && response.status !== 204) throw new Error(`markRead failed (${response.status})`);
49
49
  }
50
50
 
51
+ listCannedResponses(search?: string): Promise<{ id: string; title: string; content: string; shortcut: string | null }[]> {
52
+ const q = search?.trim() ? `?search=${encodeURIComponent(search)}` : '';
53
+ return this.request(`/v1/canned-responses${q}`);
54
+ }
55
+
56
+ getNote(conversationId: string): Promise<{ id: string; content: string; updatedAt: string } | null> {
57
+ return this.request(`/v1/conversations/${encodeURIComponent(conversationId)}/note`);
58
+ }
59
+
60
+ patchContact(contactId: string, externalId: string | null): Promise<{ id: string; externalId?: string }> {
61
+ return this.request(`/v1/contacts/${encodeURIComponent(contactId)}`, {
62
+ method: 'PATCH',
63
+ body: JSON.stringify({ externalId }),
64
+ });
65
+ }
66
+
67
+ upsertNote(conversationId: string, content: string): Promise<{ id: string; content: string; updatedAt: string }> {
68
+ return this.request(`/v1/conversations/${encodeURIComponent(conversationId)}/note`, {
69
+ method: 'PUT',
70
+ body: JSON.stringify({ content }),
71
+ });
72
+ }
73
+
51
74
  async uploadAttachment(file: File): Promise<{ url: string; mimeType: string; attachmentType: string }> {
52
75
  const form = new FormData();
53
76
  form.append('file', file);
@@ -1,2 +1,7 @@
1
+ import { forwardRef } from 'react';
1
2
  import type { TextareaHTMLAttributes } from 'react';
2
- export function Textarea({ style, ...props }: TextareaHTMLAttributes<HTMLTextAreaElement>) { return <textarea {...props} style={{ display: 'block', width: '100%', minWidth: 0, boxSizing: 'border-box', resize: 'vertical', border: 0, outline: 0, background: 'transparent', font: 'inherit', ...style }} />; }
3
+ export const Textarea = forwardRef<HTMLTextAreaElement, TextareaHTMLAttributes<HTMLTextAreaElement>>(
4
+ function Textarea({ style, ...props }, ref) {
5
+ return <textarea ref={ref} {...props} style={{ display: 'block', width: '100%', minWidth: 0, boxSizing: 'border-box', resize: 'vertical', border: 0, outline: 0, background: 'transparent', font: 'inherit', ...style }} />;
6
+ }
7
+ );
package/src/types.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type React from 'react';
2
+
1
3
  export type Provider = 'facebook' | 'instagram' | 'telegram' | 'zalo' | string;
2
4
 
3
5
  export type Connection = { id: string; provider: Provider; type?: string; metadata?: Record<string, unknown> };
@@ -9,6 +11,9 @@ export type Conversation = {
9
11
  subject?: string;
10
12
  contactName?: string;
11
13
  contactPictureUrl?: string;
14
+ contactId?: string;
15
+ contactProviderUserId?: string;
16
+ contactExternalId?: string;
12
17
  lastMessageAt?: string;
13
18
  lastMessageText?: string;
14
19
  lastMessageDirection?: 'inbound' | 'outbound';
@@ -50,6 +55,53 @@ export type ChannelHubInboxHandle = {
50
55
  refresh: () => void;
51
56
  };
52
57
 
58
+ // โ”€โ”€โ”€ Extension System โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
59
+
60
+ export interface ExtensionContact {
61
+ id: string;
62
+ name?: string;
63
+ pictureUrl?: string;
64
+ providerUserId: string;
65
+ externalId?: string;
66
+ }
67
+
68
+ export interface ExtensionContext {
69
+ conversation: Conversation;
70
+ contact: ExtensionContact;
71
+ }
72
+
73
+ export interface ContactInfoRow {
74
+ label: string;
75
+ value: string;
76
+ href?: string;
77
+ }
78
+
79
+ export interface SidebarAction {
80
+ label: string;
81
+ icon?: React.ReactNode;
82
+ onClick: (ctx: ExtensionContext) => void | Promise<void>;
83
+ }
84
+
85
+ export interface ToolbarAction {
86
+ icon: React.ReactNode;
87
+ tooltip: string;
88
+ onClick: (ctx: ExtensionContext & { insertText: (text: string) => void }) => void | Promise<void>;
89
+ }
90
+
91
+ export interface MessageEnrichment {
92
+ pattern: RegExp;
93
+ render: (match: RegExpMatchArray, ctx: ExtensionContext) => React.ReactNode;
94
+ }
95
+
96
+ export interface Extension {
97
+ contactInfoRows?: (ctx: ExtensionContext) => Promise<ContactInfoRow[]>;
98
+ sidebarActions?: SidebarAction[];
99
+ toolbarActions?: ToolbarAction[];
100
+ messageEnrichments?: MessageEnrichment[];
101
+ }
102
+
103
+ // โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
104
+
53
105
  export type WidgetProps = {
54
106
  apiUrl: string;
55
107
  token: string;
@@ -60,6 +112,7 @@ export type WidgetProps = {
60
112
  disableNotificationSound?: boolean;
61
113
  hideKeyboardHint?: boolean;
62
114
  locale?: 'vi' | 'en';
115
+ extensions?: Extension[];
63
116
  onNewMessage?: (conversationId: string) => void;
64
117
  onConversationSelect?: (conversationId: string) => void;
65
118
  };