@channelhub/widget 0.0.0-stage → 0.1.4

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.
Files changed (67) hide show
  1. package/dist/ChannelHubChannels.d.ts +15 -0
  2. package/dist/ChannelHubChannels.d.ts.map +1 -0
  3. package/dist/ChannelHubInbox.d.ts +3 -0
  4. package/dist/ChannelHubInbox.d.ts.map +1 -0
  5. package/dist/LiveChatWidget.d.ts +12 -0
  6. package/dist/LiveChatWidget.d.ts.map +1 -0
  7. package/dist/api.d.ts +36 -0
  8. package/dist/api.d.ts.map +1 -0
  9. package/dist/channels/ProviderLogo.d.ts +8 -0
  10. package/dist/channels/ProviderLogo.d.ts.map +1 -0
  11. package/dist/components/ui/avatar.d.ts +8 -0
  12. package/dist/components/ui/avatar.d.ts.map +1 -0
  13. package/dist/components/ui/button.d.ts +5 -0
  14. package/dist/components/ui/button.d.ts.map +1 -0
  15. package/dist/components/ui/index.d.ts +6 -0
  16. package/dist/components/ui/index.d.ts.map +1 -0
  17. package/dist/components/ui/input.d.ts +3 -0
  18. package/dist/components/ui/input.d.ts.map +1 -0
  19. package/dist/components/ui/scroll-area.d.ts +6 -0
  20. package/dist/components/ui/scroll-area.d.ts.map +1 -0
  21. package/dist/components/ui/textarea.d.ts +3 -0
  22. package/dist/components/ui/textarea.d.ts.map +1 -0
  23. package/dist/facebook_icon.avif +0 -0
  24. package/dist/index.cjs +114 -0
  25. package/dist/index.d.ts +6 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +2376 -0
  28. package/dist/instagram_icon.avif +0 -0
  29. package/dist/livechat-standalone.d.ts +23 -0
  30. package/dist/livechat-standalone.d.ts.map +1 -0
  31. package/dist/standalone.d.ts +22 -0
  32. package/dist/standalone.d.ts.map +1 -0
  33. package/dist/telegram_icon.avif +0 -0
  34. package/dist/types.d.ts +66 -0
  35. package/dist/types.d.ts.map +1 -0
  36. package/dist/web.avif +0 -0
  37. package/dist/zalo_icon.avif +0 -0
  38. package/package.json +45 -3
  39. package/public/facebook_icon.avif +0 -0
  40. package/public/instagram_icon.avif +0 -0
  41. package/public/telegram_icon.avif +0 -0
  42. package/public/web.avif +0 -0
  43. package/public/zalo_icon.avif +0 -0
  44. package/src/ChannelHubChannels.tsx +941 -0
  45. package/src/ChannelHubInbox.tsx +1133 -0
  46. package/src/LiveChatWidget.tsx +441 -0
  47. package/src/api.ts +100 -0
  48. package/src/assets/facebook_icon.avif +0 -0
  49. package/src/assets/instagram_icon.avif +0 -0
  50. package/src/assets/telegram_icon.avif +0 -0
  51. package/src/assets/web.avif +0 -0
  52. package/src/assets/zalo_icon.avif +0 -0
  53. package/src/assets.d.ts +4 -0
  54. package/src/channels/ProviderLogo.tsx +59 -0
  55. package/src/components/ui/avatar.tsx +2 -0
  56. package/src/components/ui/button.tsx +4 -0
  57. package/src/components/ui/index.ts +5 -0
  58. package/src/components/ui/input.tsx +2 -0
  59. package/src/components/ui/scroll-area.tsx +5 -0
  60. package/src/components/ui/textarea.tsx +7 -0
  61. package/src/index.ts +8 -0
  62. package/src/livechat-standalone.tsx +75 -0
  63. package/src/standalone.tsx +68 -0
  64. package/src/types.ts +65 -0
  65. package/tsconfig.json +14 -0
  66. package/vite.config.ts +76 -0
  67. package/README.md +0 -3
