@channelhub/widget 0.1.4 → 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.
- 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 +25 -25
- package/dist/index.js +894 -785
- package/dist/types.d.ts +43 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/ChannelHubInbox.tsx +118 -11
- package/src/api.ts +7 -0
- package/src/types.ts +53 -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,44 @@ 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
|
+
}
|
|
68
|
+
export interface ContactInfoRow {
|
|
69
|
+
label: string;
|
|
70
|
+
value: string;
|
|
71
|
+
href?: string;
|
|
72
|
+
}
|
|
73
|
+
export interface SidebarAction {
|
|
74
|
+
label: string;
|
|
75
|
+
icon?: React.ReactNode;
|
|
76
|
+
onClick: (ctx: ExtensionContext) => void | Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
export interface ToolbarAction {
|
|
79
|
+
icon: React.ReactNode;
|
|
80
|
+
tooltip: string;
|
|
81
|
+
onClick: (ctx: ExtensionContext & {
|
|
82
|
+
insertText: (text: string) => void;
|
|
83
|
+
}) => void | Promise<void>;
|
|
84
|
+
}
|
|
85
|
+
export interface MessageEnrichment {
|
|
86
|
+
pattern: RegExp;
|
|
87
|
+
render: (match: RegExpMatchArray, ctx: ExtensionContext) => React.ReactNode;
|
|
88
|
+
}
|
|
89
|
+
export interface Extension {
|
|
90
|
+
contactInfoRows?: (ctx: ExtensionContext) => Promise<ContactInfoRow[]>;
|
|
91
|
+
sidebarActions?: SidebarAction[];
|
|
92
|
+
toolbarActions?: ToolbarAction[];
|
|
93
|
+
messageEnrichments?: MessageEnrichment[];
|
|
94
|
+
}
|
|
53
95
|
export type WidgetProps = {
|
|
54
96
|
apiUrl: string;
|
|
55
97
|
token: string;
|
|
@@ -60,6 +102,7 @@ export type WidgetProps = {
|
|
|
60
102
|
disableNotificationSound?: boolean;
|
|
61
103
|
hideKeyboardHint?: boolean;
|
|
62
104
|
locale?: 'vi' | 'en';
|
|
105
|
+
extensions?: Extension[];
|
|
63
106
|
onNewMessage?: (conversationId: string) => void;
|
|
64
107
|
onConversationSelect?: (conversationId: string) => void;
|
|
65
108
|
};
|
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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IACvE,cAAc,CAAC,EAAE,aAAa,EAAE,CAAC;IACjC,cAAc,CAAC,EAAE,aAAa,EAAE,CAAC;IACjC,kBAAkB,CAAC,EAAE,iBAAiB,EAAE,CAAC;CAC1C;AAID,MAAM,MAAM,WAAW,GAAG;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,qBAAqB,CAAC,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,33 @@
|
|
|
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(conv: Conversation): ExtensionContext {
|
|
22
|
+
return { conversation: conv, contact: buildContact(conv) };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class ExtErrorBoundary extends Component<{ children: ReactNode }, { caught: boolean }> {
|
|
26
|
+
state = { caught: false };
|
|
27
|
+
static getDerivedStateFromError() { return { caught: true }; }
|
|
28
|
+
render() { return this.state.caught ? null : this.props.children; }
|
|
29
|
+
}
|
|
30
|
+
|
|
10
31
|
const c = { ink: '#172033', muted: '#718096', line: '#e7ebf2', surface: '#f7f9fc', white: '#fff' };
|
|
11
32
|
|
|
12
33
|
const EMOJI_CATEGORIES: { icon: string; emojis: string[] }[] = [
|
|
@@ -52,6 +73,7 @@ function providerLabel(p?: Provider): string {
|
|
|
52
73
|
if (p === 'instagram') return 'Instagram';
|
|
53
74
|
if (p === 'telegram') return 'Telegram';
|
|
54
75
|
if (p === 'zalo') return 'Zalo OA';
|
|
76
|
+
if (p === 'livechat') return 'Live Chat';
|
|
55
77
|
return 'Channel';
|
|
56
78
|
}
|
|
57
79
|
|
|
@@ -60,6 +82,7 @@ function providerShortLabel(p?: Provider): string {
|
|
|
60
82
|
if (p === 'instagram') return 'Instagram';
|
|
61
83
|
if (p === 'telegram') return 'Telegram';
|
|
62
84
|
if (p === 'zalo') return 'Zalo';
|
|
85
|
+
if (p === 'livechat') return 'Live Chat';
|
|
63
86
|
return String(p ?? '');
|
|
64
87
|
}
|
|
65
88
|
|
|
@@ -83,7 +106,7 @@ function StatusIcon({ status }: { status?: Message['status'] }) {
|
|
|
83
106
|
type ActiveFilters = { provider: Provider | 'all'; unreadOnly: boolean };
|
|
84
107
|
|
|
85
108
|
export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(function ChannelHubInbox(
|
|
86
|
-
{ apiUrl, token, theme, height, defaultConversationId, disableAttachments, disableNotificationSound, hideKeyboardHint, locale = 'vi', onNewMessage, onConversationSelect },
|
|
109
|
+
{ apiUrl, token, theme, height, defaultConversationId, disableAttachments, disableNotificationSound, hideKeyboardHint, locale = 'vi', extensions, onNewMessage, onConversationSelect },
|
|
87
110
|
ref
|
|
88
111
|
) {
|
|
89
112
|
const client = useRef(new ChannelHubClient(apiUrl, token));
|
|
@@ -97,13 +120,15 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
97
120
|
const [popoverAnchor, setPopoverAnchor] = useState<{ top: number; left: number } | null>(null);
|
|
98
121
|
const [loading, setLoading] = useState(true); const [error, setError] = useState('');
|
|
99
122
|
const [infoOpen, setInfoOpen] = useState(false);
|
|
123
|
+
const [infoPanelWidth, setInfoPanelWidth] = useState(260);
|
|
124
|
+
const [isResizing, setIsResizing] = useState(false);
|
|
100
125
|
const primary = theme?.primaryColor ?? '#2563eb';
|
|
101
126
|
const connectionById = useMemo(() => new Map(connections.map(connection => [connection.id, connection])), [connections]);
|
|
102
127
|
const appendMessageRef = useRef<((msg: Message) => void) | null>(null);
|
|
103
128
|
const [msgRefreshKey, setMsgRefreshKey] = useState(0);
|
|
104
129
|
const availableProviders = useMemo(() => {
|
|
105
130
|
const ps = new Set(connections.map(cn => cn.provider));
|
|
106
|
-
return (['facebook', 'instagram', 'telegram', 'zalo'] as const).filter(p => ps.has(p));
|
|
131
|
+
return (['facebook', 'instagram', 'telegram', 'zalo', 'livechat'] as const).filter(p => ps.has(p));
|
|
107
132
|
}, [connections]);
|
|
108
133
|
const countByProvider = useMemo(() => {
|
|
109
134
|
const counts: Record<string, number> = {};
|
|
@@ -113,6 +138,26 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
113
138
|
}
|
|
114
139
|
return counts;
|
|
115
140
|
}, [items, connectionById]);
|
|
141
|
+
function startInfoResize(e: React.MouseEvent) {
|
|
142
|
+
const startX = e.clientX;
|
|
143
|
+
const startWidth = infoPanelWidth;
|
|
144
|
+
setIsResizing(true);
|
|
145
|
+
document.body.style.cursor = 'col-resize';
|
|
146
|
+
document.body.style.userSelect = 'none';
|
|
147
|
+
function onMove(ev: MouseEvent) {
|
|
148
|
+
const newWidth = Math.max(200, Math.min(480, startWidth + (startX - ev.clientX)));
|
|
149
|
+
setInfoPanelWidth(newWidth);
|
|
150
|
+
}
|
|
151
|
+
function onUp() {
|
|
152
|
+
setIsResizing(false);
|
|
153
|
+
document.body.style.cursor = '';
|
|
154
|
+
document.body.style.userSelect = '';
|
|
155
|
+
document.removeEventListener('mousemove', onMove);
|
|
156
|
+
document.removeEventListener('mouseup', onUp);
|
|
157
|
+
}
|
|
158
|
+
document.addEventListener('mousemove', onMove);
|
|
159
|
+
document.addEventListener('mouseup', onUp);
|
|
160
|
+
}
|
|
116
161
|
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
162
|
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
163
|
useEffect(() => { selectedId.current = selected?.id; }, [selected]);
|
|
@@ -162,9 +207,9 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
162
207
|
fontFamily: theme?.fontFamily ?? 'Inter, ui-sans-serif, system-ui, sans-serif',
|
|
163
208
|
borderRadius: theme?.borderRadius ?? '16px',
|
|
164
209
|
gridTemplateColumns: infoOpen
|
|
165
|
-
?
|
|
210
|
+
? `minmax(260px, 300px) minmax(0, 1fr) ${infoPanelWidth}px`
|
|
166
211
|
: 'minmax(260px, 300px) minmax(0, 1fr)',
|
|
167
|
-
transition: 'grid-template-columns 220ms ease',
|
|
212
|
+
transition: isResizing ? 'none' : 'grid-template-columns 220ms ease',
|
|
168
213
|
};
|
|
169
214
|
if (loading) return <div style={{ ...css.state, borderRadius: theme?.borderRadius ?? '16px' }}><Spinner /> Đang tải hộp thư…</div>;
|
|
170
215
|
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 +317,7 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
272
317
|
t={t}
|
|
273
318
|
disableAttachments={disableAttachments}
|
|
274
319
|
hideKeyboardHint={hideKeyboardHint}
|
|
320
|
+
extensions={extensions}
|
|
275
321
|
/>
|
|
276
322
|
) : <EmptyConversation />}
|
|
277
323
|
</main>
|
|
@@ -282,6 +328,8 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
282
328
|
primary={primary}
|
|
283
329
|
onClose={() => setInfoOpen(false)}
|
|
284
330
|
client={client.current}
|
|
331
|
+
onStartResize={startInfoResize}
|
|
332
|
+
extensions={extensions}
|
|
285
333
|
/>
|
|
286
334
|
)}
|
|
287
335
|
</div>
|
|
@@ -342,7 +390,7 @@ function ConversationRow({ conversation, provider, selected, primary, onSelect }
|
|
|
342
390
|
|
|
343
391
|
type PendingAttachment = { file: File; uploading: boolean; url?: string; mimeType?: string; attachmentType?: string; objectUrl?: string };
|
|
344
392
|
|
|
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 }) {
|
|
393
|
+
function MessagePanel({ conversation, provider, connection, client, primary, error, onRegisterAppend, refreshKey, infoOpen, onToggleInfo, t, disableAttachments, hideKeyboardHint, extensions }: { 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[] }) {
|
|
346
394
|
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
395
|
const fileImageRef = useRef<HTMLInputElement>(null);
|
|
348
396
|
const fileDocRef = useRef<HTMLInputElement>(null);
|
|
@@ -512,7 +560,20 @@ function MessagePanel({ conversation, provider, connection, client, primary, err
|
|
|
512
560
|
{cursor && <button onClick={() => void load(cursor)} style={css.historyButton}>{t.loadOlder}</button>}
|
|
513
561
|
{loading && <div style={css.messageHint}><Spinner /> {t.loading}</div>}
|
|
514
562
|
{!loading && !messages.length && <div style={css.messageHint}>{t.noMessages}</div>}
|
|
515
|
-
{messages.map(m =>
|
|
563
|
+
{messages.map((m, idx) => {
|
|
564
|
+
const enrichments = extensions?.flatMap(e => e.messageEnrichments ?? []) ?? [];
|
|
565
|
+
const shouldEnrich = m.direction === 'inbound' && enrichments.length > 0 && idx >= messages.length - 15;
|
|
566
|
+
return (
|
|
567
|
+
<div key={m.id}>
|
|
568
|
+
<MessageBubble message={m} primary={primary} />
|
|
569
|
+
{shouldEnrich && enrichments.map((enrich, i) => {
|
|
570
|
+
const match = m.text?.match(enrich.pattern);
|
|
571
|
+
if (!match) return null;
|
|
572
|
+
return <ExtErrorBoundary key={i}>{enrich.render(match, buildCtx(conversation))}</ExtErrorBoundary>;
|
|
573
|
+
})}
|
|
574
|
+
</div>
|
|
575
|
+
);
|
|
576
|
+
})}
|
|
516
577
|
</ScrollArea>
|
|
517
578
|
{/* Quick reply chips */}
|
|
518
579
|
<div style={{ display: 'flex', gap: 6, flexWrap: 'wrap', padding: '6px 14px 2px', borderTop: `1px solid ${c.line}` }}>
|
|
@@ -716,6 +777,12 @@ function MessagePanel({ conversation, provider, connection, client, primary, err
|
|
|
716
777
|
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
778
|
I
|
|
718
779
|
</button>
|
|
780
|
+
{extensions?.flatMap(e => e.toolbarActions ?? []).map((action, i) => (
|
|
781
|
+
<button key={i} type="button" title={action.tooltip} style={css.toolbarBtn}
|
|
782
|
+
onClick={() => { const insertText = (t: string) => setText(prev => prev + t); void action.onClick({ ...buildCtx(conversation), insertText }); }}>
|
|
783
|
+
{action.icon}
|
|
784
|
+
</button>
|
|
785
|
+
))}
|
|
719
786
|
</div>
|
|
720
787
|
|
|
721
788
|
{/* Textarea */}
|
|
@@ -873,12 +940,16 @@ function ContactInfoPanel({
|
|
|
873
940
|
primary,
|
|
874
941
|
onClose,
|
|
875
942
|
client,
|
|
943
|
+
onStartResize,
|
|
944
|
+
extensions,
|
|
876
945
|
}: {
|
|
877
946
|
conversation: Conversation;
|
|
878
947
|
connection?: Connection;
|
|
879
948
|
primary: string;
|
|
880
949
|
onClose: () => void;
|
|
881
950
|
client: ChannelHubClient;
|
|
951
|
+
onStartResize?: (e: React.MouseEvent) => void;
|
|
952
|
+
extensions?: Extension[];
|
|
882
953
|
}) {
|
|
883
954
|
const name = conversation.contactName ?? conversation.subject ?? conversation.providerConversationKey;
|
|
884
955
|
const provider = connection?.provider;
|
|
@@ -888,6 +959,7 @@ function ContactInfoPanel({
|
|
|
888
959
|
const [noteSaving, setNoteSaving] = useState(false);
|
|
889
960
|
const [noteSaved, setNoteSaved] = useState(false);
|
|
890
961
|
const [noteError, setNoteError] = useState('');
|
|
962
|
+
const [extRows, setExtRows] = useState<ContactInfoRow[]>([]);
|
|
891
963
|
|
|
892
964
|
useEffect(() => {
|
|
893
965
|
setNoteText('');
|
|
@@ -898,6 +970,20 @@ function ContactInfoPanel({
|
|
|
898
970
|
.catch(() => undefined);
|
|
899
971
|
}, [conversation.id, client]);
|
|
900
972
|
|
|
973
|
+
useEffect(() => {
|
|
974
|
+
if (!extensions?.some(e => e.contactInfoRows)) return;
|
|
975
|
+
const ctx = buildCtx(conversation);
|
|
976
|
+
setExtRows([]);
|
|
977
|
+
void Promise.allSettled(
|
|
978
|
+
extensions.filter(e => e.contactInfoRows).map(e => e.contactInfoRows!(ctx))
|
|
979
|
+
).then(results => {
|
|
980
|
+
const rows = results
|
|
981
|
+
.filter((r): r is PromiseFulfilledResult<ContactInfoRow[]> => r.status === 'fulfilled')
|
|
982
|
+
.flatMap(r => r.value);
|
|
983
|
+
setExtRows(rows);
|
|
984
|
+
});
|
|
985
|
+
}, [conversation.id, extensions]);
|
|
986
|
+
|
|
901
987
|
async function saveNote() {
|
|
902
988
|
if (!noteText.trim() || noteSaving) return;
|
|
903
989
|
setNoteSaving(true);
|
|
@@ -914,7 +1000,13 @@ function ContactInfoPanel({
|
|
|
914
1000
|
}
|
|
915
1001
|
|
|
916
1002
|
return (
|
|
917
|
-
<aside style={css.infoPanel}>
|
|
1003
|
+
<aside style={{ ...css.infoPanel, position: 'relative' }}>
|
|
1004
|
+
<div
|
|
1005
|
+
onMouseDown={onStartResize}
|
|
1006
|
+
style={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: 4, cursor: 'col-resize', zIndex: 1 }}
|
|
1007
|
+
onMouseEnter={e => { (e.currentTarget as HTMLElement).style.background = 'rgba(0,0,0,0.07)'; }}
|
|
1008
|
+
onMouseLeave={e => { (e.currentTarget as HTMLElement).style.background = ''; }}
|
|
1009
|
+
/>
|
|
918
1010
|
<div style={css.infoPanelHeader}>
|
|
919
1011
|
<span style={{ fontWeight: 600, fontSize: 13, color: c.ink }}>Thông tin liên hệ</span>
|
|
920
1012
|
<button style={css.iconButton} onClick={onClose} aria-label="Đóng">
|
|
@@ -973,13 +1065,28 @@ function ContactInfoPanel({
|
|
|
973
1065
|
<InfoRow label="Điện thoại" value="— chưa có" valueMuted />
|
|
974
1066
|
<InfoRow label="Email" value="— chưa có" valueMuted />
|
|
975
1067
|
<InfoRow label="Nhãn" value="— chưa có" valueMuted />
|
|
1068
|
+
{extRows.map(row => (
|
|
1069
|
+
<InfoRow key={row.label} label={row.label}>
|
|
1070
|
+
{row.href
|
|
1071
|
+
? <a href={row.href} target="_blank" rel="noopener noreferrer" style={{ color: primary, fontSize: 12 }}>{row.value}</a>
|
|
1072
|
+
: <span style={{ fontSize: 12 }}>{row.value}</span>
|
|
1073
|
+
}
|
|
1074
|
+
</InfoRow>
|
|
1075
|
+
))}
|
|
976
1076
|
</InfoSection>
|
|
977
1077
|
|
|
978
1078
|
<div style={{ padding: '12px 16px', display: 'flex', flexDirection: 'column', gap: 8 }}>
|
|
979
1079
|
<button style={{ ...css.ghostButton, color: primary, borderColor: `${primary}33` }}>Xem lịch sử</button>
|
|
980
|
-
|
|
1080
|
+
{extensions?.flatMap(e => e.sidebarActions ?? []).map((action, i) => (
|
|
1081
|
+
<button key={i} style={{ ...css.ghostButton, color: c.ink, borderColor: c.line, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 6 }}
|
|
1082
|
+
onClick={() => { void action.onClick(buildCtx(conversation)); }}>
|
|
1083
|
+
{action.icon}
|
|
1084
|
+
{action.label}
|
|
1085
|
+
</button>
|
|
1086
|
+
))}
|
|
1087
|
+
<div style={{ border: '1.5px solid #000', borderRadius: 10, background: '#fff', overflow: 'hidden' }}>
|
|
981
1088
|
<div style={{ display: 'flex', alignItems: 'center', gap: 6, padding: '7px 10px', borderBottom: '1px solid #fde68a', fontSize: 11, fontWeight: 600, color: '#92400e' }}>
|
|
982
|
-
|
|
1089
|
+
Ghi chú nội bộ
|
|
983
1090
|
</div>
|
|
984
1091
|
<textarea
|
|
985
1092
|
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,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
|
};
|