@asgard-js/react 0.1.24 → 0.1.25
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/README.md +65 -65
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -243,70 +243,6 @@ config: {
|
|
|
243
243
|
|
|
244
244
|
**Backward Compatibility:** Existing code using `endpoint` will continue to work but may show deprecation warnings when `debugMode` is enabled.
|
|
245
245
|
|
|
246
|
-
### EMIT Action
|
|
247
|
-
|
|
248
|
-
EMIT buttons allow you to handle custom actions in your application. Implement the `onTemplateBtnClick` callback to process these events. See the [EMIT Action documentation](https://www.asgard-ai.com/docs/developer-reference/asgard-builtin/message-template-action-object-emit) for details.
|
|
249
|
-
|
|
250
|
-
The callback receives the following parameters:
|
|
251
|
-
|
|
252
|
-
1. `payload` (optional): Custom data from the button action
|
|
253
|
-
2. `eventName` (required): Event name specified in the button action
|
|
254
|
-
3. `raw` (required): Complete SSE response data as JSON string. Use this when you need information beyond `payload` and `eventName`. Parse it to access additional fields from the original SSE response. See [SSE Response documentation](https://www.asgard-ai.com/docs/developer-reference/api-doc/send-message/sse-response/message-complete) for the complete response structure.
|
|
255
|
-
|
|
256
|
-
Configure EMIT buttons in your backend SSE response:
|
|
257
|
-
|
|
258
|
-
```json
|
|
259
|
-
{
|
|
260
|
-
"template": {
|
|
261
|
-
"type": "BUTTON",
|
|
262
|
-
"title": "Action Menu",
|
|
263
|
-
"text": "Please select an action:",
|
|
264
|
-
"buttons": [
|
|
265
|
-
{
|
|
266
|
-
"label": "Support Request",
|
|
267
|
-
"action": {
|
|
268
|
-
"type": "EMIT",
|
|
269
|
-
"eventName": "support_request",
|
|
270
|
-
"payload": {
|
|
271
|
-
"category": "technical",
|
|
272
|
-
"priority": "high"
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
]
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
#### EMIT Example
|
|
282
|
-
|
|
283
|
-
```typescript
|
|
284
|
-
import { useCallback } from 'react';
|
|
285
|
-
|
|
286
|
-
const handleTemplateBtnClick = useCallback((payload: Record<string, unknown>, eventName: string, raw: string): void => {
|
|
287
|
-
if (eventName === 'support_request') {
|
|
288
|
-
// Access payload data
|
|
289
|
-
const category = payload.category as string;
|
|
290
|
-
const priority = payload.priority as string;
|
|
291
|
-
|
|
292
|
-
// Optionally parse raw SSE data to access additional fields
|
|
293
|
-
let customChannelId: string | undefined;
|
|
294
|
-
try {
|
|
295
|
-
const sseData = JSON.parse(raw);
|
|
296
|
-
customChannelId = sseData.customChannelId;
|
|
297
|
-
} catch {
|
|
298
|
-
// Handle parse error if needed
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
const channelInfo = customChannelId ? `\nChannel ID: ${customChannelId}` : '';
|
|
302
|
-
window.alert(`Support request created\n\nCategory: ${category}\nPriority: ${priority}${channelInfo}`);
|
|
303
|
-
}
|
|
304
|
-
}, []);
|
|
305
|
-
|
|
306
|
-
// Pass the handler to Chatbot
|
|
307
|
-
<Chatbot config={config} customChannelId={nanoid()} onTemplateBtnClick={handleTemplateBtnClick} />;
|
|
308
|
-
```
|
|
309
|
-
|
|
310
246
|
### Chatbot Component Props
|
|
311
247
|
|
|
312
248
|
- **title?**: `string` - The title of the chatbot (optional). If not provided, will use the value from the API if available.
|
|
@@ -552,7 +488,7 @@ const defaultTheme = {
|
|
|
552
488
|
};
|
|
553
489
|
```
|
|
554
490
|
|
|
555
|
-
|
|
491
|
+
#### Usage Example
|
|
556
492
|
|
|
557
493
|
```javascript
|
|
558
494
|
const App = () => {
|
|
@@ -583,6 +519,70 @@ const App = () => {
|
|
|
583
519
|
|
|
584
520
|
Note: When `fullScreen` prop is set to `true`, the chatbot's width and height will be set to `100vw` and `100vh` respectively, and `borderRadius` will be set to zero, regardless of theme settings.
|
|
585
521
|
|
|
522
|
+
### EMIT Action
|
|
523
|
+
|
|
524
|
+
EMIT buttons allow you to handle custom actions in your application. Implement the `onTemplateBtnClick` callback to process these events. See the [EMIT Action documentation](https://www.asgard-ai.com/docs/developer-reference/asgard-builtin/message-template-action-object-emit) for details.
|
|
525
|
+
|
|
526
|
+
The callback receives the following parameters:
|
|
527
|
+
|
|
528
|
+
1. `payload` (optional): Custom data from the button action
|
|
529
|
+
2. `eventName` (required): Event name specified in the button action
|
|
530
|
+
3. `raw` (required): Complete SSE response data as JSON string. Use this when you need information beyond `payload` and `eventName`. Parse it to access additional fields from the original SSE response. See [SSE Response documentation](https://www.asgard-ai.com/docs/developer-reference/api-doc/send-message/sse-response/message-complete) for the complete response structure.
|
|
531
|
+
|
|
532
|
+
Configure EMIT buttons in your backend SSE response:
|
|
533
|
+
|
|
534
|
+
```json
|
|
535
|
+
{
|
|
536
|
+
"template": {
|
|
537
|
+
"type": "BUTTON",
|
|
538
|
+
"title": "Action Menu",
|
|
539
|
+
"text": "Please select an action:",
|
|
540
|
+
"buttons": [
|
|
541
|
+
{
|
|
542
|
+
"label": "Support Request",
|
|
543
|
+
"action": {
|
|
544
|
+
"type": "EMIT",
|
|
545
|
+
"eventName": "support_request",
|
|
546
|
+
"payload": {
|
|
547
|
+
"category": "technical",
|
|
548
|
+
"priority": "high"
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
]
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
#### Usage Example
|
|
558
|
+
|
|
559
|
+
```typescript
|
|
560
|
+
import { useCallback } from 'react';
|
|
561
|
+
|
|
562
|
+
const handleTemplateBtnClick = useCallback((payload: Record<string, unknown>, eventName: string, raw: string): void => {
|
|
563
|
+
if (eventName === 'support_request') {
|
|
564
|
+
// Access payload data
|
|
565
|
+
const category = payload.category as string;
|
|
566
|
+
const priority = payload.priority as string;
|
|
567
|
+
|
|
568
|
+
// Optionally parse raw SSE data to access additional fields
|
|
569
|
+
let customChannelId: string | undefined;
|
|
570
|
+
try {
|
|
571
|
+
const sseData = JSON.parse(raw);
|
|
572
|
+
customChannelId = sseData.customChannelId;
|
|
573
|
+
} catch {
|
|
574
|
+
// Handle parse error if needed
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const channelInfo = customChannelId ? `\nChannel ID: ${customChannelId}` : '';
|
|
578
|
+
window.alert(`Support request created\n\nCategory: ${category}\nPriority: ${priority}${channelInfo}`);
|
|
579
|
+
}
|
|
580
|
+
}, []);
|
|
581
|
+
|
|
582
|
+
// Pass the handler to Chatbot
|
|
583
|
+
<Chatbot config={config} customChannelId={nanoid()} onTemplateBtnClick={handleTemplateBtnClick} />;
|
|
584
|
+
```
|
|
585
|
+
|
|
586
586
|
## Testing
|
|
587
587
|
|
|
588
588
|
The React package includes comprehensive tests using Vitest and React Testing Library.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asgard-js/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.25",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"vitest": "^1.6.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"@asgard-js/core": "^0.1.
|
|
51
|
+
"@asgard-js/core": "^0.1.25",
|
|
52
52
|
"react": "^18.0.0 || ^19.0.0",
|
|
53
53
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
54
54
|
},
|