@@ -0,0 +1,75 @@
1
+ import { createRoot } from 'react-dom/client';
2
+ import { LiveChatWidget } from './LiveChatWidget.js';
3
+
4
+ interface LiveChatOptions {
5
+ apiUrl: string;
6
+ widgetId: string;
7
+ primaryColor?: string;
8
+ greeting?: string;
9
+ title?: string;
10
+ identifier?: string;
11
+ hmac?: string;
12
+ }
13
+
14
+ function init(options: LiveChatOptions): void {
15
+ const container = document.createElement('div');
16
+ document.body.appendChild(container);
17
+ createRoot(container).render(
18
+ <LiveChatWidget
19
+ apiUrl={options.apiUrl}
20
+ widgetId={options.widgetId}
21
+ primaryColor={options.primaryColor}
22
+ greeting={options.greeting}
23
+ title={options.title}
24
+ identifier={options.identifier}
25
+ hmac={options.hmac}
26
+ />,
27
+ );
28
+ }
29
+
30
+ declare global {
31
+ interface Window {
32
+ ChannelHubLiveChat: { init: typeof init };
33
+ // Optional window global for server-rendered identity injection:
34
+ // Set BEFORE the script tag loads: window.__channelhub_identity = { identifier, hmac }
35
+ __channelhub_identity?: { identifier: string; hmac: string };
36
+ }
37
+ }
38
+
39
+ declare const __WIDGET_VERSION__: string;
40
+ console.log(`[ChannelHub] livechat v${__WIDGET_VERSION__} loaded`);
41
+
42
+ window.ChannelHubLiveChat = { init };
43
+
44
+ // Auto-initialize from script tag data attributes + optional window identity global
45
+ function autoInit(): void {
46
+ const script = document.currentScript as HTMLScriptElement | null
47
+ ?? document.querySelector<HTMLScriptElement>('script[data-widget-id]');
48
+ if (!script) return;
49
+ // data-manual-init suppresses autoInit — caller will invoke init() themselves
50
+ if ('manualInit' in script.dataset) return;
51
+ const widgetId = script.dataset['widgetId'];
52
+ const apiUrl = script.dataset['apiUrl'] ?? script.src.replace(/\/static\/livechat\.js.*$/, '');
53
+ if (!widgetId || !apiUrl) return;
54
+
55
+ // Identity: data attributes take priority, then window global
56
+ const identity = window.__channelhub_identity;
57
+ const identifier = script.dataset['identifier'] ?? identity?.identifier;
58
+ const hmac = script.dataset['hmac'] ?? identity?.hmac;
59
+
60
+ init({
61
+ apiUrl,
62
+ widgetId,
63
+ primaryColor: script.dataset['primaryColor'],
64
+ greeting: script.dataset['greeting'],
65
+ title: script.dataset['title'],
66
+ identifier,
67
+ hmac,
68
+ });
69
+ }
70
+
71
+ if (document.readyState === 'loading') {
72
+ document.addEventListener('DOMContentLoaded', autoInit);
73
+ } else {
74
+ autoInit();
75
+ }
@@ -0,0 +1,68 @@
1
+ import { createRoot, type Root } from 'react-dom/client';
2
+ import { ChannelHubInbox } from './ChannelHubInbox.js';
3
+ import type { WidgetTheme } from './types.js';
4
+
5
+ interface InitOptions {
6
+ container?: string | HTMLElement;
7
+ apiUrl: string;
8
+ token: string;
9
+ theme?: WidgetTheme;
10
+ onNewMessage?: (conversationId: string) => void;
11
+ }
12
+
13
+ interface WidgetInstance {
14
+ unmount: () => void;
15
+ updateToken: (token: string) => void;
16
+ }
17
+
18
+ const instances = new Map<HTMLElement, Root>();
19
+
20
+ function init(options: InitOptions): WidgetInstance {
21
+ const el =
22
+ typeof options.container === 'string'
23
+ ? (document.querySelector(options.container) as HTMLElement | null)
24
+ : options.container ?? document.getElementById('channelhub-widget');
25
+
26
+ if (!el) {
27
+ throw new Error(
28
+ '[ChannelHub] container not found: ' + (options.container ?? '#channelhub-widget'),
29
+ );
30
+ }
31
+
32
+ let currentToken = options.token;
33
+
34
+ const root = createRoot(el);
35
+ instances.set(el, root);
36
+
37
+ function render() {
38
+ root.render(
39
+ <ChannelHubInbox
40
+ apiUrl={options.apiUrl}
41
+ token={currentToken}
42
+ theme={options.theme}
43
+ onNewMessage={options.onNewMessage}
44
+ />,
45
+ );
46
+ }
47
+
48
+ render();
49
+
50
+ return {
51
+ unmount() {
52
+ root.unmount();
53
+ instances.delete(el);
54
+ },
55
+ updateToken(token: string) {
56
+ currentToken = token;
57
+ render();
58
+ },
59
+ };
60
+ }
61
+
62
+ declare global {
63
+ interface Window {
64
+ ChannelHub: { init: typeof init };
65
+ }
66
+ }
67
+
68
+ window.ChannelHub = { init };
package/src/types.ts ADDED
@@ -0,0 +1,65 @@
1
+ export type Provider = 'facebook' | 'instagram' | 'telegram' | 'zalo' | string;
2
+
3
+ export type Connection = { id: string; provider: Provider; type?: string; metadata?: Record<string, unknown> };
4
+
5
+ export type Conversation = {
6
+ id: string;
7
+ connectionId: string;
8
+ providerConversationKey: string;
9
+ subject?: string;
10
+ contactName?: string;
11
+ contactPictureUrl?: string;
12
+ lastMessageAt?: string;
13
+ lastMessageText?: string;
14
+ lastMessageDirection?: 'inbound' | 'outbound';
15
+ messageCount?: number;
16
+ unreadCount?: number;
17
+ };
18
+
19
+ export type MessageAttachment = {
20
+ type: 'image' | 'video' | 'audio' | 'file' | 'sticker';
21
+ url: string;
22
+ mimeType?: string;
23
+ };
24
+
25
+ export type Message = {
26
+ id: string;
27
+ direction: 'inbound' | 'outbound';
28
+ status?: 'sending' | 'sent' | 'delivered' | 'read' | 'failed';
29
+ text?: string;
30
+ attachments?: MessageAttachment[];
31
+ occurredAt: string;
32
+ senderId?: string;
33
+ };
34
+
35
+ export type Page<T> = { items: T[]; nextCursor?: string };
36
+
37
+ export type WidgetTheme = {
38
+ primaryColor?: string;
39
+ fontFamily?: string;
40
+ borderRadius?: string;
41
+ branding?: {
42
+ name?: string;
43
+ logoUrl?: string;
44
+ mark?: string;
45
+ };
46
+ };
47
+
48
+ export type ChannelHubInboxHandle = {
49
+ openConversation: (conversationId: string) => void;
50
+ refresh: () => void;
51
+ };
52
+
53
+ export type WidgetProps = {
54
+ apiUrl: string;
55
+ token: string;
56
+ theme?: WidgetTheme;
57
+ height?: number | string;
58
+ defaultConversationId?: string;
59
+ disableAttachments?: boolean;
60
+ disableNotificationSound?: boolean;
61
+ hideKeyboardHint?: boolean;
62
+ locale?: 'vi' | 'en';
63
+ onNewMessage?: (conversationId: string) => void;
64
+ onConversationSelect?: (conversationId: string) => void;
65
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "lib": ["ES2020", "DOM"],
5
+ "module": "ESNext",
6
+ "moduleResolution": "bundler",
7
+ "jsx": "react-jsx",
8
+ "strict": true,
9
+ "declaration": true,
10
+ "declarationMap": true,
11
+ "skipLibCheck": true
12
+ },
13
+ "include": ["src"]
14
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,76 @@
1
+ import { defineConfig } from 'vite';
2
+ import react from '@vitejs/plugin-react';
3
+ import { readFileSync } from 'node:fs';
4
+
5
+ const { version } = JSON.parse(readFileSync('./package.json', 'utf-8')) as { version: string };
6
+
7
+ const sharedDefine = { __WIDGET_VERSION__: JSON.stringify(version) };
8
+
9
+ const libConfig = defineConfig({
10
+ plugins: [react()],
11
+ define: sharedDefine,
12
+ build: {
13
+ assetsInlineLimit: 0,
14
+ lib: {
15
+ entry: 'src/index.ts',
16
+ formats: ['es', 'cjs'],
17
+ fileName: (format) => (format === 'es' ? 'index.js' : 'index.cjs'),
18
+ },
19
+ rollupOptions: {
20
+ external: ['react', 'react-dom', 'react/jsx-runtime'],
21
+ output: {
22
+ globals: { react: 'React', 'react-dom': 'ReactDOM' },
23
+ assetFileNames: 'assets/[name][extname]',
24
+ },
25
+ },
26
+ },
27
+ });
28
+
29
+ const standaloneConfig = defineConfig({
30
+ plugins: [react()],
31
+ build: {
32
+ outDir: 'dist',
33
+ emptyOutDir: false,
34
+ lib: {
35
+ entry: 'src/standalone.tsx',
36
+ formats: ['iife'],
37
+ name: 'ChannelHub',
38
+ fileName: () => 'channelhub-widget.iife.js',
39
+ },
40
+ // Bundle React inside — host page needs no deps
41
+ rollupOptions: {
42
+ output: {
43
+ inlineDynamicImports: true,
44
+ },
45
+ },
46
+ },
47
+ });
48
+
49
+ const livechatConfig = defineConfig({
50
+ plugins: [react()],
51
+ define: {
52
+ 'process.env.NODE_ENV': '"production"',
53
+ ...sharedDefine,
54
+ },
55
+ build: {
56
+ outDir: 'dist',
57
+ emptyOutDir: false,
58
+ lib: {
59
+ entry: 'src/livechat-standalone.tsx',
60
+ formats: ['iife'],
61
+ name: 'ChannelHubLiveChat',
62
+ fileName: () => 'livechat.iife.js',
63
+ },
64
+ rollupOptions: {
65
+ output: {
66
+ inlineDynamicImports: true,
67
+ },
68
+ },
69
+ },
70
+ });
71
+
72
+ export default process.env.BUILD_TARGET === 'standalone'
73
+ ? standaloneConfig
74
+ : process.env.BUILD_TARGET === 'livechat'
75
+ ? livechatConfig
76
+ : libConfig;
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.