@ihoomanai/react-chat 2.0.0

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/src/types.ts ADDED
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Type definitions for @ihooman/react-chat
3
+ *
4
+ * Re-exports core types from @ihooman/chat-widget and defines
5
+ * React-specific types for components and hooks.
6
+ */
7
+
8
+ // Re-export core types for convenience
9
+ export type {
10
+ WidgetConfig,
11
+ UserInfo,
12
+ Message,
13
+ WidgetState,
14
+ WidgetEvent,
15
+ EventCallback,
16
+ ThemeConfig,
17
+ BehaviorConfig,
18
+ BrandingConfig,
19
+ IhoomanChatAPI,
20
+ } from '@ihooman/chat-widget';
21
+
22
+ /**
23
+ * Props for the ChatWidget React component
24
+ */
25
+ export interface ChatWidgetProps {
26
+ /**
27
+ * Widget ID for initialization (required)
28
+ * Format: wgt_[12-alphanumeric-characters]
29
+ * Obtain from your Ihooman dashboard
30
+ */
31
+ widgetId: string;
32
+
33
+ /**
34
+ * Server URL for the widget API
35
+ * Defaults to production Ihooman API
36
+ */
37
+ serverUrl?: string;
38
+
39
+ /** Widget color theme */
40
+ theme?: 'light' | 'dark';
41
+
42
+ /** Widget position on the page */
43
+ position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
44
+
45
+ /** Start with widget open */
46
+ startOpen?: boolean;
47
+
48
+ /** User information for personalization */
49
+ user?: {
50
+ name?: string;
51
+ email?: string;
52
+ metadata?: Record<string, string>;
53
+ };
54
+
55
+ /** Widget title displayed in header */
56
+ title?: string;
57
+
58
+ /** Subtitle displayed in header */
59
+ subtitle?: string;
60
+
61
+ /** Welcome message shown when chat opens */
62
+ welcomeMessage?: string;
63
+
64
+ /** Placeholder text for input field */
65
+ placeholder?: string;
66
+
67
+ /** Primary brand color */
68
+ primaryColor?: string;
69
+
70
+ /** Gradient start color */
71
+ gradientFrom?: string;
72
+
73
+ /** Gradient end color */
74
+ gradientTo?: string;
75
+
76
+ /** Show timestamps on messages */
77
+ showTimestamps?: boolean;
78
+
79
+ /** Show typing indicator */
80
+ showTypingIndicator?: boolean;
81
+
82
+ /** Enable notification sounds */
83
+ enableSounds?: boolean;
84
+
85
+ /** Enable file upload */
86
+ enableFileUpload?: boolean;
87
+
88
+ /** Persist session across page reloads */
89
+ persistSession?: boolean;
90
+
91
+ /** Z-index for widget positioning */
92
+ zIndex?: number;
93
+
94
+ /** Widget width in pixels */
95
+ width?: number;
96
+
97
+ /** Widget height in pixels */
98
+ height?: number;
99
+
100
+ /** Toggle button size in pixels */
101
+ buttonSize?: number;
102
+
103
+ /** Border radius in pixels */
104
+ borderRadius?: number;
105
+
106
+ /** Font family */
107
+ fontFamily?: string;
108
+
109
+ /** Custom avatar URL */
110
+ avatarUrl?: string;
111
+
112
+ /** Show powered by branding */
113
+ poweredBy?: boolean;
114
+
115
+ /** Callback when widget is ready */
116
+ onReady?: () => void;
117
+
118
+ /** Callback when widget opens */
119
+ onOpen?: () => void;
120
+
121
+ /** Callback when widget closes */
122
+ onClose?: () => void;
123
+
124
+ /** Callback when a message is sent or received */
125
+ onMessage?: (message: import('@ihooman/chat-widget').Message) => void;
126
+
127
+ /** Callback when an error occurs */
128
+ onError?: (error: Error) => void;
129
+ }
130
+
131
+ /**
132
+ * Return type for the useChatWidget hook
133
+ */
134
+ export interface UseChatWidgetReturn {
135
+ /** Whether the widget window is currently open */
136
+ isOpen: boolean;
137
+
138
+ /** Whether the widget is initialized and ready */
139
+ isReady: boolean;
140
+
141
+ /** Whether connected to the server */
142
+ isConnected: boolean;
143
+
144
+ /** Open the chat widget window */
145
+ open: () => void;
146
+
147
+ /** Close the chat widget window */
148
+ close: () => void;
149
+
150
+ /** Toggle the chat widget window open/closed */
151
+ toggle: () => void;
152
+
153
+ /** Send a message programmatically */
154
+ sendMessage: (content: string) => void;
155
+
156
+ /** Set user information for personalization */
157
+ setUser: (user: { name?: string; email?: string; metadata?: Record<string, string> }) => void;
158
+
159
+ /** Clear chat history and start a new conversation */
160
+ clearHistory: () => void;
161
+
162
+ /** Current chat messages */
163
+ messages: import('@ihooman/chat-widget').Message[];
164
+
165
+ /** Number of unread messages */
166
+ unreadCount: number;
167
+ }
168
+
169
+ /**
170
+ * Props for the ChatWidgetProvider component
171
+ */
172
+ export interface ChatWidgetProviderProps {
173
+ /** Child components */
174
+ children: React.ReactNode;
175
+ }
@@ -0,0 +1,149 @@
1
+ /**
2
+ * useChatWidget Hook
3
+ *
4
+ * Provides programmatic control over the Ihooman Chat Widget
5
+ * from any component within the ChatWidgetProvider.
6
+ *
7
+ * @module @ihooman/react-chat
8
+ */
9
+
10
+ import { useCallback } from 'react';
11
+ import { useChatWidgetContext, isWidgetAvailable } from './context';
12
+ import type { UseChatWidgetReturn } from './types';
13
+
14
+ /**
15
+ * Hook for controlling the Ihooman Chat Widget
16
+ *
17
+ * Must be used within a ChatWidgetProvider. Provides access to
18
+ * widget state and control functions.
19
+ *
20
+ * @returns Object containing widget state and control functions
21
+ *
22
+ * @example Basic usage
23
+ * ```tsx
24
+ * import { useChatWidget } from '@ihooman/react-chat';
25
+ *
26
+ * function SupportButton() {
27
+ * const { open, isReady } = useChatWidget();
28
+ *
29
+ * return (
30
+ * <button onClick={open} disabled={!isReady}>
31
+ * Need Help?
32
+ * </button>
33
+ * );
34
+ * }
35
+ * ```
36
+ *
37
+ * @example Sending messages programmatically
38
+ * ```tsx
39
+ * function QuickActions() {
40
+ * const { sendMessage, isReady } = useChatWidget();
41
+ *
42
+ * const handleOrderHelp = () => {
43
+ * sendMessage('I need help with my order');
44
+ * };
45
+ *
46
+ * return (
47
+ * <button onClick={handleOrderHelp} disabled={!isReady}>
48
+ * Order Help
49
+ * </button>
50
+ * );
51
+ * }
52
+ * ```
53
+ *
54
+ * @example Accessing widget state
55
+ * ```tsx
56
+ * function ChatStatus() {
57
+ * const { isOpen, isConnected, unreadCount } = useChatWidget();
58
+ *
59
+ * return (
60
+ * <div>
61
+ * <span>Chat: {isOpen ? 'Open' : 'Closed'}</span>
62
+ * <span>Status: {isConnected ? 'Connected' : 'Disconnected'}</span>
63
+ * {unreadCount > 0 && <span>({unreadCount} unread)</span>}
64
+ * </div>
65
+ * );
66
+ * }
67
+ * ```
68
+ */
69
+ export function useChatWidget(): UseChatWidgetReturn {
70
+ const context = useChatWidgetContext();
71
+
72
+ /**
73
+ * Open the chat widget window
74
+ */
75
+ const open = useCallback(() => {
76
+ if (isWidgetAvailable(context.widgetRef)) {
77
+ context.widgetRef.open();
78
+ }
79
+ }, [context.widgetRef]);
80
+
81
+ /**
82
+ * Close the chat widget window
83
+ */
84
+ const close = useCallback(() => {
85
+ if (isWidgetAvailable(context.widgetRef)) {
86
+ context.widgetRef.close();
87
+ }
88
+ }, [context.widgetRef]);
89
+
90
+ /**
91
+ * Toggle the chat widget window open/closed
92
+ */
93
+ const toggle = useCallback(() => {
94
+ if (isWidgetAvailable(context.widgetRef)) {
95
+ context.widgetRef.toggle();
96
+ }
97
+ }, [context.widgetRef]);
98
+
99
+ /**
100
+ * Send a message programmatically
101
+ */
102
+ const sendMessage = useCallback(
103
+ (content: string) => {
104
+ if (isWidgetAvailable(context.widgetRef)) {
105
+ context.widgetRef.sendMessage(content);
106
+ }
107
+ },
108
+ [context.widgetRef]
109
+ );
110
+
111
+ /**
112
+ * Set user information for personalization
113
+ */
114
+ const setUser = useCallback(
115
+ (user: { name?: string; email?: string; metadata?: Record<string, string> }) => {
116
+ if (isWidgetAvailable(context.widgetRef)) {
117
+ context.widgetRef.setUser(user);
118
+ }
119
+ },
120
+ [context.widgetRef]
121
+ );
122
+
123
+ /**
124
+ * Clear chat history and start a new conversation
125
+ */
126
+ const clearHistory = useCallback(() => {
127
+ if (isWidgetAvailable(context.widgetRef)) {
128
+ context.widgetRef.clearHistory();
129
+ context.setMessages([]);
130
+ context.setUnreadCount(0);
131
+ }
132
+ }, [context]);
133
+
134
+ return {
135
+ isOpen: context.isOpen,
136
+ isReady: context.isReady,
137
+ isConnected: context.isConnected,
138
+ open,
139
+ close,
140
+ toggle,
141
+ sendMessage,
142
+ setUser,
143
+ clearHistory,
144
+ messages: context.messages,
145
+ unreadCount: context.unreadCount,
146
+ };
147
+ }
148
+
149
+ export default useChatWidget;