@channelhub/widget 0.1.5 → 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/index.cjs +26 -25
- package/dist/index.js +911 -807
- package/dist/types.d.ts +13 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/ChannelHubInbox.tsx +173 -28
- package/src/types.ts +13 -1
package/dist/types.d.ts
CHANGED
|
@@ -64,15 +64,27 @@ 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 {
|
|
@@ -84,7 +96,7 @@ export interface ToolbarAction {
|
|
|
84
96
|
}
|
|
85
97
|
export interface MessageEnrichment {
|
|
86
98
|
pattern: RegExp;
|
|
87
|
-
render: (match: RegExpMatchArray, ctx: ExtensionContext) => React.ReactNode;
|
|
99
|
+
render: (match: RegExpMatchArray, ctx: ExtensionContext, message: Message) => React.ReactNode;
|
|
88
100
|
}
|
|
89
101
|
export interface Extension {
|
|
90
102
|
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,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,SAAS,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gB,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,13 +599,25 @@ 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;
|
|
566
614
|
return (
|
|
567
615
|
<div key={m.id}>
|
|
568
616
|
<MessageBubble message={m} primary={primary} />
|
|
569
|
-
{shouldEnrich && enrichments.map((enrich, i) => {
|
|
617
|
+
{shouldEnrich && panelCtx && enrichments.map((enrich, i) => {
|
|
570
618
|
const match = m.text?.match(enrich.pattern);
|
|
571
619
|
if (!match) return null;
|
|
572
|
-
return <ExtErrorBoundary key={i}>{enrich.render(match,
|
|
620
|
+
return <ExtErrorBoundary key={`${m.id}-${i}`}>{enrich.render(match, panelCtx, m)}</ExtErrorBoundary>;
|
|
573
621
|
})}
|
|
574
622
|
</div>
|
|
575
623
|
);
|
|
@@ -779,7 +827,23 @@ function MessagePanel({ conversation, provider, connection, client, primary, err
|
|
|
779
827
|
</button>
|
|
780
828
|
{extensions?.flatMap(e => e.toolbarActions ?? []).map((action, i) => (
|
|
781
829
|
<button key={i} type="button" title={action.tooltip} style={css.toolbarBtn}
|
|
782
|
-
onClick={() => {
|
|
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
|
+
}}>
|
|
783
847
|
{action.icon}
|
|
784
848
|
</button>
|
|
785
849
|
))}
|
|
@@ -942,6 +1006,9 @@ function ContactInfoPanel({
|
|
|
942
1006
|
client,
|
|
943
1007
|
onStartResize,
|
|
944
1008
|
extensions,
|
|
1009
|
+
extRefreshKey,
|
|
1010
|
+
onLinkContact,
|
|
1011
|
+
onRefreshContactInfo,
|
|
945
1012
|
}: {
|
|
946
1013
|
conversation: Conversation;
|
|
947
1014
|
connection?: Connection;
|
|
@@ -950,16 +1017,21 @@ function ContactInfoPanel({
|
|
|
950
1017
|
client: ChannelHubClient;
|
|
951
1018
|
onStartResize?: (e: React.MouseEvent) => void;
|
|
952
1019
|
extensions?: Extension[];
|
|
1020
|
+
extRefreshKey?: number;
|
|
1021
|
+
onLinkContact: (externalId: string | null) => Promise<void>;
|
|
1022
|
+
onRefreshContactInfo: () => void;
|
|
953
1023
|
}) {
|
|
954
1024
|
const name = conversation.contactName ?? conversation.subject ?? conversation.providerConversationKey;
|
|
955
1025
|
const provider = connection?.provider;
|
|
956
|
-
const
|
|
1026
|
+
const hasExtContactRows = Boolean(extensions?.some(e => e.contactInfoRows));
|
|
1027
|
+
const [expanded, setExpanded] = useState<Record<string, boolean>>({ channel: true, stats: false, extra: true });
|
|
957
1028
|
const toggle = (key: string) => setExpanded(e => ({ ...e, [key]: !e[key] }));
|
|
958
1029
|
const [noteText, setNoteText] = useState('');
|
|
959
1030
|
const [noteSaving, setNoteSaving] = useState(false);
|
|
960
1031
|
const [noteSaved, setNoteSaved] = useState(false);
|
|
961
1032
|
const [noteError, setNoteError] = useState('');
|
|
962
1033
|
const [extRows, setExtRows] = useState<ContactInfoRow[]>([]);
|
|
1034
|
+
const [extLoading, setExtLoading] = useState(false);
|
|
963
1035
|
|
|
964
1036
|
useEffect(() => {
|
|
965
1037
|
setNoteText('');
|
|
@@ -971,18 +1043,29 @@ function ContactInfoPanel({
|
|
|
971
1043
|
}, [conversation.id, client]);
|
|
972
1044
|
|
|
973
1045
|
useEffect(() => {
|
|
974
|
-
if (!
|
|
975
|
-
|
|
976
|
-
|
|
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);
|
|
977
1057
|
void Promise.allSettled(
|
|
978
1058
|
extensions.filter(e => e.contactInfoRows).map(e => e.contactInfoRows!(ctx))
|
|
979
1059
|
).then(results => {
|
|
1060
|
+
if (cancelled) return;
|
|
980
1061
|
const rows = results
|
|
981
1062
|
.filter((r): r is PromiseFulfilledResult<ContactInfoRow[]> => r.status === 'fulfilled')
|
|
982
1063
|
.flatMap(r => r.value);
|
|
983
1064
|
setExtRows(rows);
|
|
1065
|
+
setExtLoading(false);
|
|
984
1066
|
});
|
|
985
|
-
|
|
1067
|
+
return () => { cancelled = true; };
|
|
1068
|
+
}, [conversation.id, conversation.contactExternalId, extensions, extRefreshKey, hasExtContactRows, provider, onLinkContact, onRefreshContactInfo, client]);
|
|
986
1069
|
|
|
987
1070
|
async function saveNote() {
|
|
988
1071
|
if (!noteText.trim() || noteSaving) return;
|
|
@@ -1062,28 +1145,90 @@ function ContactInfoPanel({
|
|
|
1062
1145
|
</InfoSection>
|
|
1063
1146
|
|
|
1064
1147
|
<InfoSection label="THÔNG TIN BỔ SUNG" sectionKey="extra" expanded={expanded['extra']} onToggle={() => toggle('extra')}>
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
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
|
+
)}
|
|
1068
1158
|
{extRows.map(row => (
|
|
1069
1159
|
<InfoRow key={row.label} label={row.label}>
|
|
1070
|
-
{
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
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>
|
|
1074
1191
|
</InfoRow>
|
|
1075
1192
|
))}
|
|
1076
1193
|
</InfoSection>
|
|
1077
1194
|
|
|
1078
1195
|
<div style={{ padding: '12px 16px', display: 'flex', flexDirection: 'column', gap: 8 }}>
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
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
|
+
})}
|
|
1087
1232
|
<div style={{ border: '1.5px solid #000', borderRadius: 10, background: '#fff', overflow: 'hidden' }}>
|
|
1088
1233
|
<div style={{ display: 'flex', alignItems: 'center', gap: 6, padding: '7px 10px', borderBottom: '1px solid #fde68a', fontSize: 11, fontWeight: 600, color: '#92400e' }}>
|
|
1089
1234
|
Ghi chú nội bộ
|
package/src/types.ts
CHANGED
|
@@ -68,17 +68,29 @@ 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
|
|
|
@@ -90,7 +102,7 @@ export interface ToolbarAction {
|
|
|
90
102
|
|
|
91
103
|
export interface MessageEnrichment {
|
|
92
104
|
pattern: RegExp;
|
|
93
|
-
render: (match: RegExpMatchArray, ctx: ExtensionContext) => React.ReactNode;
|
|
105
|
+
render: (match: RegExpMatchArray, ctx: ExtensionContext, message: Message) => React.ReactNode;
|
|
94
106
|
}
|
|
95
107
|
|
|
96
108
|
export interface Extension {
|