@channelhub/widget 0.1.10 → 0.1.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@channelhub/widget",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "description": "Embeddable chat inbox widget for ChannelHub",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -53,9 +53,28 @@ function displayName(conn: ChannelConnection): string {
53
53
  }
54
54
 
55
55
  function metaLine(conn: ChannelConnection): string {
56
- const label = PROVIDER_LABEL[conn.provider] ?? conn.provider;
57
- const name = String(conn.metadata['username'] ?? conn.metadata['pageName'] ?? '');
58
- return name ? `${label} · ${name}` : label;
56
+ const m = conn.metadata;
57
+ switch (conn.provider) {
58
+ case 'facebook':
59
+ return `Fanpage · ID ${conn.externalAccountId}`;
60
+ case 'instagram': {
61
+ const ig = String(m['username'] ?? conn.externalAccountId);
62
+ return `@${ig}`;
63
+ }
64
+ case 'zalo':
65
+ return `Zalo OA · ID ${conn.externalAccountId}`;
66
+ case 'telegram': {
67
+ const tg = String(m['username'] ?? '');
68
+ return tg ? `@${tg}` : `Bot · ${conn.externalAccountId}`;
69
+ }
70
+ case 'livechat':
71
+ return `Widget ID: ${conn.id}`;
72
+ default: {
73
+ const label = PROVIDER_LABEL[conn.provider] ?? conn.provider;
74
+ const name = String(m['username'] ?? m['pageName'] ?? '');
75
+ return name ? `${label} · ${name}` : label;
76
+ }
77
+ }
59
78
  }
60
79
 
61
80
  // ── Shared style tokens ────────────────────────────────────────────────────────
@@ -871,29 +890,92 @@ function ChannelAvatar({ conn, apiUrl, size = 40 }: { conn: ChannelConnection; a
871
890
 
872
891
  function ChannelItem({ conn, apiUrl, onDisconnect }: { conn: ChannelConnection; apiUrl: string; onDisconnect: () => void }) {
873
892
  const [hover, setHover] = useState(false);
893
+ const [showScript, setShowScript] = useState(false);
894
+ const [copied, setCopied] = useState(false);
895
+
896
+ const isLivechat = conn.provider === 'livechat';
897
+ const base = apiUrl.replace(/\/$/, '');
898
+ const embedScript = isLivechat
899
+ ? `<script src="${base}/static/livechat.js" data-widget-id="${conn.id}" defer></script>`
900
+ : null;
901
+
902
+ const copyScript = () => {
903
+ if (!embedScript) return;
904
+ void navigator.clipboard.writeText(embedScript).then(() => {
905
+ setCopied(true);
906
+ setTimeout(() => setCopied(false), 2000);
907
+ });
908
+ };
909
+
874
910
  return (
875
- <div style={s.channelItem}>
876
- <ChannelAvatar conn={conn} apiUrl={apiUrl} size={40} />
877
- <div style={s.channelInfo}>
878
- <div style={s.channelName}>{displayName(conn)}</div>
879
- <div style={s.channelMeta}>{metaLine(conn)}</div>
880
- </div>
881
- <div style={s.channelActions}>
882
- <span style={conn.status === 'active' ? s.badgeActive : s.badgeInactive}>
883
- {conn.status === 'active' ? 'Hoạt động' : conn.status}
884
- </span>
885
- <button
886
- style={{
887
- ...s.disconnectBtn,
888
- ...(hover ? { background: '#fef2f2', borderColor: '#fca5a5', color: '#dc2626' } : {}),
889
- }}
890
- onMouseEnter={() => setHover(true)}
891
- onMouseLeave={() => setHover(false)}
892
- onClick={onDisconnect}
893
- >
894
- Ngắt kết nối
895
- </button>
911
+ <div style={{ ...s.channelItem, flexDirection: 'column', alignItems: 'stretch', gap: 0 }}>
912
+ <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
913
+ <ChannelAvatar conn={conn} apiUrl={apiUrl} size={40} />
914
+ <div style={s.channelInfo}>
915
+ <div style={s.channelName}>{displayName(conn)}</div>
916
+ <div style={{ ...s.channelMeta, userSelect: 'text' as const }}>{metaLine(conn)}</div>
917
+ </div>
918
+ <div style={s.channelActions}>
919
+ <span style={conn.status === 'active' ? s.badgeActive : s.badgeInactive}>
920
+ {conn.status === 'active' ? 'Hoạt động' : conn.status}
921
+ </span>
922
+ {isLivechat && (
923
+ <button
924
+ style={{
925
+ ...s.disconnectBtn,
926
+ ...(showScript ? { background: '#ede9fe', borderColor: '#c4b5fd', color: '#7c3aed' } : {}),
927
+ }}
928
+ onClick={() => setShowScript(v => !v)}
929
+ >
930
+ Mã nhúng
931
+ </button>
932
+ )}
933
+ <button
934
+ style={{
935
+ ...s.disconnectBtn,
936
+ ...(hover ? { background: '#fef2f2', borderColor: '#fca5a5', color: '#dc2626' } : {}),
937
+ }}
938
+ onMouseEnter={() => setHover(true)}
939
+ onMouseLeave={() => setHover(false)}
940
+ onClick={onDisconnect}
941
+ >
942
+ Ngắt kết nối
943
+ </button>
944
+ </div>
896
945
  </div>
946
+
947
+ {isLivechat && showScript && embedScript && (
948
+ <div style={{ marginTop: 10, borderTop: '1px solid #f1f5f9', paddingTop: 10 }}>
949
+ <div style={{ fontSize: 11, fontWeight: 600, color: '#7c3aed', marginBottom: 6, textTransform: 'uppercase' as const, letterSpacing: '0.05em' }}>
950
+ Mã nhúng — dán vào trước thẻ &lt;/body&gt;
951
+ </div>
952
+ <div style={{ position: 'relative' as const }}>
953
+ <pre style={{
954
+ margin: 0, padding: '10px 12px', background: '#f8f7ff',
955
+ border: '1px solid #e9d5ff', borderRadius: 8,
956
+ fontSize: 12, color: '#3b0764', overflowX: 'auto' as const,
957
+ fontFamily: 'ui-monospace, SFMono-Regular, monospace',
958
+ lineHeight: 1.5, userSelect: 'text' as const,
959
+ }}>
960
+ {embedScript}
961
+ </pre>
962
+ <button
963
+ onClick={copyScript}
964
+ style={{
965
+ position: 'absolute' as const, top: 6, right: 6,
966
+ fontSize: 11, fontWeight: 600, padding: '3px 10px', borderRadius: 6,
967
+ border: '1px solid',
968
+ borderColor: copied ? '#059669' : '#c4b5fd',
969
+ background: copied ? '#d1fae5' : '#fff',
970
+ color: copied ? '#059669' : '#7c3aed',
971
+ cursor: 'pointer',
972
+ }}
973
+ >
974
+ {copied ? '✓ Đã sao chép' : 'Sao chép'}
975
+ </button>
976
+ </div>
977
+ </div>
978
+ )}
897
979
  </div>
898
980
  );
899
981
  }