@data-netmonk/mona-chat-widget 2.1.36 → 2.1.37

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 (4) hide show
  1. package/README.md +271 -8
  2. package/dist/index.cjs +61 -60
  3. package/dist/index.js +21395 -11451
  4. package/package.json +3 -2
package/README.md CHANGED
@@ -146,26 +146,289 @@ For responses with buttons:
146
146
 
147
147
  ---
148
148
 
149
- 1. Install package
150
- ```
151
- npm install @aaiiccaa/mona-chat-widget
149
+ 1. **Install package**
150
+ ```bash
151
+ npm install @data-netmonk/mona-chat-widget
152
152
  ```
153
- 2. Import styles on your `App.jsx` or `index.jsx`
153
+
154
+ 2. **Import styles on your `App.jsx` or `index.jsx`**
154
155
 
155
156
  ```jsx
156
- import "@aaiiccaa/mona-chat-widget/dist/style.css";
157
+ import "@data-netmonk/mona-chat-widget/dist/style.css";
157
158
  ```
158
159
 
159
- 3. Import & use component
160
+ 3. **Import & use component**
160
161
 
161
162
  ```jsx
162
- import { ChatWidget } from "@aaiiccaa/mona-chat-widget";
163
+ import { ChatWidget } from "@data-netmonk/mona-chat-widget";
163
164
 
