@channelhub/widget 0.1.4 → 0.1.6
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/ChannelHubInbox.d.ts.map +1 -1
- package/dist/api.d.ts +4 -0
- package/dist/api.d.ts.map +1 -1
- package/dist/index.cjs +26 -25
- package/dist/index.js +966 -753
- package/dist/types.d.ts +55 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/ChannelHubInbox.tsx +269 -17
- package/src/api.ts +7 -0
- package/src/types.ts +65 -0
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type React from 'react';
|
|
1
2
|
export type Provider = 'facebook' | 'instagram' | 'telegram' | 'zalo' | string;
|
|
2
3
|
export type Connection = {
|
|
3
4
|
id: string;
|
|
@@ -12,6 +13,9 @@ export type Conversation = {
|
|
|
12
13
|
subject?: string;
|
|
13
14
|
contactName?: string;
|
|
14
15
|
contactPictureUrl?: string;
|
|
16
|
+
contactId?: string;
|
|
17
|
+
contactProviderUserId?: string;
|
|
18
|
+
contactExternalId?: string;
|
|
15
19
|
lastMessageAt?: string;
|
|
16
20
|
lastMessageText?: string;
|
|
17
21
|
lastMessageDirection?: 'inbound' | 'outbound';
|
|
@@ -50,6 +54,56 @@ export type ChannelHubInboxHandle = {
|
|
|
50
54
|
openConversation: (conversationId: string) => void;
|
|
51
55
|
refresh: () => void;
|
|
52
56
|
};
|
|
57
|
+
export interface ExtensionContact {
|
|
58
|
+
id: string;
|
|
59
|
+
name?: string;
|
|
60
|
+
pictureUrl?: string;
|
|
61
|
+
providerUserId: string;
|
|
62
|
+
externalId?: string;
|
|
63
|
+
}
|
|
64
|
+
export interface ExtensionContext {
|
|
65
|
+
conversation: Conversation;
|
|
66
|
+
contact: ExtensionContact;
|
|
67
|
+
provider?: Provider;
|
|
68
|
+
recentMessages?: Message[];
|
|
69
|
+
linkContact: (externalId: string | null) => Promise<void>;
|
|
70
|
+
refreshContactInfo: () => void;
|
|
71
|
+
sendMessage: (text?: string, attachments?: MessageAttachment[]) => Promise<void>;
|
|
72
|
+
}
|
|
73
|
+
export interface ContactInfoRow {
|
|
74
|
+
label: string;
|
|
75
|
+
value: string;
|
|
76
|
+
href?: string;
|
|
77
|
+
onClick?: () => void;
|
|
78
|
+
badge?: {
|
|
79
|
+
text: string;
|
|
80
|
+
color?: string;
|
|
81
|
+
bg?: string;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export interface SidebarAction {
|
|
85
|
+
label: string;
|
|
86
|
+
icon?: React.ReactNode;
|
|
87
|
+
variant?: 'primary' | 'default';
|
|
88
|
+
onClick: (ctx: ExtensionContext) => void | Promise<void>;
|
|
89
|
+
}
|
|
90
|
+
export interface ToolbarAction {
|
|
91
|
+
icon: React.ReactNode;
|
|
92
|
+
tooltip: string;
|
|
93
|
+
onClick: (ctx: ExtensionContext & {
|
|
94
|
+
insertText: (text: string) => void;
|
|
95
|
+
}) => void | Promise<void>;
|
|
96
|
+
}
|
|
97
|
+
export interface MessageEnrichment {
|
|
98
|
+
pattern: RegExp;
|
|
99
|
+
render: (match: RegExpMatchArray, ctx: ExtensionContext, message: Message) => React.ReactNode;
|
|
100
|
+
}
|
|
101
|
+
export interface Extension {
|
|
102
|
+
contactInfoRows?: (ctx: ExtensionContext) => Promise<ContactInfoRow[]>;
|
|
103
|
+
sidebarActions?: SidebarAction[];
|
|
104
|
+
toolbarActions?: ToolbarAction[];
|
|
105
|
+
messageEnrichments?: MessageEnrichment[];
|
|
106
|
+
}
|
|
53
107
|
export type WidgetProps = {
|
|
54
108
|
apiUrl: string;
|
|
55
109
|
token: string;
|
|
@@ -60,6 +114,7 @@ export type WidgetProps = {
|
|
|
60
114
|
disableNotificationSound?: boolean;
|
|
61
115
|
hideKeyboardHint?: boolean;
|
|
62
116
|
locale?: 'vi' | 'en';
|
|
117
|
+
extensions?: Extension[];
|
|
63
118
|
onNewMessage?: (conversationId: string) => void;
|
|
64
119
|
onConversationSelect?: (conversationId: string) => void;
|
|
65
120
|
};
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG,UAAU,GAAG,WAAW,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAE/E,MAAM,MAAM,UAAU,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,CAAC;AAE/G,MAAM,MAAM,YAAY,GAAG;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,YAAY,EAAE,MAAM,CAAC;IACrB,uBAAuB,EAAE,MAAM,CAAC;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,oBAAoB,CAAC,EAAE,SAAS,GAAG,UAAU,CAAC;IAC9C,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,SAAS,CAAC;IACvD,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,SAAS,GAAG,UAAU,CAAC;IAClC,MAAM,CAAC,EAAE,SAAS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AAC,EAAE,MAAM,CAAC;IAC/B,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,MAAM,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IACrB,UAAU,CAAC,EAAE,SAAS,EAAE,CAAC;IACzB,YAAY,CAAC,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,IAAI,CAAC;IAChD,oBAAoB,CAAC,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,IAAI,CAAC;CACzD,CAAC"}
|
package/package.json
CHANGED
package/src/ChannelHubInbox.tsx
CHANGED
|
@@ -1,12 +1,50 @@
|
|
|
1
|
-
import { FormEvent, createContext, forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
|
1
|
+
import { Component, FormEvent, createContext, forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
2
3
|
import { createPortal } from 'react-dom';
|
|
3
4
|
import { AlertCircle, Check, CheckCheck, ChevronDown, ChevronRight, Image, Inbox, Info, Loader2, MoreHorizontal, Paperclip, Play, Search, Send, SlidersHorizontal, Smile, X, Zap } from 'lucide-react';
|
|
4
5
|
import { ChannelHubClient } from './api.js';
|
|
5
|
-
import type { ChannelHubInboxHandle, Connection, Conversation, Message, MessageAttachment, Page, Provider, WidgetProps } from './types.js';
|
|
6
|
+
import type { ChannelHubInboxHandle, Connection, Conversation, ContactInfoRow, Extension, ExtensionContact, ExtensionContext, Message, MessageAttachment, Page, Provider, WidgetProps } from './types.js';
|
|
6
7
|
|
|
7
8
|
type WidgetTranslations = { today: string; loadOlder: string; loading: string; noMessages: string; searchPlaceholder: string; unreadOnly: string; sendPlaceholder: string; send: string; all: string };
|
|
8
9
|
import { Avatar as UiAvatar, Button, Input, ScrollArea, Textarea } from './components/ui/index.js';
|
|
9
10
|
|
|
11
|
+
function buildContact(conv: Conversation): ExtensionContact {
|
|
12
|
+
return {
|
|
13
|
+
id: conv.contactId ?? conv.providerConversationKey,
|
|
14
|
+
name: conv.contactName,
|
|
15
|
+
pictureUrl: conv.contactPictureUrl,
|
|
16
|
+
providerUserId: conv.contactProviderUserId ?? conv.providerConversationKey,
|
|
17
|
+
...(conv.contactExternalId ? { externalId: conv.contactExternalId } : {}),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function buildCtx(
|
|
22
|
+
conv: Conversation,
|
|
23
|
+
opts?: {
|
|
24
|
+
provider?: Provider;
|
|
25
|
+
recentMessages?: Message[];
|
|
26
|
+
linkContact?: (externalId: string | null) => Promise<void>;
|
|
27
|
+
refreshContactInfo?: () => void;
|
|
28
|
+
sendMessage?: (text?: string, attachments?: MessageAttachment[]) => Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
): ExtensionContext {
|
|
31
|
+
return {
|
|
32
|
+
conversation: conv,
|
|
33
|
+
contact: buildContact(conv),
|
|
34
|
+
provider: opts?.provider,
|
|
35
|
+
recentMessages: opts?.recentMessages,
|
|
36
|
+
linkContact: opts?.linkContact ?? (async () => undefined),
|
|
37
|
+
refreshContactInfo: opts?.refreshContactInfo ?? (() => undefined),
|
|
38
|
+
sendMessage: opts?.sendMessage ?? (async () => undefined),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class ExtErrorBoundary extends Component<{ children: ReactNode }, { caught: boolean }> {
|
|
43
|
+
state = { caught: false };
|
|
44
|
+
static getDerivedStateFromError() { return { caught: true }; }
|
|
45
|
+
render() { return this.state.caught ? null : this.props.children; }
|
|
46
|
+
}
|
|
47
|
+
|
|
10
48
|
const c = { ink: '#172033', muted: '#718096', line: '#e7ebf2', surface: '#f7f9fc', white: '#fff' };
|
|
11
49
|
|
|
12
50
|
const EMOJI_CATEGORIES: { icon: string; emojis: string[] }[] = [
|
|
@@ -52,6 +90,7 @@ function providerLabel(p?: Provider): string {
|
|
|
52
90
|
if (p === 'instagram') return 'Instagram';
|
|
53
91
|
if (p === 'telegram') return 'Telegram';
|
|
54
92
|
if (p === 'zalo') return 'Zalo OA';
|
|
93
|
+
if (p === 'livechat') return 'Live Chat';
|
|
55
94
|
return 'Channel';
|
|
56
95
|
}
|
|
57
96
|
|
|
@@ -60,6 +99,7 @@ function providerShortLabel(p?: Provider): string {
|
|
|
60
99
|
if (p === 'instagram') return 'Instagram';
|
|
61
100
|
if (p === 'telegram') return 'Telegram';
|
|
62
101
|
if (p === 'zalo') return 'Zalo';
|
|
102
|
+
if (p === 'livechat') return 'Live Chat';
|
|
63
103
|
return String(p ?? '');
|
|
64
104
|
}
|
|
65
105
|
|
|
@@ -83,7 +123,7 @@ function StatusIcon({ status }: { status?: Message['status'] }) {
|
|
|
83
123
|
type ActiveFilters = { provider: Provider | 'all'; unreadOnly: boolean };
|
|
84
124
|
|
|
85
125
|
export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(function ChannelHubInbox(
|
|
86
|
-
{ apiUrl, token, theme, height, defaultConversationId, disableAttachments, disableNotificationSound, hideKeyboardHint, locale = 'vi', onNewMessage, onConversationSelect },
|
|
126
|
+
{ apiUrl, token, theme, height, defaultConversationId, disableAttachments, disableNotificationSound, hideKeyboardHint, locale = 'vi', extensions, onNewMessage, onConversationSelect },
|
|
87
127
|
ref
|
|
88
128
|
) {
|
|
89
129
|
const client = useRef(new ChannelHubClient(apiUrl, token));
|
|
@@ -96,14 +136,30 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
96
136
|
const filterPopoverRef = useRef<HTMLDivElement>(null);
|
|
97
137
|
const [popoverAnchor, setPopoverAnchor] = useState<{ top: number; left: number } | null>(null);
|
|
98
138
|
const [loading, setLoading] = useState(true); const [error, setError] = useState('');
|
|
99
|
-
const [infoOpen, setInfoOpen] = useState(
|
|
139
|
+
const [infoOpen, setInfoOpen] = useState(() => Boolean(extensions?.length));
|
|
140
|
+
const [infoPanelWidth, setInfoPanelWidth] = useState(280);
|
|
141
|
+
const [isResizing, setIsResizing] = useState(false);
|
|
142
|
+
const [extRefreshKey, setExtRefreshKey] = useState(0);
|
|
100
143
|
const primary = theme?.primaryColor ?? '#2563eb';
|
|
101
144
|
const connectionById = useMemo(() => new Map(connections.map(connection => [connection.id, connection])), [connections]);
|
|
102
145
|
const appendMessageRef = useRef<((msg: Message) => void) | null>(null);
|
|
103
146
|
const [msgRefreshKey, setMsgRefreshKey] = useState(0);
|
|
147
|
+
|
|
148
|
+
const handleLinkContact = useCallback(async (conv: Conversation, externalId: string | null) => {
|
|
149
|
+
if (conv.contactId) {
|
|
150
|
+
await client.current.patchContact(conv.contactId, externalId);
|
|
151
|
+
}
|
|
152
|
+
setItems(prev => prev.map(item => item.id === conv.id ? { ...item, contactExternalId: externalId ?? undefined } : item));
|
|
153
|
+
setSelected(prev => prev && prev.id === conv.id ? { ...prev, contactExternalId: externalId ?? undefined } : prev);
|
|
154
|
+
setExtRefreshKey(k => k + 1);
|
|
155
|
+
}, []);
|
|
156
|
+
|
|
157
|
+
const handleRefreshContactInfo = useCallback(() => {
|
|
158
|
+
setExtRefreshKey(k => k + 1);
|
|
159
|
+
}, []);
|
|
104
160
|
const availableProviders = useMemo(() => {
|
|
105
161
|
const ps = new Set(connections.map(cn => cn.provider));
|
|
106
|
-
return (['facebook', 'instagram', 'telegram', 'zalo'] as const).filter(p => ps.has(p));
|
|
162
|
+
return (['facebook', 'instagram', 'telegram', 'zalo', 'livechat'] as const).filter(p => ps.has(p));
|
|
107
163
|
}, [connections]);
|
|
108
164
|
const countByProvider = useMemo(() => {
|
|
109
165
|
const counts: Record<string, number> = {};
|
|
@@ -113,6 +169,26 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
113
169
|
}
|
|
114
170
|
return counts;
|
|
115
171
|
}, [items, connectionById]);
|
|
172
|
+
function startInfoResize(e: React.MouseEvent) {
|
|
173
|
+
const startX = e.clientX;
|
|
174
|
+
const startWidth = infoPanelWidth;
|
|
175
|
+
setIsResizing(true);
|
|
176
|
+
document.body.style.cursor = 'col-resize';
|
|
177
|
+
document.body.style.userSelect = 'none';
|
|
178
|
+
function onMove(ev: MouseEvent) {
|
|
179
|
+
const newWidth = Math.max(200, Math.min(480, startWidth + (startX - ev.clientX)));
|
|
180
|
+
setInfoPanelWidth(newWidth);
|
|
181
|
+
}
|
|
182
|
+
function onUp() {
|
|
183
|
+
setIsResizing(false);
|
|
184
|
+
document.body.style.cursor = '';
|
|
185
|
+
document.body.style.userSelect = '';
|
|
186
|
+
document.removeEventListener('mousemove', onMove);
|
|
187
|
+
document.removeEventListener('mouseup', onUp);
|
|
188
|
+
}
|
|
189
|
+
document.addEventListener('mousemove', onMove);
|
|
190
|
+
document.addEventListener('mouseup', onUp);
|
|
191
|
+
}
|
|
116
192
|
const load = useCallback(async (next?: string) => { try { const page: Page<Conversation> = await client.current.listConversations(next); setItems(old => next ? [...old, ...page.items] : page.items); setCursor(page.nextCursor); if (!next && page.items.length) setSelected(old => old ?? (defaultConversationId ? (page.items.find(c => c.id === defaultConversationId) ?? page.items[0]) : page.items[0])); setError(''); } catch (e) { setError(e instanceof Error ? e.message : 'Không tải được hội thoại'); } finally { setLoading(false); } }, [defaultConversationId]);
|
|
117
193
|
useEffect(() => { void client.current.listConnections().then(setConnections).catch(() => undefined); void load(); const stop = client.current.connectSSE((type, data) => { if (type === 'message.new' || type === 'conversation.updated') { void load(); if (type === 'message.new') { const evtData = typeof data === 'object' && data ? data as Record<string, unknown> : {}; const evtConvId = evtData['conversationId'] ? String(evtData['conversationId']) : ''; const evtMsgId = evtData['messageId'] ? String(evtData['messageId']) : ''; const evtDirection = evtData['direction'] ? String(evtData['direction']) : ''; if (evtDirection === 'inbound' && !disableNotificationSound) playNotificationSound(); if (evtConvId && evtConvId === selectedId.current && evtMsgId && appendMessageRef.current) { const appendFn = appendMessageRef.current; void client.current.getMessage(evtMsgId).then(msg => { appendFn(msg); }).catch(() => undefined); } onNewMessage?.(evtConvId || selectedId.current || ''); } } else if (type === 'message.status_updated') { setMsgRefreshKey(k => k + 1); } }); return stop; }, [load, onNewMessage]);
|
|
118
194
|
useEffect(() => { selectedId.current = selected?.id; }, [selected]);
|
|
@@ -162,9 +238,9 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
162
238
|
fontFamily: theme?.fontFamily ?? 'Inter, ui-sans-serif, system-ui, sans-serif',
|
|
163
239
|
borderRadius: theme?.borderRadius ?? '16px',
|
|
164
240
|
gridTemplateColumns: infoOpen
|
|
165
|
-
?
|
|
241
|
+
? `minmax(260px, 300px) minmax(0, 1fr) ${infoPanelWidth}px`
|
|
166
242
|
: 'minmax(260px, 300px) minmax(0, 1fr)',
|
|
167
|
-
transition: 'grid-template-columns 220ms ease',
|
|
243
|
+
transition: isResizing ? 'none' : 'grid-template-columns 220ms ease',
|
|
168
244
|
};
|
|
169
245
|
if (loading) return <div style={{ ...css.state, borderRadius: theme?.borderRadius ?? '16px' }}><Spinner /> Đang tải hộp thư…</div>;
|
|
170
246
|
if (error && !items.length) return <div style={{ ...css.state, color: '#b42318', borderRadius: theme?.borderRadius ?? '16px' }}><AlertCircle size={16} strokeWidth={2} /> {error}<button style={css.retry} onClick={() => { setLoading(true); void load(); }}>Thử lại</button></div>;
|
|
@@ -272,6 +348,9 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
272
348
|
t={t}
|
|
273
349
|
disableAttachments={disableAttachments}
|
|
274
350
|
hideKeyboardHint={hideKeyboardHint}
|
|
351
|
+
extensions={extensions}
|
|
352
|
+
onLinkContact={(extId) => handleLinkContact(selected, extId)}
|
|
353
|
+
onRefreshContactInfo={handleRefreshContactInfo}
|
|
275
354
|
/>
|
|
276
355
|
) : <EmptyConversation />}
|
|
277
356
|
</main>
|
|
@@ -282,6 +361,11 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
282
361
|
primary={primary}
|
|
283
362
|
onClose={() => setInfoOpen(false)}
|
|
284
363
|
client={client.current}
|
|
364
|
+
onStartResize={startInfoResize}
|
|
365
|
+
extensions={extensions}
|
|
366
|
+
extRefreshKey={extRefreshKey}
|
|
367
|
+
onLinkContact={(extId) => handleLinkContact(selected, extId)}
|
|
368
|
+
onRefreshContactInfo={handleRefreshContactInfo}
|
|
285
369
|
/>
|
|
286
370
|
)}
|
|
287
371
|
</div>
|
|
@@ -342,7 +426,7 @@ function ConversationRow({ conversation, provider, selected, primary, onSelect }
|
|
|
342
426
|
|
|
343
427
|
type PendingAttachment = { file: File; uploading: boolean; url?: string; mimeType?: string; attachmentType?: string; objectUrl?: string };
|
|
344
428
|
|
|
345
|
-
function MessagePanel({ conversation, provider, connection, client, primary, error, onRegisterAppend, refreshKey, infoOpen, onToggleInfo, t, disableAttachments, hideKeyboardHint }: { conversation: Conversation; provider?: Provider; connection?: Connection; client: ChannelHubClient; primary: string; error: string; onRegisterAppend?: (fn: ((msg: Message) => void) | null) => void; refreshKey?: number; infoOpen: boolean; onToggleInfo: () => void; t: WidgetTranslations; disableAttachments?: boolean; hideKeyboardHint?: boolean }) {
|
|
429
|
+
function MessagePanel({ conversation, provider, connection, client, primary, error, onRegisterAppend, refreshKey, infoOpen, onToggleInfo, t, disableAttachments, hideKeyboardHint, extensions, onLinkContact, onRefreshContactInfo }: { conversation: Conversation; provider?: Provider; connection?: Connection; client: ChannelHubClient; primary: string; error: string; onRegisterAppend?: (fn: ((msg: Message) => void) | null) => void; refreshKey?: number; infoOpen: boolean; onToggleInfo: () => void; t: WidgetTranslations; disableAttachments?: boolean; hideKeyboardHint?: boolean; extensions?: Extension[]; onLinkContact: (externalId: string | null) => Promise<void>; onRefreshContactInfo: () => void }) {
|
|
346
430
|
const [messages, setMessages] = useState<Message[]>([]); const [cursor, setCursor] = useState<string>(); const [text, setText] = useState(''); const [loading, setLoading] = useState(true); const [sending, setSending] = useState(false); const [sendError, setSendError] = useState(''); const messageAreaRef = useRef<HTMLDivElement>(null); const name = conversation.contactName ?? conversation.subject ?? conversation.providerConversationKey;
|
|
347
431
|
const fileImageRef = useRef<HTMLInputElement>(null);
|
|
348
432
|
const fileDocRef = useRef<HTMLInputElement>(null);
|
|
@@ -512,7 +596,32 @@ function MessagePanel({ conversation, provider, connection, client, primary, err
|
|
|
512
596
|
{cursor && <button onClick={() => void load(cursor)} style={css.historyButton}>{t.loadOlder}</button>}
|
|
513
597
|
{loading && <div style={css.messageHint}><Spinner /> {t.loading}</div>}
|
|
514
598
|
{!loading && !messages.length && <div style={css.messageHint}>{t.noMessages}</div>}
|
|
515
|
-
{messages.map(m =>
|
|
599
|
+
{messages.map((m, idx) => {
|
|
600
|
+
const enrichments = extensions?.flatMap(e => e.messageEnrichments ?? []) ?? [];
|
|
601
|
+
const shouldEnrich = m.direction === 'inbound' && enrichments.length > 0 && idx >= messages.length - 15;
|
|
602
|
+
const panelCtx = shouldEnrich
|
|
603
|
+
? buildCtx(conversation, {
|
|
604
|
+
provider,
|
|
605
|
+
recentMessages: messages.slice(-10),
|
|
606
|
+
linkContact: onLinkContact,
|
|
607
|
+
refreshContactInfo: onRefreshContactInfo,
|
|
608
|
+
sendMessage: async (msgText, msgAttachments) => {
|
|
609
|
+
await client.sendMessage(conversation.id, msgText, msgAttachments);
|
|
610
|
+
await load(undefined, true);
|
|
611
|
+
},
|
|
612
|
+
})
|
|
613
|
+
: null;
|
|
614
|
+
return (
|
|
615
|
+
<div key={m.id}>
|
|
616
|
+
<MessageBubble message={m} primary={primary} />
|
|
617
|
+
{shouldEnrich && panelCtx && enrichments.map((enrich, i) => {
|
|
618
|
+
const match = m.text?.match(enrich.pattern);
|
|
619
|
+
if (!match) return null;
|
|
620
|
+
return <ExtErrorBoundary key={`${m.id}-${i}`}>{enrich.render(match, panelCtx, m)}</ExtErrorBoundary>;
|
|
621
|
+
})}
|
|
622
|
+
</div>
|
|
623
|
+
);
|
|
624
|
+
})}
|
|
516
625
|
</ScrollArea>
|
|
517
626
|
{/* Quick reply chips */}
|
|
518
627
|
<div style={{ display: 'flex', gap: 6, flexWrap: 'wrap', padding: '6px 14px 2px', borderTop: `1px solid ${c.line}` }}>
|
|
@@ -716,6 +825,28 @@ function MessagePanel({ conversation, provider, connection, client, primary, err
|
|
|
716
825
|
style={{ ...css.toolbarBtn, fontStyle: 'italic', fontSize: 12, background: italicActive ? `${primary}18` : 'transparent', color: italicActive ? primary : '#9ca3af', border: italicActive ? `1px solid ${primary}33` : '1px solid transparent' }}>
|
|
717
826
|
I
|
|
718
827
|
</button>
|
|
828
|
+
{extensions?.flatMap(e => e.toolbarActions ?? []).map((action, i) => (
|
|
829
|
+
<button key={i} type="button" title={action.tooltip} style={css.toolbarBtn}
|
|
830
|
+
onClick={() => {
|
|
831
|
+
const insertText = (snippet: string) => {
|
|
832
|
+
setText(prev => (prev && !prev.endsWith(' ') && !prev.endsWith('\n') ? `${prev} ${snippet}` : `${prev}${snippet}`));
|
|
833
|
+
requestAnimationFrame(() => textareaRef.current?.focus());
|
|
834
|
+
};
|
|
835
|
+
const ctx = buildCtx(conversation, {
|
|
836
|
+
provider,
|
|
837
|
+
recentMessages: messages.slice(-10),
|
|
838
|
+
linkContact: onLinkContact,
|
|
839
|
+
refreshContactInfo: onRefreshContactInfo,
|
|
840
|
+
sendMessage: async (msgText, msgAttachments) => {
|
|
841
|
+
await client.sendMessage(conversation.id, msgText, msgAttachments);
|
|
842
|
+
await load(undefined, true);
|
|
843
|
+
},
|
|
844
|
+
});
|
|
845
|
+
void action.onClick({ ...ctx, insertText });
|
|
846
|
+
}}>
|
|
847
|
+
{action.icon}
|
|
848
|
+
</button>
|
|
849
|
+
))}
|
|
719
850
|
</div>
|
|
720
851
|
|
|
721
852
|
{/* Textarea */}
|
|
@@ -873,21 +1004,34 @@ function ContactInfoPanel({
|
|
|
873
1004
|
primary,
|
|
874
1005
|
onClose,
|
|
875
1006
|
client,
|
|
1007
|
+
onStartResize,
|
|
1008
|
+
extensions,
|
|
1009
|
+
extRefreshKey,
|
|
1010
|
+
onLinkContact,
|
|
1011
|
+
onRefreshContactInfo,
|
|
876
1012
|
}: {
|
|
877
1013
|
conversation: Conversation;
|
|
878
1014
|
connection?: Connection;
|
|
879
1015
|
primary: string;
|
|
880
1016
|
onClose: () => void;
|
|
881
1017
|
client: ChannelHubClient;
|
|
1018
|
+
onStartResize?: (e: React.MouseEvent) => void;
|
|
1019
|
+
extensions?: Extension[];
|
|
1020
|
+
extRefreshKey?: number;
|
|
1021
|
+
onLinkContact: (externalId: string | null) => Promise<void>;
|
|
1022
|
+
onRefreshContactInfo: () => void;
|
|
882
1023
|
}) {
|
|
883
1024
|
const name = conversation.contactName ?? conversation.subject ?? conversation.providerConversationKey;
|
|
884
1025
|
const provider = connection?.provider;
|
|
885
|
-
const
|
|
1026
|
+
const hasExtContactRows = Boolean(extensions?.some(e => e.contactInfoRows));
|
|
1027
|
+
const [expanded, setExpanded] = useState<Record<string, boolean>>({ channel: true, stats: false, extra: true });
|
|
886
1028
|
const toggle = (key: string) => setExpanded(e => ({ ...e, [key]: !e[key] }));
|
|
887
1029
|
const [noteText, setNoteText] = useState('');
|
|
888
1030
|
const [noteSaving, setNoteSaving] = useState(false);
|
|
889
1031
|
const [noteSaved, setNoteSaved] = useState(false);
|
|
890
1032
|
const [noteError, setNoteError] = useState('');
|
|
1033
|
+
const [extRows, setExtRows] = useState<ContactInfoRow[]>([]);
|
|
1034
|
+
const [extLoading, setExtLoading] = useState(false);
|
|
891
1035
|
|
|
892
1036
|
useEffect(() => {
|
|
893
1037
|
setNoteText('');
|
|
@@ -898,6 +1042,31 @@ function ContactInfoPanel({
|
|
|
898
1042
|
.catch(() => undefined);
|
|
899
1043
|
}, [conversation.id, client]);
|
|
900
1044
|
|
|
1045
|
+
useEffect(() => {
|
|
1046
|
+
if (!hasExtContactRows || !extensions) return;
|
|
1047
|
+
let cancelled = false;
|
|
1048
|
+
const ctx = buildCtx(conversation, {
|
|
1049
|
+
provider,
|
|
1050
|
+
linkContact: onLinkContact,
|
|
1051
|
+
refreshContactInfo: onRefreshContactInfo,
|
|
1052
|
+
sendMessage: async (msgText, msgAttachments) => {
|
|
1053
|
+
await client.sendMessage(conversation.id, msgText, msgAttachments);
|
|
1054
|
+
},
|
|
1055
|
+
});
|
|
1056
|
+
setExtLoading(true);
|
|
1057
|
+
void Promise.allSettled(
|
|
1058
|
+
extensions.filter(e => e.contactInfoRows).map(e => e.contactInfoRows!(ctx))
|
|
1059
|
+
).then(results => {
|
|
1060
|
+
if (cancelled) return;
|
|
1061
|
+
const rows = results
|
|
1062
|
+
.filter((r): r is PromiseFulfilledResult<ContactInfoRow[]> => r.status === 'fulfilled')
|
|
1063
|
+
.flatMap(r => r.value);
|
|
1064
|
+
setExtRows(rows);
|
|
1065
|
+
setExtLoading(false);
|
|
1066
|
+
});
|
|
1067
|
+
return () => { cancelled = true; };
|
|
1068
|
+
}, [conversation.id, conversation.contactExternalId, extensions, extRefreshKey, hasExtContactRows, provider, onLinkContact, onRefreshContactInfo, client]);
|
|
1069
|
+
|
|
901
1070
|
async function saveNote() {
|
|
902
1071
|
if (!noteText.trim() || noteSaving) return;
|
|
903
1072
|
setNoteSaving(true);
|
|
@@ -914,7 +1083,13 @@ function ContactInfoPanel({
|
|
|
914
1083
|
}
|
|
915
1084
|
|
|
916
1085
|
return (
|
|
917
|
-
<aside style={css.infoPanel}>
|
|
1086
|
+
<aside style={{ ...css.infoPanel, position: 'relative' }}>
|
|
1087
|
+
<div
|
|
1088
|
+
onMouseDown={onStartResize}
|
|
1089
|
+
style={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: 4, cursor: 'col-resize', zIndex: 1 }}
|
|
1090
|
+
onMouseEnter={e => { (e.currentTarget as HTMLElement).style.background = 'rgba(0,0,0,0.07)'; }}
|
|
1091
|
+
onMouseLeave={e => { (e.currentTarget as HTMLElement).style.background = ''; }}
|
|
1092
|
+
/>
|
|
918
1093
|
<div style={css.infoPanelHeader}>
|
|
919
1094
|
<span style={{ fontWeight: 600, fontSize: 13, color: c.ink }}>Thông tin liên hệ</span>
|
|
920
1095
|
<button style={css.iconButton} onClick={onClose} aria-label="Đóng">
|
|
@@ -970,16 +1145,93 @@ function ContactInfoPanel({
|
|
|
970
1145
|
</InfoSection>
|
|
971
1146
|
|
|
972
1147
|
<InfoSection label="THÔNG TIN BỔ SUNG" sectionKey="extra" expanded={expanded['extra']} onToggle={() => toggle('extra')}>
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
1148
|
+
{!hasExtContactRows && (
|
|
1149
|
+
<>
|
|
1150
|
+
<InfoRow label="Điện thoại" value="— chưa có" valueMuted />
|
|
1151
|
+
<InfoRow label="Email" value="— chưa có" valueMuted />
|
|
1152
|
+
<InfoRow label="Nhãn" value="— chưa có" valueMuted />
|
|
1153
|
+
</>
|
|
1154
|
+
)}
|
|
1155
|
+
{extLoading && extRows.length === 0 && (
|
|
1156
|
+
<div style={{ fontSize: 12, color: c.muted, padding: '6px 0' }}>Đang tải hồ sơ CRM…</div>
|
|
1157
|
+
)}
|
|
1158
|
+
{extRows.map(row => (
|
|
1159
|
+
<InfoRow key={row.label} label={row.label}>
|
|
1160
|
+
<span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, flexWrap: 'wrap', justifyContent: 'flex-end' }}>
|
|
1161
|
+
{row.onClick ? (
|
|
1162
|
+
<button
|
|
1163
|
+
type="button"
|
|
1164
|
+
onClick={row.onClick}
|
|
1165
|
+
style={{ border: 0, background: 'none', padding: 0, color: primary, fontSize: 12, fontWeight: 600, cursor: 'pointer', textDecoration: 'underline' }}
|
|
1166
|
+
>
|
|
1167
|
+
{row.value}
|
|
1168
|
+
</button>
|
|
1169
|
+
) : row.href ? (
|
|
1170
|
+
<a href={row.href} target="_blank" rel="noopener noreferrer" style={{ color: primary, fontSize: 12, fontWeight: 600 }}>
|
|
1171
|
+
{row.value}
|
|
1172
|
+
</a>
|
|
1173
|
+
) : (
|
|
1174
|
+
<span style={{ fontSize: 12, fontWeight: 500, color: c.ink }}>{row.value}</span>
|
|
1175
|
+
)}
|
|
1176
|
+
{row.badge && (
|
|
1177
|
+
<span
|
|
1178
|
+
style={{
|
|
1179
|
+
fontSize: 10.5,
|
|
1180
|
+
fontWeight: 700,
|
|
1181
|
+
padding: '1px 7px',
|
|
1182
|
+
borderRadius: 999,
|
|
1183
|
+
color: row.badge.color ?? primary,
|
|
1184
|
+
background: row.badge.bg ?? `${primary}14`,
|
|
1185
|
+
}}
|
|
1186
|
+
>
|
|
1187
|
+
{row.badge.text}
|
|
1188
|
+
</span>
|
|
1189
|
+
)}
|
|
1190
|
+
</span>
|
|
1191
|
+
</InfoRow>
|
|
1192
|
+
))}
|
|
976
1193
|
</InfoSection>
|
|
977
1194
|
|
|
978
1195
|
<div style={{ padding: '12px 16px', display: 'flex', flexDirection: 'column', gap: 8 }}>
|
|
979
|
-
|
|
980
|
-
|
|
1196
|
+
{!extensions?.length && (
|
|
1197
|
+
<button style={{ ...css.ghostButton, color: primary, borderColor: `${primary}33` }}>Xem lịch sử</button>
|
|
1198
|
+
)}
|
|
1199
|
+
{extensions?.flatMap(e => e.sidebarActions ?? []).map((action, i) => {
|
|
1200
|
+
const isPrimary = action.variant === 'primary';
|
|
1201
|
+
return (
|
|
1202
|
+
<button
|
|
1203
|
+
key={i}
|
|
1204
|
+
style={{
|
|
1205
|
+
...css.ghostButton,
|
|
1206
|
+
background: isPrimary ? primary : '#fff',
|
|
1207
|
+
color: isPrimary ? '#fff' : c.ink,
|
|
1208
|
+
borderColor: isPrimary ? primary : c.line,
|
|
1209
|
+
fontWeight: isPrimary ? 700 : 600,
|
|
1210
|
+
display: 'flex',
|
|
1211
|
+
alignItems: 'center',
|
|
1212
|
+
justifyContent: 'center',
|
|
1213
|
+
gap: 6,
|
|
1214
|
+
}}
|
|
1215
|
+
onClick={() => {
|
|
1216
|
+
const ctx = buildCtx(conversation, {
|
|
1217
|
+
provider,
|
|
1218
|
+
linkContact: onLinkContact,
|
|
1219
|
+
refreshContactInfo: onRefreshContactInfo,
|
|
1220
|
+
sendMessage: async (msgText, msgAttachments) => {
|
|
1221
|
+
await client.sendMessage(conversation.id, msgText, msgAttachments);
|
|
1222
|
+
},
|
|
1223
|
+
});
|
|
1224
|
+
void action.onClick(ctx);
|
|
1225
|
+
}}
|
|
1226
|
+
>
|
|
1227
|
+
{action.icon}
|
|
1228
|
+
{action.label}
|
|
1229
|
+
</button>
|
|
1230
|
+
);
|
|
1231
|
+
})}
|
|
1232
|
+
<div style={{ border: '1.5px solid #000', borderRadius: 10, background: '#fff', overflow: 'hidden' }}>
|
|
981
1233
|
<div style={{ display: 'flex', alignItems: 'center', gap: 6, padding: '7px 10px', borderBottom: '1px solid #fde68a', fontSize: 11, fontWeight: 600, color: '#92400e' }}>
|
|
982
|
-
|
|
1234
|
+
Ghi chú nội bộ
|
|
983
1235
|
</div>
|
|
984
1236
|
<textarea
|
|
985
1237
|
value={noteText}
|
package/src/api.ts
CHANGED
|
@@ -57,6 +57,13 @@ export class ChannelHubClient {
|
|
|
57
57
|
return this.request(`/v1/conversations/${encodeURIComponent(conversationId)}/note`);
|
|
58
58
|
}
|
|
59
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
|
+
|
|
60
67
|
upsertNote(conversationId: string, content: string): Promise<{ id: string; content: string; updatedAt: string }> {
|
|
61
68
|
return this.request(`/v1/conversations/${encodeURIComponent(conversationId)}/note`, {
|
|
62
69
|
method: 'PUT',
|
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,65 @@ 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
|
+
provider?: Provider;
|
|
72
|
+
recentMessages?: Message[];
|
|
73
|
+
linkContact: (externalId: string | null) => Promise<void>;
|
|
74
|
+
refreshContactInfo: () => void;
|
|
75
|
+
sendMessage: (text?: string, attachments?: MessageAttachment[]) => Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ContactInfoRow {
|
|
79
|
+
label: string;
|
|
80
|
+
value: string;
|
|
81
|
+
href?: string;
|
|
82
|
+
onClick?: () => void;
|
|
83
|
+
badge?: {
|
|
84
|
+
text: string;
|
|
85
|
+
color?: string;
|
|
86
|
+
bg?: string;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface SidebarAction {
|
|
91
|
+
label: string;
|
|
92
|
+
icon?: React.ReactNode;
|
|
93
|
+
variant?: 'primary' | 'default';
|
|
94
|
+
onClick: (ctx: ExtensionContext) => void | Promise<void>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface ToolbarAction {
|
|
98
|
+
icon: React.ReactNode;
|
|
99
|
+
tooltip: string;
|
|
100
|
+
onClick: (ctx: ExtensionContext & { insertText: (text: string) => void }) => void | Promise<void>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface MessageEnrichment {
|
|
104
|
+
pattern: RegExp;
|
|
105
|
+
render: (match: RegExpMatchArray, ctx: ExtensionContext, message: Message) => React.ReactNode;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface Extension {
|
|
109
|
+
contactInfoRows?: (ctx: ExtensionContext) => Promise<ContactInfoRow[]>;
|
|
110
|
+
sidebarActions?: SidebarAction[];
|
|
111
|
+
toolbarActions?: ToolbarAction[];
|
|
112
|
+
messageEnrichments?: MessageEnrichment[];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
116
|
+
|
|
53
117
|
export type WidgetProps = {
|
|
54
118
|
apiUrl: string;
|
|
55
119
|
token: string;
|
|
@@ -60,6 +124,7 @@ export type WidgetProps = {
|
|
|
60
124
|
disableNotificationSound?: boolean;
|
|
61
125
|
hideKeyboardHint?: boolean;
|
|
62
126
|
locale?: 'vi' | 'en';
|
|
127
|
+
extensions?: Extension[];
|
|
63
128
|
onNewMessage?: (conversationId: string) => void;
|
|
64
129
|
onConversationSelect?: (conversationId: string) => void;
|
|
65
130
|
};
|