@channelhub/widget 0.1.13 → 0.1.14
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/ChannelHubSettings.d.ts.map +1 -1
- package/dist/index.cjs +15 -15
- package/dist/index.js +1503 -1318
- package/package.json +1 -1
- package/src/ChannelHubInbox.tsx +26 -1
- package/src/ChannelHubSettings.tsx +231 -39
package/package.json
CHANGED
package/src/ChannelHubInbox.tsx
CHANGED
|
@@ -281,7 +281,32 @@ export const ChannelHubInbox = forwardRef<ChannelHubInboxHandle, WidgetProps>(fu
|
|
|
281
281
|
document.addEventListener('mouseup', onUp);
|
|
282
282
|
}
|
|
283
283
|
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]);
|
|
284
|
-
useEffect(() => {
|
|
284
|
+
useEffect(() => {
|
|
285
|
+
void client.current.listConnections().then(setConnections).catch(() => undefined);
|
|
286
|
+
void load();
|
|
287
|
+
const stop = client.current.connectSSE((type, data) => {
|
|
288
|
+
if (type === 'message.new' || type === 'conversation.updated') {
|
|
289
|
+
void load();
|
|
290
|
+
if (type === 'message.new') {
|
|
291
|
+
const evtData = typeof data === 'object' && data ? data as Record<string, unknown> : {};
|
|
292
|
+
const evtConvId = evtData['conversationId'] ? String(evtData['conversationId']) : '';
|
|
293
|
+
const evtMsgId = evtData['messageId'] ? String(evtData['messageId']) : '';
|
|
294
|
+
const evtDirection = evtData['direction'] ? String(evtData['direction']) : '';
|
|
295
|
+
if (evtDirection === 'inbound' && !disableNotificationSound && tenantSettingsRef.current.inbox.soundNotification) {
|
|
296
|
+
playNotificationSound();
|
|
297
|
+
}
|
|
298
|
+
if (evtConvId && evtConvId === selectedId.current && evtMsgId && appendMessageRef.current) {
|
|
299
|
+
const appendFn = appendMessageRef.current;
|
|
300
|
+
void client.current.getMessage(evtMsgId).then(msg => { appendFn(msg); }).catch(() => undefined);
|
|
301
|
+
}
|
|
302
|
+
onNewMessage?.(evtConvId || selectedId.current || '');
|
|
303
|
+
}
|
|
304
|
+
} else if (type === 'message.status_updated') {
|
|
305
|
+
setMsgRefreshKey(k => k + 1);
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
return stop;
|
|
309
|
+
}, [load, onNewMessage, disableNotificationSound]);
|
|
285
310
|
useEffect(() => { selectedId.current = selected?.id; }, [selected]);
|
|
286
311
|
useEffect(() => { if (selected?.unreadCount) void client.current.markRead(selected.id).then(() => load()).catch(() => undefined); }, [selected?.id]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
287
312
|
|
|
@@ -188,6 +188,16 @@ export function ChannelHubSettings({
|
|
|
188
188
|
// Quick reply chip input
|
|
189
189
|
const [newQuickReply, setNewQuickReply] = useState('');
|
|
190
190
|
|
|
191
|
+
// Webhook test state
|
|
192
|
+
const [webhookTestMsg, setWebhookTestMsg] = useState('Shop ơi mẫu này còn hàng không, giá bao nhiêu ạ?');
|
|
193
|
+
const [webhookTesting, setWebhookTesting] = useState(false);
|
|
194
|
+
const [webhookTestResult, setWebhookTestResult] = useState<{
|
|
195
|
+
ok: boolean;
|
|
196
|
+
status: number;
|
|
197
|
+
latencyMs: number;
|
|
198
|
+
response: Record<string, unknown>;
|
|
199
|
+
} | null>(null);
|
|
200
|
+
|
|
191
201
|
// Responsive container observation
|
|
192
202
|
const rootRef = useRef<HTMLDivElement>(null);
|
|
193
203
|
const [isCompact, setIsCompact] = useState<boolean>(() =>
|
|
@@ -1010,46 +1020,228 @@ export function ChannelHubSettings({
|
|
|
1010
1020
|
</div>
|
|
1011
1021
|
|
|
1012
1022
|
{settings.ai.provider === 'webhook' ? (
|
|
1013
|
-
<div style={{ display: '
|
|
1014
|
-
<div>
|
|
1015
|
-
<
|
|
1016
|
-
<
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1023
|
+
<div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
|
|
1024
|
+
<div style={{ display: 'grid', gridTemplateColumns: isCompact ? '1fr' : '2fr 1fr', gap: 10 }}>
|
|
1025
|
+
<div>
|
|
1026
|
+
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8, marginBottom: 5 }}>
|
|
1027
|
+
<label style={{ display: 'flex', alignItems: 'center', gap: 5, fontSize: 12, fontWeight: 600, color: c.ink }}>
|
|
1028
|
+
<Webhook size={13} color={c.muted} /> Webhook Endpoint URL
|
|
1029
|
+
</label>
|
|
1030
|
+
<button
|
|
1031
|
+
type="button"
|
|
1032
|
+
onClick={() => {
|
|
1033
|
+
const origin = typeof window !== 'undefined' ? window.location.origin : 'http://localhost:3000';
|
|
1034
|
+
updateAi('webhookUrl', `${origin}/api/mock-ai-webhook`);
|
|
1035
|
+
}}
|
|
1036
|
+
style={{
|
|
1037
|
+
border: 0,
|
|
1038
|
+
background: 'none',
|
|
1039
|
+
color: primary,
|
|
1040
|
+
fontSize: 11,
|
|
1041
|
+
fontWeight: 600,
|
|
1042
|
+
cursor: 'pointer',
|
|
1043
|
+
padding: 0,
|
|
1044
|
+
textDecoration: 'underline',
|
|
1045
|
+
}}
|
|
1046
|
+
>
|
|
1047
|
+
Dùng URL Mock Local để test
|
|
1048
|
+
</button>
|
|
1049
|
+
</div>
|
|
1050
|
+
<input
|
|
1051
|
+
type="url"
|
|
1052
|
+
value={settings.ai.webhookUrl ?? ''}
|
|
1053
|
+
onChange={e => updateAi('webhookUrl', e.target.value)}
|
|
1054
|
+
placeholder="https://n8n.yourshop.com/webhook/channelhub-ai"
|
|
1055
|
+
style={{
|
|
1056
|
+
width: '100%',
|
|
1057
|
+
boxSizing: 'border-box',
|
|
1058
|
+
padding: '8px 10px',
|
|
1059
|
+
borderRadius: 8,
|
|
1060
|
+
border: `1px solid ${c.line}`,
|
|
1061
|
+
fontSize: isCompact ? 16 : 12.5,
|
|
1062
|
+
fontFamily: 'inherit',
|
|
1063
|
+
}}
|
|
1064
|
+
/>
|
|
1065
|
+
</div>
|
|
1066
|
+
<div>
|
|
1067
|
+
<label style={{ display: 'block', fontSize: 12, fontWeight: 600, color: c.ink, marginBottom: 5 }}>
|
|
1068
|
+
Webhook Secret (Tùy chọn)
|
|
1069
|
+
</label>
|
|
1070
|
+
<input
|
|
1071
|
+
type="password"
|
|
1072
|
+
value={settings.ai.webhookSecret ?? ''}
|
|
1073
|
+
onChange={e => updateAi('webhookSecret', e.target.value)}
|
|
1074
|
+
placeholder="whsec_..."
|
|
1075
|
+
style={{
|
|
1076
|
+
width: '100%',
|
|
1077
|
+
boxSizing: 'border-box',
|
|
1078
|
+
padding: '8px 10px',
|
|
1079
|
+
borderRadius: 8,
|
|
1080
|
+
border: `1px solid ${c.line}`,
|
|
1081
|
+
fontSize: isCompact ? 16 : 12.5,
|
|
1082
|
+
fontFamily: 'inherit',
|
|
1083
|
+
}}
|
|
1084
|
+
/>
|
|
1085
|
+
</div>
|
|
1033
1086
|
</div>
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1087
|
+
|
|
1088
|
+
{/* Interactive Webhook Test Console */}
|
|
1089
|
+
<div
|
|
1090
|
+
style={{
|
|
1091
|
+
borderRadius: 10,
|
|
1092
|
+
border: `1px solid ${c.line}`,
|
|
1093
|
+
background: c.surface,
|
|
1094
|
+
padding: 12,
|
|
1095
|
+
}}
|
|
1096
|
+
>
|
|
1097
|
+
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8, marginBottom: 8, flexWrap: 'wrap' }}>
|
|
1098
|
+
<span style={{ fontSize: 12, fontWeight: 700, color: c.ink }}>
|
|
1099
|
+
🧪 Kiểm tra gọi Webhook & Phản hồi tự động
|
|
1100
|
+
</span>
|
|
1101
|
+
<span style={{ fontSize: 11, color: c.muted }}>
|
|
1102
|
+
Event: <code style={{ background: '#e2e8f0', padding: '1px 5px', borderRadius: 4 }}>ai.reply_requested</code>
|
|
1103
|
+
</span>
|
|
1104
|
+
</div>
|
|
1105
|
+
<div style={{ display: 'flex', gap: 8, flexDirection: isCompact ? 'column' : 'row' }}>
|
|
1106
|
+
<input
|
|
1107
|
+
type="text"
|
|
1108
|
+
value={webhookTestMsg}
|
|
1109
|
+
onChange={e => setWebhookTestMsg(e.target.value)}
|
|
1110
|
+
placeholder="Nhập tin nhắn giả lập của khách hàng..."
|
|
1111
|
+
style={{
|
|
1112
|
+
flex: 1,
|
|
1113
|
+
padding: '7px 10px',
|
|
1114
|
+
borderRadius: 8,
|
|
1115
|
+
border: `1px solid ${c.line}`,
|
|
1116
|
+
background: c.white,
|
|
1117
|
+
fontSize: isCompact ? 16 : 12,
|
|
1118
|
+
fontFamily: 'inherit',
|
|
1119
|
+
}}
|
|
1120
|
+
/>
|
|
1121
|
+
<button
|
|
1122
|
+
type="button"
|
|
1123
|
+
disabled={webhookTesting || !settings.ai.webhookUrl?.trim()}
|
|
1124
|
+
onClick={async () => {
|
|
1125
|
+
const targetUrl = settings.ai.webhookUrl?.trim();
|
|
1126
|
+
if (!targetUrl) return;
|
|
1127
|
+
setWebhookTesting(true);
|
|
1128
|
+
setWebhookTestResult(null);
|
|
1129
|
+
const start = performance.now();
|
|
1130
|
+
const samplePayload = {
|
|
1131
|
+
event: 'ai.reply_requested',
|
|
1132
|
+
conversation: { id: 'conv_test_001', provider: 'facebook', assignedAgent: null },
|
|
1133
|
+
contact: { name: 'Khách hàng Test', providerUserId: 'psid_998877', externalId: 'CRM_01' },
|
|
1134
|
+
unansweredText: webhookTestMsg,
|
|
1135
|
+
recentMessages: [
|
|
1136
|
+
{ direction: 'inbound', text: webhookTestMsg, occurredAt: new Date().toISOString() },
|
|
1137
|
+
],
|
|
1138
|
+
aiConfig: {
|
|
1139
|
+
tone: settings.ai.tone,
|
|
1140
|
+
handoffOnPhone: settings.ai.handoffOnPhone,
|
|
1141
|
+
handoffOnComplaint: settings.ai.handoffOnComplaint,
|
|
1142
|
+
parameters: settings.ai.parameters,
|
|
1143
|
+
},
|
|
1144
|
+
};
|
|
1145
|
+
try {
|
|
1146
|
+
let res: Response;
|
|
1147
|
+
// Use local proxy if available to avoid CORS and compute HMAC signature
|
|
1148
|
+
if (typeof window !== 'undefined' && window.location.origin) {
|
|
1149
|
+
res = await fetch('/api/mock-ai-webhook', {
|
|
1150
|
+
method: 'POST',
|
|
1151
|
+
headers: { 'content-type': 'application/json' },
|
|
1152
|
+
body: JSON.stringify({
|
|
1153
|
+
...samplePayload,
|
|
1154
|
+
_proxyWebhookUrl: targetUrl,
|
|
1155
|
+
_proxyWebhookSecret: settings.ai.webhookSecret ?? '',
|
|
1156
|
+
}),
|
|
1157
|
+
});
|
|
1158
|
+
} else {
|
|
1159
|
+
res = await fetch(targetUrl, {
|
|
1160
|
+
method: 'POST',
|
|
1161
|
+
headers: { 'content-type': 'application/json', 'x-channelhub-event': 'ai.reply_requested' },
|
|
1162
|
+
body: JSON.stringify(samplePayload),
|
|
1163
|
+
});
|
|
1164
|
+
}
|
|
1165
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
1166
|
+
const data = (await res.json().catch(() => ({}))) as Record<string, unknown>;
|
|
1167
|
+
setWebhookTestResult({
|
|
1168
|
+
ok: res.ok,
|
|
1169
|
+
status: res.status,
|
|
1170
|
+
latencyMs,
|
|
1171
|
+
response: data,
|
|
1172
|
+
});
|
|
1173
|
+
} catch (err) {
|
|
1174
|
+
setWebhookTestResult({
|
|
1175
|
+
ok: false,
|
|
1176
|
+
status: 0,
|
|
1177
|
+
latencyMs: Math.round(performance.now() - start),
|
|
1178
|
+
response: { error: err instanceof Error ? err.message : 'Lỗi kết nối Webhook' },
|
|
1179
|
+
});
|
|
1180
|
+
} finally {
|
|
1181
|
+
setWebhookTesting(false);
|
|
1182
|
+
}
|
|
1183
|
+
}}
|
|
1184
|
+
style={{
|
|
1185
|
+
padding: '7px 14px',
|
|
1186
|
+
borderRadius: 8,
|
|
1187
|
+
border: 0,
|
|
1188
|
+
background: !settings.ai.webhookUrl?.trim() ? '#cbd5e1' : primary,
|
|
1189
|
+
color: '#fff',
|
|
1190
|
+
fontSize: 12,
|
|
1191
|
+
fontWeight: 600,
|
|
1192
|
+
cursor: !settings.ai.webhookUrl?.trim() ? 'not-allowed' : 'pointer',
|
|
1193
|
+
whiteSpace: 'nowrap',
|
|
1194
|
+
}}
|
|
1195
|
+
>
|
|
1196
|
+
{webhookTesting ? 'Đang gọi Webhook…' : '🚀 Gửi test tới Webhook'}
|
|
1197
|
+
</button>
|
|
1198
|
+
</div>
|
|
1199
|
+
|
|
1200
|
+
{webhookTestResult && (
|
|
1201
|
+
<div
|
|
1202
|
+
style={{
|
|
1203
|
+
marginTop: 10,
|
|
1204
|
+
padding: '10px 12px',
|
|
1205
|
+
borderRadius: 8,
|
|
1206
|
+
background: webhookTestResult.ok ? '#f0fdf4' : '#fef2f2',
|
|
1207
|
+
border: `1px solid ${webhookTestResult.ok ? '#bbf7d0' : '#fecaca'}`,
|
|
1208
|
+
fontSize: 12,
|
|
1209
|
+
}}
|
|
1210
|
+
>
|
|
1211
|
+
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 6 }}>
|
|
1212
|
+
<strong style={{ color: webhookTestResult.ok ? '#15803d' : '#b91c1c' }}>
|
|
1213
|
+
{webhookTestResult.ok ? `✓ Webhook phản hồi thành công (HTTP ${webhookTestResult.status})` : `✕ Lỗi gọi Webhook (HTTP ${webhookTestResult.status})`}
|
|
1214
|
+
</strong>
|
|
1215
|
+
<span style={{ fontSize: 11, color: c.muted }}>{webhookTestResult.latencyMs} ms</span>
|
|
1216
|
+
</div>
|
|
1217
|
+
{Boolean(webhookTestResult.response['text'] ?? webhookTestResult.response['replyText']) && (
|
|
1218
|
+
<div style={{ marginBottom: 4, color: c.ink }}>
|
|
1219
|
+
<strong>Tin nhắn sẽ tự động rep khách:</strong>{' '}
|
|
1220
|
+
<span style={{ background: '#fff', padding: '2px 6px', borderRadius: 4, border: `1px solid ${c.line}` }}>
|
|
1221
|
+
{String(webhookTestResult.response['text'] ?? webhookTestResult.response['replyText'])}
|
|
1222
|
+
</span>
|
|
1223
|
+
</div>
|
|
1224
|
+
)}
|
|
1225
|
+
{Boolean(webhookTestResult.response['internalNote']) && (
|
|
1226
|
+
<div style={{ marginBottom: 4, color: c.muted, fontSize: 11.5 }}>
|
|
1227
|
+
<strong>Ghi chú nội bộ tự động lưu:</strong> {String(webhookTestResult.response['internalNote'])}
|
|
1228
|
+
</div>
|
|
1229
|
+
)}
|
|
1230
|
+
<pre
|
|
1231
|
+
style={{
|
|
1232
|
+
margin: '6px 0 0',
|
|
1233
|
+
padding: 8,
|
|
1234
|
+
borderRadius: 6,
|
|
1235
|
+
background: '#0f172a',
|
|
1236
|
+
color: '#e2e8f0',
|
|
1237
|
+
fontSize: 11,
|
|
1238
|
+
overflowX: 'auto',
|
|
1239
|
+
}}
|
|
1240
|
+
>
|
|
1241
|
+
{JSON.stringify(webhookTestResult.response, null, 2)}
|
|
1242
|
+
</pre>
|
|
1243
|
+
</div>
|
|
1244
|
+
)}
|
|
1053
1245
|
</div>
|
|
1054
1246
|
</div>
|
|
1055
1247
|
) : (
|