164
- <ChatWidget type={"prime"} userId={"1234"} sourceId={"1"}/>;
165
+ function App() {
166
+ return (
167
+ <ChatWidget
168
+ type="prime"
169
+ userId="user123"
170
+ sourceId="691e1b5952068ff7aaeccffc9"
171
+ botServerUrl="https://your-backend-url.com"
172
+ />
173
+ );
174
+ }
165
175
  ```
166
176
 
167
177
  ---
168
178
 
179
+ ### Component Props
180
+
181
+ ---
182
+
183
+ #### Required Props
184
+
185
+ | Prop | Type | Description |
186
+ |------|------|-------------|
187
+ | `userId` | `string` | **Required.** Unique identifier for the user |
188
+ | `sourceId` | `string` | **Required.** Source/channel identifier for the chat |
189
+
190
+ #### Dynamic Data Prop (NEW!)
191
+ | Prop | Type | Description |
192
+ |------|------|-------------|
193
+ | `data` | `string` | URL-encoded config string (see advanced usage below) |
194
+
195
+
196
+ #### Optional Props
197
+
198
+ | Prop | Type | Default | Description |
199
+ |------|------|---------|-------------|
200
+ | `type` | `"prime" \| "hi"` | `"hi"` | Agent type for multi-agent system |
201
+ | `botServerUrl` | `string` | - | Backend API URL (falls back to env vars if not provided) |
202
+ | `data` | `string` | - | URL-encoded config string (see advanced usage below) |
203
+ | `width` | `string` | `"25vw"` | Widget width (CSS value) |
204
+ | `height` | `string` | `"90vh"` | Widget height (CSS value) |
205
+ | `right` | `string` | `"1.25rem"` | Distance from right edge |
206
+ | `bottom` | `string` | `"1.25rem"` | Distance from bottom edge |
207
+ | `zIndex` | `number` | `2000` | CSS z-index for widget positioning |
208
+ | `position` | `string` | `"fixed"` | CSS position (`"fixed"` or `"relative"`) |
209
+ | `onToggle` | `function` | - | Callback when widget opens/closes: `(isOpen: boolean) => void` |
210
+
211
+ ---
212
+
213
+ ### Usage Examples
214
+
215
+ ---
216
+
217
+ #### Basic Usage
218
+
219
+ ```jsx
220
+ import { ChatWidget } from "@data-netmonk/mona-chat-widget";
221
+ import "@data-netmonk/mona-chat-widget/dist/style.css";
222
+
223
+ function App() {
224
+ return (
225
+ <ChatWidget
226
+ userId="user123"
227
+ sourceId="691e1b5952068ff7aaeccffc9"
228
+ />
229
+ );
230
+ }
231
+ ```
232
+
233
+ #### With Backend URL
234
+
235
+ ```jsx
236
+ <ChatWidget
237
+ userId="user123"
238
+ sourceId="691e1b5952068ff7aaeccffc9"
239
+ botServerUrl="https://api.example.com"
240
+ type="prime"
241
+ />
242
+ ```
243
+
244
+ #### With Custom Variables (NEW!)
245
+
246
+ Pass custom user data like username, phone number, etc. to the backend:
247
+
248
+ ```jsx
249
+ <ChatWidget
250
+ data="url:https://api.example.com~user_id=user123~source_id=691e1b5952068ff7aaeccffc9~username=John Doe~telephone_number=+628123456789~email=john@example.com"
251
+ />
252
+ ```
253
+
254
+ **Variables sent to backend:**
255
+ ```json
256
+ {
257
+ "chat_id": "...",
258
+ "session_id": "...",
259
+ "user_id": "user123",
260
+ "message": "Hello",
261
+ "type": "text",
262
+ "variables": {
263
+ "username": "John Doe",
264
+ "telephone_number": "+628123456789",
265
+ "email": "john@example.com"
266
+ }
267
+ }
268
+ ```
269
+
270
+ **Reserved keys** (won't go into `variables`):
271
+ - `url` - Backend URL
272
+ - `user_id` - User identifier
273
+ - `source_id` - Source identifier
274
+ - `type` - Agent type
275
+
276
+ **Any other key** you add will be included in the `variables` object automatically!
277
+
278
+ #### Custom Styling
279
+
280
+ ```jsx
281
+ <ChatWidget
282
+ userId="user123"
283
+ sourceId="691e1b5952068ff7aaeccffc9"
284
+ width="400px"
285
+ height="600px"
286
+ right="20px"
287
+ bottom="20px"
288
+ zIndex={9999}
289
+ />
290
+ ```
291
+
292
+ #### Embedded in Layout (Relative Positioning)
293
+
294
+ ```jsx
295
+ <div style={{ display: 'grid', gridTemplateColumns: '1fr 400px' }}>
296
+ <div>Main content</div>
297
+ <ChatWidget
298
+ userId="user123"
299
+ sourceId="691e1b5952068ff7aaeccffc9"
300
+ position="relative"
301
+ width="100%"
302
+ height="100vh"
303
+ />
304
+ </div>
305
+ ```
306
+
307
+ #### With Toggle Callback
308
+
309
+ ```jsx
310
+ function App() {
311
+ const handleToggle = (isOpen) => {
312
+ console.log('Chat widget is', isOpen ? 'open' : 'closed');
313
+ // Track analytics, update UI, etc.
314
+ };
315
+
316
+ return (
317
+ <ChatWidget
318
+ userId="user123"
319
+ sourceId="691e1b5952068ff7aaeccffc9"
320
+ onToggle={handleToggle}
321
+ />
322
+ );
323
+ }
324
+ ```
325
+
326
+ #### Full-Screen Widget
327
+
328
+ ```jsx
329
+ <ChatWidget
330
+ userId="user123"
331
+ sourceId="691e1b5952068ff7aaeccffc9"
332
+ width="100vw"
333
+ height="100vh"
334
+ right="0"
335
+ bottom="0"
336
+ />
337
+ ```
338
+
339
+ ---
340
+
341
+ ### Data Prop Format & Helper Function
342
+
343
+ ---
344
+
345
+ The `data` prop allows you to pass configuration via a single string. This is especially useful when you need to dynamically build the configuration or pass it through URL parameters.
346
+
347
+ **Format:** `key:value` pairs separated by `~`
348
+
349
+ **Example data string:**
350
+ ```
351
+ url:https://api.example.com~user_id=user123~source_id=abc~username=John~phone=+1234567890
352
+ ```
353
+
354
+ #### Building Data String Programmatically
355
+
356
+ Instead of manually constructing the data string, you can create a helper function to build it dynamically:
357
+
358
+ **Usage:**
359
+ ```jsx
360
+ import { ChatWidget } from "@data-netmonk/mona-chat-widget";
361
+ import { buildChatWidgetData } from "./helpers/chatWidget";
362
+
363
+ function App() {
364
+ const chatData = buildChatWidgetData({
365
+ userId: "user123",
366
+ sourceId: "691e1b5952068ff7aaeccffc9",
367
+ botServerUrl: "https://api.example.com",
368
+ username: "John Doe",
369
+ email: "john@example.com",
370
+ phone: "+628123456789"
371
+ });
372
+
373
+ return <ChatWidget data={chatData} />;
374
+ }
375
+ ```
376
+
377
+ The helper function should:
378
+ - Accept configuration parameters (userId, sourceId, botServerUrl, and any custom variables)
379
+ - Build the data string in format: `key=value` pairs separated by `~`
380
+ - Use `url:` prefix (not `url=`) for the backend URL
381
+ - Return the formatted string
382
+
383
+ #### Using with URL Parameters (Optional)
384
+
385
+ If you need to pass the data through URL parameters:
386
+
387
+ ```jsx
388
+ // Parse from URL
389
+ const urlParams = new URLSearchParams(window.location.search);
390
+ const dataParam = urlParams.get('data');
391
+
392
+ <ChatWidget data={dataParam} />
393
+ ```
394
+
395
+ **URL example:**
396
+ ```
397
+ https://yourapp.com/chat?data=url:https://api.example.com~user_id=user123~source_id=abc~username=John~phone=%2B628123456789
398
+ ```
399
+
400
+ #### Real-World Example from ui-mona-web
401
+
402
+ See the complete helper implementation in `ui-mona-web/src/helpers/chatWidget.js`:
403
+
404
+ ```jsx
405
+ import { getUserId } from './cookie';
406
+
407
+ export const buildChatWidgetData = ({ loginType, sourceId, userId, botServerUrl }) => {
408
+ // Get userId from cookies if not provided
409
+ const finalUserId = userId || getUserId();
410
+
411
+ // Get sourceId based on loginType
412
+ const finalSourceId = sourceId || getSourceIdByLoginType(loginType);
413
+
414
+ // Get bot server URL from env
415
+ const finalBotServerUrl = import.meta.env.VITE_CHAT_WIDGET_WEBHOOK_URL || botServerUrl;
416
+
417
+ const parts = [
418
+ `user_id=${String(finalUserId)}`,
419
+ `source_id=${finalSourceId}`,
420
+ ];
421
+
422
+ if (finalBotServerUrl) {
423
+ parts.unshift(`url:${finalBotServerUrl}`);
424
+ }
425
+
426
+ return parts.join("~");
427
+ };
428
+ ```
429
+
430
+ ---
431
+
169
432
  ### Standalone app (for demonstration)
170
433
 
171
434
  1. **How to run locally** (access at `http://localhost:${PORT}/${APP_PREFIX}`)