@channelhub/widget 0.0.0-stage → 0.1.3
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/ChannelHubChannels.d.ts +15 -0
- package/dist/ChannelHubChannels.d.ts.map +1 -0
- package/dist/ChannelHubInbox.d.ts +3 -0
- package/dist/ChannelHubInbox.d.ts.map +1 -0
- package/dist/LiveChatWidget.d.ts +12 -0
- package/dist/LiveChatWidget.d.ts.map +1 -0
- package/dist/api.d.ts +20 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/channels/ProviderLogo.d.ts +8 -0
- package/dist/channels/ProviderLogo.d.ts.map +1 -0
- package/dist/components/ui/avatar.d.ts +8 -0
- package/dist/components/ui/avatar.d.ts.map +1 -0
- package/dist/components/ui/button.d.ts +5 -0
- package/dist/components/ui/button.d.ts.map +1 -0
- package/dist/components/ui/index.d.ts +6 -0
- package/dist/components/ui/index.d.ts.map +1 -0
- package/dist/components/ui/input.d.ts +3 -0
- package/dist/components/ui/input.d.ts.map +1 -0
- package/dist/components/ui/scroll-area.d.ts +6 -0
- package/dist/components/ui/scroll-area.d.ts.map +1 -0
- package/dist/components/ui/textarea.d.ts +3 -0
- package/dist/components/ui/textarea.d.ts.map +1 -0
- package/dist/facebook_icon.avif +0 -0
- package/dist/index.cjs +99 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1975 -0
- package/dist/instagram_icon.avif +0 -0
- package/dist/livechat-standalone.d.ts +23 -0
- package/dist/livechat-standalone.d.ts.map +1 -0
- package/dist/standalone.d.ts +22 -0
- package/dist/standalone.d.ts.map +1 -0
- package/dist/telegram_icon.avif +0 -0
- package/dist/types.d.ts +66 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/web.avif +0 -0
- package/dist/zalo_icon.avif +0 -0
- package/package.json +45 -3
- package/public/facebook_icon.avif +0 -0
- package/public/instagram_icon.avif +0 -0
- package/public/telegram_icon.avif +0 -0
- package/public/web.avif +0 -0
- package/public/zalo_icon.avif +0 -0
- package/src/ChannelHubChannels.tsx +941 -0
- package/src/ChannelHubInbox.tsx +769 -0
- package/src/LiveChatWidget.tsx +380 -0
- package/src/api.ts +84 -0
- package/src/assets/facebook_icon.avif +0 -0
- package/src/assets/instagram_icon.avif +0 -0
- package/src/assets/telegram_icon.avif +0 -0
- package/src/assets/web.avif +0 -0
- package/src/assets/zalo_icon.avif +0 -0
- package/src/assets.d.ts +4 -0
- package/src/channels/ProviderLogo.tsx +59 -0
- package/src/components/ui/avatar.tsx +2 -0
- package/src/components/ui/button.tsx +4 -0
- package/src/components/ui/index.ts +5 -0
- package/src/components/ui/input.tsx +2 -0
- package/src/components/ui/scroll-area.tsx +5 -0
- package/src/components/ui/textarea.tsx +2 -0
- package/src/index.ts +8 -0
- package/src/livechat-standalone.tsx +75 -0
- package/src/standalone.tsx +68 -0
- package/src/types.ts +65 -0
- package/tsconfig.json +14 -0
- package/vite.config.ts +76 -0
- package/README.md +0 -3
|
@@ -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