@channelhub/widget 0.1.5 → 0.1.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/ChannelHubInbox.d.ts.map +1 -1
- package/dist/index.cjs +26 -25
- package/dist/index.js +958 -833
- package/dist/types.d.ts +14 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/ChannelHubInbox.tsx +222 -42
- package/src/types.ts +14 -1
package/dist/types.d.ts
CHANGED
|
@@ -64,19 +64,32 @@ export interface ExtensionContact {
|
|
|
64
64
|
export interface ExtensionContext {
|
|
65
65
|
conversation: Conversation;
|
|
66
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>;
|
|
67
72
|
}
|
|
68
73
|
export interface ContactInfoRow {
|
|
69
74
|
label: string;
|
|
70
75
|
value: string;
|
|
71
76
|
href?: string;
|
|
77
|
+
onClick?: () => void;
|
|
78
|
+
badge?: {
|
|
79
|
+
text: string;
|
|
80
|
+
color?: string;
|
|
81
|
+
bg?: string;
|
|
82
|
+
};
|
|
72
83
|
}
|
|
73
84
|
export interface SidebarAction {
|
|
74
85
|
label: string;
|
|
75
86
|
icon?: React.ReactNode;
|
|
87
|
+
variant?: 'primary' | 'default';
|
|
76
88
|
onClick: (ctx: ExtensionContext) => void | Promise<void>;
|
|
77
89
|
}
|
|
78
90
|
export interface ToolbarAction {
|
|
79
91
|
icon: React.ReactNode;
|
|
92
|
+
label?: string;
|
|
80
93
|
tooltip: string;
|
|
81
94
|
onClick: (ctx: ExtensionContext & {
|
|
82
95
|
insertText: (text: string) => void;
|
|
@@ -84,7 +97,7 @@ export interface ToolbarAction {
|
|
|
84
97
|
}
|
|
85
98
|
export interface MessageEnrichment {
|
|
86
99
|
pattern: RegExp;
|
|
87
|
-
render: (match: RegExpMatchArray, ctx: ExtensionContext) => React.ReactNode;
|
|
100
|
+
render: (match: RegExpMatchArray, ctx: ExtensionContext, message: Message) => React.ReactNode;
|
|
88
101
|
}
|
|
89
102
|
export interface Extension {
|
|
90
103
|
contactInfoRows?: (ctx: ExtensionContext) => Promise<ContactInfoRow[]>;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,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,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,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,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GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACnG;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,CAAC,KAAK,EAAE,gBAAgB,EAAE,GAAG,EAAE,gBAAgB,EAAE,OAAO,EAAE,OAAO,KAAK,KAAK,CAAC,SAAS,CAAC;CAC/F;AAED,MAAM,WAAW,SAAS;IACxB,eAAe,CAAC,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;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
|
@@ -18,8 +18,25 @@ function buildContact(conv: Conversation): ExtensionContact {
|
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
function buildCtx(
|
|
22
|
-
|
|
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
|
+
};
|
|
23
40
|
}
|
|
24
41
|
|
|
25
42
|
class ExtErrorBoundary extends Component<{ children: ReactNode }, { caught: boolean }> {
|
|
@@ -119,13 +136,27 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
119
136
|
const filterPopoverRef = useRef<HTMLDivElement>(null);
|
|
120
137
|
const [popoverAnchor, setPopoverAnchor] = useState<{ top: number; left: number } | null>(null);
|
|
121
138
|
const [loading, setLoading] = useState(true); const [error, setError] = useState('');
|
|
122
|
-
const [infoOpen, setInfoOpen] = useState(
|
|
123
|
-
const [infoPanelWidth, setInfoPanelWidth] = useState(
|
|
139
|
+
const [infoOpen, setInfoOpen] = useState(() => Boolean(extensions?.length));
|
|
140
|
+
const [infoPanelWidth, setInfoPanelWidth] = useState(280);
|
|
124
141
|
const [isResizing, setIsResizing] = useState(false);
|
|
142
|
+
const [extRefreshKey, setExtRefreshKey] = useState(0);
|
|
125
143
|
const primary = theme?.primaryColor ?? '#2563eb';
|
|
126
144
|
const connectionById = useMemo(() => new Map(connections.map(connection => [connection.id, connection])), [connections]);
|
|
127
145
|
const appendMessageRef = useRef<((msg: Message) => void) | null>(null);
|
|
128
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
|
+
}, []);
|
|
129
160
|
const availableProviders = useMemo(() => {
|
|
130
161
|
const ps = new Set(connections.map(cn => cn.provider));
|
|
131
162
|
return (['facebook', 'instagram', 'telegram', 'zalo', 'livechat'] as const).filter(p => ps.has(p));
|
|
@@ -318,6 +349,8 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
318
349
|
disableAttachments={disableAttachments}
|
|
319
350
|
hideKeyboardHint={hideKeyboardHint}
|
|
320
351
|
extensions={extensions}
|
|
352
|
+
onLinkContact={(extId) => handleLinkContact(selected, extId)}
|
|
353
|
+
onRefreshContactInfo={handleRefreshContactInfo}
|
|
321
354
|
/>
|
|
322
355
|
) : <EmptyConversation />}
|
|
323
356
|
</main>
|
|
@@ -330,6 +363,9 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
330
363
|
client={client.current}
|
|
331
364
|
onStartResize={startInfoResize}
|
|
332
365
|
extensions={extensions}
|
|
366
|
+
extRefreshKey={extRefreshKey}
|
|
367
|
+
onLinkContact={(extId) => handleLinkContact(selected, extId)}
|
|
368
|
+
onRefreshContactInfo={handleRefreshContactInfo}
|
|
333
369
|
/>
|
|
334
370
|
)}
|
|
335
371
|
</div>
|
|
@@ -390,7 +426,7 @@ function ConversationRow({ conversation, provider, selected, primary, onSelect }
|
|
|
390
426
|
|
|
391
427
|
type PendingAttachment = { file: File; uploading: boolean; url?: string; mimeType?: string; attachmentType?: string; objectUrl?: string };
|
|
392
428
|
|
|
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[] }) {
|
|
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 }) {
|
|
394
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;
|
|
395
431
|
const fileImageRef = useRef<HTMLInputElement>(null);
|
|
396
432
|
const fileDocRef = useRef<HTMLInputElement>(null);
|
|
@@ -563,14 +599,35 @@ function MessagePanel({ conversation, provider, connection, client, primary, err
|
|
|
563
599
|
{messages.map((m, idx) => {
|
|
564
600
|
const enrichments = extensions?.flatMap(e => e.messageEnrichments ?? []) ?? [];
|
|
565
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
|
+
const matchedEnrichments = shouldEnrich && panelCtx
|
|
615
|
+
? enrichments
|
|
616
|
+
.map((enrich, i) => {
|
|
617
|
+
const match = m.text?.match(enrich.pattern);
|
|
618
|
+
if (!match) return null;
|
|
619
|
+
return <ExtErrorBoundary key={`${m.id}-${i}`}>{enrich.render(match, panelCtx, m)}</ExtErrorBoundary>;
|
|
620
|
+
})
|
|
621
|
+
.filter(Boolean)
|
|
622
|
+
: [];
|
|
566
623
|
return (
|
|
567
624
|
<div key={m.id}>
|
|
568
625
|
<MessageBubble message={m} primary={primary} />
|
|
569
|
-
{
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
626
|
+
{matchedEnrichments.length > 0 && (
|
|
627
|
+
<div style={{ maxWidth: 'min(70%, 480px)', marginTop: -4, marginBottom: 10, display: 'flex', flexDirection: 'column', gap: 6 }}>
|
|
628
|
+
{matchedEnrichments}
|
|
629
|
+
</div>
|
|
630
|
+
)}
|
|
574
631
|
</div>
|
|
575
632
|
);
|
|
576
633
|
})}
|
|
@@ -777,10 +834,52 @@ function MessagePanel({ conversation, provider, connection, client, primary, err
|
|
|
777
834
|
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' }}>
|
|
778
835
|
I
|
|
779
836
|
</button>
|
|
837
|
+
{Boolean(extensions?.some(e => (e.toolbarActions?.length ?? 0) > 0)) && (
|
|
838
|
+
<span style={{ width: 1, height: 14, background: '#e5e7eb', margin: '0 4px' }} />
|
|
839
|
+
)}
|
|
780
840
|
{extensions?.flatMap(e => e.toolbarActions ?? []).map((action, i) => (
|
|
781
|
-
<button
|
|
782
|
-
|
|
841
|
+
<button
|
|
842
|
+
key={i}
|
|
843
|
+
type="button"
|
|
844
|
+
title={action.tooltip}
|
|
845
|
+
style={
|
|
846
|
+
action.label
|
|
847
|
+
? {
|
|
848
|
+
display: 'inline-flex',
|
|
849
|
+
alignItems: 'center',
|
|
850
|
+
gap: 5,
|
|
851
|
+
padding: '3px 9px',
|
|
852
|
+
borderRadius: 999,
|
|
853
|
+
border: `1px solid ${primary}33`,
|
|
854
|
+
background: `${primary}0f`,
|
|
855
|
+
color: primary,
|
|
856
|
+
fontSize: 11.5,
|
|
857
|
+
fontWeight: 600,
|
|
858
|
+
cursor: 'pointer',
|
|
859
|
+
lineHeight: 1.2,
|
|
860
|
+
whiteSpace: 'nowrap',
|
|
861
|
+
}
|
|
862
|
+
: css.toolbarBtn
|
|
863
|
+
}
|
|
864
|
+
onClick={() => {
|
|
865
|
+
const insertText = (snippet: string) => {
|
|
866
|
+
setText(prev => (prev && !prev.endsWith(' ') && !prev.endsWith('\n') ? `${prev} ${snippet}` : `${prev}${snippet}`));
|
|
867
|
+
requestAnimationFrame(() => textareaRef.current?.focus());
|
|
868
|
+
};
|
|
869
|
+
const ctx = buildCtx(conversation, {
|
|
870
|
+
provider,
|
|
871
|
+
recentMessages: messages.slice(-10),
|
|
872
|
+
linkContact: onLinkContact,
|
|
873
|
+
refreshContactInfo: onRefreshContactInfo,
|
|
874
|
+
sendMessage: async (msgText, msgAttachments) => {
|
|
875
|
+
await client.sendMessage(conversation.id, msgText, msgAttachments);
|
|
876
|
+
await load(undefined, true);
|
|
877
|
+
},
|
|
878
|
+
});
|
|
879
|
+
void action.onClick({ ...ctx, insertText });
|
|
880
|
+
}}>
|
|
783
881
|
{action.icon}
|
|
882
|
+
{action.label && <span>{action.label}</span>}
|
|
784
883
|
</button>
|
|
785
884
|
))}
|
|
786
885
|
</div>
|
|
@@ -942,6 +1041,9 @@ function ContactInfoPanel({
|
|
|
942
1041
|
client,
|
|
943
1042
|
onStartResize,
|
|
944
1043
|
extensions,
|
|
1044
|
+
extRefreshKey,
|
|
1045
|
+
onLinkContact,
|
|
1046
|
+
onRefreshContactInfo,
|
|
945
1047
|
}: {
|
|
946
1048
|
conversation: Conversation;
|
|
947
1049
|
connection?: Connection;
|
|
@@ -950,16 +1052,21 @@ function ContactInfoPanel({
|
|
|
950
1052
|
client: ChannelHubClient;
|
|
951
1053
|
onStartResize?: (e: React.MouseEvent) => void;
|
|
952
1054
|
extensions?: Extension[];
|
|
1055
|
+
extRefreshKey?: number;
|
|
1056
|
+
onLinkContact: (externalId: string | null) => Promise<void>;
|
|
1057
|
+
onRefreshContactInfo: () => void;
|
|
953
1058
|
}) {
|
|
954
1059
|
const name = conversation.contactName ?? conversation.subject ?? conversation.providerConversationKey;
|
|
955
1060
|
const provider = connection?.provider;
|
|
956
|
-
const
|
|
1061
|
+
const hasExtContactRows = Boolean(extensions?.some(e => e.contactInfoRows));
|
|
1062
|
+
const [expanded, setExpanded] = useState<Record<string, boolean>>({ channel: true, stats: false, extra: true });
|
|
957
1063
|
const toggle = (key: string) => setExpanded(e => ({ ...e, [key]: !e[key] }));
|
|
958
1064
|
const [noteText, setNoteText] = useState('');
|
|
959
1065
|
const [noteSaving, setNoteSaving] = useState(false);
|
|
960
1066
|
const [noteSaved, setNoteSaved] = useState(false);
|
|
961
1067
|
const [noteError, setNoteError] = useState('');
|
|
962
1068
|
const [extRows, setExtRows] = useState<ContactInfoRow[]>([]);
|
|
1069
|
+
const [extLoading, setExtLoading] = useState(false);
|
|
963
1070
|
|
|
964
1071
|
useEffect(() => {
|
|
965
1072
|
setNoteText('');
|
|
@@ -971,18 +1078,29 @@ function ContactInfoPanel({
|
|
|
971
1078
|
}, [conversation.id, client]);
|
|
972
1079
|
|
|
973
1080
|
useEffect(() => {
|
|
974
|
-
if (!
|
|
975
|
-
|
|
976
|
-
|
|
1081
|
+
if (!hasExtContactRows || !extensions) return;
|
|
1082
|
+
let cancelled = false;
|
|
1083
|
+
const ctx = buildCtx(conversation, {
|
|
1084
|
+
provider,
|
|
1085
|
+
linkContact: onLinkContact,
|
|
1086
|
+
refreshContactInfo: onRefreshContactInfo,
|
|
1087
|
+
sendMessage: async (msgText, msgAttachments) => {
|
|
1088
|
+
await client.sendMessage(conversation.id, msgText, msgAttachments);
|
|
1089
|
+
},
|
|
1090
|
+
});
|
|
1091
|
+
setExtLoading(true);
|
|
977
1092
|
void Promise.allSettled(
|
|
978
1093
|
extensions.filter(e => e.contactInfoRows).map(e => e.contactInfoRows!(ctx))
|
|
979
1094
|
).then(results => {
|
|
1095
|
+
if (cancelled) return;
|
|
980
1096
|
const rows = results
|
|
981
1097
|
.filter((r): r is PromiseFulfilledResult<ContactInfoRow[]> => r.status === 'fulfilled')
|
|
982
1098
|
.flatMap(r => r.value);
|
|
983
1099
|
setExtRows(rows);
|
|
1100
|
+
setExtLoading(false);
|
|
984
1101
|
});
|
|
985
|
-
|
|
1102
|
+
return () => { cancelled = true; };
|
|
1103
|
+
}, [conversation.id, conversation.contactExternalId, extensions, extRefreshKey, hasExtContactRows, provider, onLinkContact, onRefreshContactInfo, client]);
|
|
986
1104
|
|
|
987
1105
|
async function saveNote() {
|
|
988
1106
|
if (!noteText.trim() || noteSaving) return;
|
|
@@ -1062,30 +1180,92 @@ function ContactInfoPanel({
|
|
|
1062
1180
|
</InfoSection>
|
|
1063
1181
|
|
|
1064
1182
|
<InfoSection label="THÔNG TIN BỔ SUNG" sectionKey="extra" expanded={expanded['extra']} onToggle={() => toggle('extra')}>
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1183
|
+
{!hasExtContactRows && (
|
|
1184
|
+
<>
|
|
1185
|
+
<InfoRow label="Điện thoại" value="— chưa có" valueMuted />
|
|
1186
|
+
<InfoRow label="Email" value="— chưa có" valueMuted />
|
|
1187
|
+
<InfoRow label="Nhãn" value="— chưa có" valueMuted />
|
|
1188
|
+
</>
|
|
1189
|
+
)}
|
|
1190
|
+
{extLoading && extRows.length === 0 && (
|
|
1191
|
+
<div style={{ fontSize: 12, color: c.muted, padding: '6px 0' }}>Đang tải hồ sơ CRM…</div>
|
|
1192
|
+
)}
|
|
1068
1193
|
{extRows.map(row => (
|
|
1069
1194
|
<InfoRow key={row.label} label={row.label}>
|
|
1070
|
-
{
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1195
|
+
<span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, flexWrap: 'wrap', justifyContent: 'flex-end' }}>
|
|
1196
|
+
{row.onClick ? (
|
|
1197
|
+
<button
|
|
1198
|
+
type="button"
|
|
1199
|
+
onClick={row.onClick}
|
|
1200
|
+
style={{ border: 0, background: 'none', padding: 0, color: primary, fontSize: 12, fontWeight: 600, cursor: 'pointer', textDecoration: 'underline' }}
|
|
1201
|
+
>
|
|
1202
|
+
{row.value}
|
|
1203
|
+
</button>
|
|
1204
|
+
) : row.href ? (
|
|
1205
|
+
<a href={row.href} target="_blank" rel="noopener noreferrer" style={{ color: primary, fontSize: 12, fontWeight: 600 }}>
|
|
1206
|
+
{row.value}
|
|
1207
|
+
</a>
|
|
1208
|
+
) : (
|
|
1209
|
+
<span style={{ fontSize: 12, fontWeight: 500, color: c.ink }}>{row.value}</span>
|
|
1210
|
+
)}
|
|
1211
|
+
{row.badge && (
|
|
1212
|
+
<span
|
|
1213
|
+
style={{
|
|
1214
|
+
fontSize: 10.5,
|
|
1215
|
+
fontWeight: 700,
|
|
1216
|
+
padding: '1px 7px',
|
|
1217
|
+
borderRadius: 999,
|
|
1218
|
+
color: row.badge.color ?? primary,
|
|
1219
|
+
background: row.badge.bg ?? `${primary}14`,
|
|
1220
|
+
}}
|
|
1221
|
+
>
|
|
1222
|
+
{row.badge.text}
|
|
1223
|
+
</span>
|
|
1224
|
+
)}
|
|
1225
|
+
</span>
|
|
1074
1226
|
</InfoRow>
|
|
1075
1227
|
))}
|
|
1076
1228
|
</InfoSection>
|
|
1077
1229
|
|
|
1078
1230
|
<div style={{ padding: '12px 16px', display: 'flex', flexDirection: 'column', gap: 8 }}>
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1231
|
+
{!extensions?.length && (
|
|
1232
|
+
<button style={{ ...css.ghostButton, color: primary, borderColor: `${primary}33` }}>Xem lịch sử</button>
|
|
1233
|
+
)}
|
|
1234
|
+
{extensions?.flatMap(e => e.sidebarActions ?? []).map((action, i) => {
|
|
1235
|
+
const isPrimary = action.variant === 'primary';
|
|
1236
|
+
return (
|
|
1237
|
+
<button
|
|
1238
|
+
key={i}
|
|
1239
|
+
style={{
|
|
1240
|
+
...css.ghostButton,
|
|
1241
|
+
background: isPrimary ? primary : '#fff',
|
|
1242
|
+
color: isPrimary ? '#fff' : c.ink,
|
|
1243
|
+
borderColor: isPrimary ? primary : c.line,
|
|
1244
|
+
fontWeight: isPrimary ? 700 : 600,
|
|
1245
|
+
display: 'flex',
|
|
1246
|
+
alignItems: 'center',
|
|
1247
|
+
justifyContent: 'center',
|
|
1248
|
+
gap: 6,
|
|
1249
|
+
}}
|
|
1250
|
+
onClick={() => {
|
|
1251
|
+
const ctx = buildCtx(conversation, {
|
|
1252
|
+
provider,
|
|
1253
|
+
linkContact: onLinkContact,
|
|
1254
|
+
refreshContactInfo: onRefreshContactInfo,
|
|
1255
|
+
sendMessage: async (msgText, msgAttachments) => {
|
|
1256
|
+
await client.sendMessage(conversation.id, msgText, msgAttachments);
|
|
1257
|
+
},
|
|
1258
|
+
});
|
|
1259
|
+
void action.onClick(ctx);
|
|
1260
|
+
}}
|
|
1261
|
+
>
|
|
1262
|
+
{action.icon}
|
|
1263
|
+
{action.label}
|
|
1264
|
+
</button>
|
|
1265
|
+
);
|
|
1266
|
+
})}
|
|
1267
|
+
<div style={{ border: `1px solid ${c.line}`, borderRadius: 10, background: '#fff', overflow: 'hidden' }}>
|
|
1268
|
+
<div style={{ display: 'flex', alignItems: 'center', gap: 6, padding: '7px 10px', borderBottom: '1px solid #fde68a', background: '#fffbeb', fontSize: 11, fontWeight: 600, color: '#92400e' }}>
|
|
1089
1269
|
Ghi chú nội bộ
|
|
1090
1270
|
</div>
|
|
1091
1271
|
<textarea
|
|
@@ -1137,9 +1317,9 @@ function InfoRow({ label, value, children, valueColor, valueMuted }: {
|
|
|
1137
1317
|
label: string; value?: string; children?: React.ReactNode; valueColor?: string; valueMuted?: boolean;
|
|
1138
1318
|
}) {
|
|
1139
1319
|
return (
|
|
1140
|
-
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: '
|
|
1141
|
-
<span style={{ color: c.muted, flexShrink: 0 }}>{label}</span>
|
|
1142
|
-
<span style={{ color: valueColor ?? (valueMuted ? c.muted : c.ink), textAlign: 'right', minWidth: 0,
|
|
1320
|
+
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', padding: '5px 16px', fontSize: 12, gap: 10 }}>
|
|
1321
|
+
<span style={{ color: c.muted, flexShrink: 0, lineHeight: 1.4 }}>{label}</span>
|
|
1322
|
+
<span style={{ color: valueColor ?? (valueMuted ? c.muted : c.ink), textAlign: 'right', minWidth: 0, display: 'flex', alignItems: 'center', justifyContent: 'flex-end', flexWrap: 'wrap', gap: 4, lineHeight: 1.4 }}>
|
|
1143
1323
|
{children ?? value}
|
|
1144
1324
|
</span>
|
|
1145
1325
|
</div>
|
|
@@ -1196,13 +1376,13 @@ const css: Record<string, React.CSSProperties> = {
|
|
|
1196
1376
|
clear: { border: 0, background: 'none', color: c.muted, cursor: 'pointer', fontSize: 18 },
|
|
1197
1377
|
listHeader: { display: 'flex', alignItems: 'center', padding: '10px 18px', borderTop: `1px solid ${c.line}`, borderBottom: `1px solid ${c.line}`, color: c.muted, fontSize: 11, fontWeight: 700, letterSpacing: .4, textTransform: 'uppercase' },
|
|
1198
1378
|
conversationList: { overflowY: 'auto', flex: 1, minHeight: 0 },
|
|
1199
|
-
row: { display: 'flex', width: '100%', gap: 11, padding: '
|
|
1200
|
-
avatarWrap: { position: 'relative', flex: '0 0 auto', width:
|
|
1201
|
-
avatar: { flex: '0 0 auto', width:
|
|
1379
|
+
row: { display: 'flex', width: '100%', gap: 11, padding: '16px 14px', border: 0, borderBottom: `1px solid ${c.line}`, background: 'transparent', textAlign: 'left', cursor: 'pointer', transition: 'background 120ms ease' },
|
|
1380
|
+
avatarWrap: { position: 'relative', flex: '0 0 auto', width: 44, height: 44 },
|
|
1381
|
+
avatar: { flex: '0 0 auto', width: 44, height: 44, display: 'grid', placeItems: 'center', borderRadius: '50%', objectFit: 'cover', fontSize: 14, fontWeight: 800 },
|
|
1202
1382
|
providerBadge: { position: 'absolute', left: -3, bottom: -3, display: 'grid', placeItems: 'center', width: 17, height: 17, border: '2px solid #fff', borderRadius: '50%', background: '#fff', color: '#64748b' },
|
|
1203
1383
|
rowBody: { minWidth: 0, flex: 1 },
|
|
1204
|
-
rowTop: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 8, fontSize:
|
|
1205
|
-
rowBottom: { display: 'flex', marginTop: 4, fontSize:
|
|
1384
|
+
rowTop: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 8, fontSize: 14 },
|
|
1385
|
+
rowBottom: { display: 'flex', marginTop: 4, fontSize: 12, minWidth: 0, overflow: 'hidden', lineHeight: 1.4 },
|
|
1206
1386
|
channel: { color: '#9aa5b5' },
|
|
1207
1387
|
unreadBadge: { display: 'inline-grid', placeItems: 'center', minWidth: 18, height: 18, borderRadius: 999, background: '#ef4444', color: '#fff', fontSize: 10, fontWeight: 800, padding: '0 5px' },
|
|
1208
1388
|
loadMore: { width: '100%', border: 0, background: 'transparent', color: '#4c72ca', padding: 14, cursor: 'pointer', fontSize: 12 },
|
package/src/types.ts
CHANGED
|
@@ -68,29 +68,42 @@ export interface ExtensionContact {
|
|
|
68
68
|
export interface ExtensionContext {
|
|
69
69
|
conversation: Conversation;
|
|
70
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>;
|
|
71
76
|
}
|
|
72
77
|
|
|
73
78
|
export interface ContactInfoRow {
|
|
74
79
|
label: string;
|
|
75
80
|
value: string;
|
|
76
81
|
href?: string;
|
|
82
|
+
onClick?: () => void;
|
|
83
|
+
badge?: {
|
|
84
|
+
text: string;
|
|
85
|
+
color?: string;
|
|
86
|
+
bg?: string;
|
|
87
|
+
};
|
|
77
88
|
}
|
|
78
89
|
|
|
79
90
|
export interface SidebarAction {
|
|
80
91
|
label: string;
|
|
81
92
|
icon?: React.ReactNode;
|
|
93
|
+
variant?: 'primary' | 'default';
|
|
82
94
|
onClick: (ctx: ExtensionContext) => void | Promise<void>;
|
|
83
95
|
}
|
|
84
96
|
|
|
85
97
|
export interface ToolbarAction {
|
|
86
98
|
icon: React.ReactNode;
|
|
99
|
+
label?: string;
|
|
87
100
|
tooltip: string;
|
|
88
101
|
onClick: (ctx: ExtensionContext & { insertText: (text: string) => void }) => void | Promise<void>;
|
|
89
102
|
}
|
|
90
103
|
|
|
91
104
|
export interface MessageEnrichment {
|
|
92
105
|
pattern: RegExp;
|
|
93
|
-
render: (match: RegExpMatchArray, ctx: ExtensionContext) => React.ReactNode;
|
|
106
|
+
render: (match: RegExpMatchArray, ctx: ExtensionContext, message: Message) => React.ReactNode;
|
|
94
107
|
}
|
|
95
108
|
|
|
96
109
|
export interface Extension {
|