@asgard-js/react 0.0.2 → 0.0.3-8.canary-1
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/.babelrc +12 -0
- package/README.md +523 -4
- package/dist/components/chatbot/chatbot-body/chatbot-body.d.ts.map +1 -0
- package/dist/components/{conversation-message-renderer → chatbot/chatbot-body}/conversation-message-renderer.d.ts +2 -2
- package/dist/components/chatbot/chatbot-body/conversation-message-renderer.d.ts.map +1 -0
- package/dist/components/chatbot/chatbot-body/index.d.ts +2 -0
- package/dist/components/chatbot/chatbot-body/index.d.ts.map +1 -0
- package/dist/components/chatbot/chatbot-container/chatbot-container.d.ts +10 -0
- package/dist/components/chatbot/chatbot-container/chatbot-container.d.ts.map +1 -0
- package/dist/components/chatbot/chatbot-container/chatbot-full-screen-container.d.ts +4 -0
- package/dist/components/chatbot/chatbot-container/chatbot-full-screen-container.d.ts.map +1 -0
- package/dist/components/chatbot/chatbot-footer/chatbot-footer.d.ts.map +1 -0
- package/dist/components/chatbot/chatbot-footer/index.d.ts +2 -0
- package/dist/components/chatbot/chatbot-footer/index.d.ts.map +1 -0
- package/dist/components/chatbot/chatbot-footer/speech-input-button.d.ts +10 -0
- package/dist/components/chatbot/chatbot-footer/speech-input-button.d.ts.map +1 -0
- package/dist/components/chatbot/chatbot-header/chatbot-header.d.ts +12 -0
- package/dist/components/chatbot/chatbot-header/chatbot-header.d.ts.map +1 -0
- package/dist/components/chatbot/chatbot-header/index.d.ts +2 -0
- package/dist/components/chatbot/chatbot-header/index.d.ts.map +1 -0
- package/dist/components/chatbot/chatbot.d.ts +25 -8
- package/dist/components/chatbot/chatbot.d.ts.map +1 -1
- package/dist/components/chatbot/profile-icon.d.ts +5 -1
- package/dist/components/chatbot/profile-icon.d.ts.map +1 -1
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/templates/avatar/avatar.d.ts +5 -1
- package/dist/components/templates/avatar/avatar.d.ts.map +1 -1
- package/dist/components/templates/button-template/button-template.d.ts +2 -2
- package/dist/components/templates/button-template/button-template.d.ts.map +1 -1
- package/dist/components/templates/button-template/card.d.ts +8 -2
- package/dist/components/templates/button-template/card.d.ts.map +1 -1
- package/dist/components/templates/carousel-template/carousel-template.d.ts +2 -2
- package/dist/components/templates/carousel-template/carousel-template.d.ts.map +1 -1
- package/dist/components/templates/chart-template/chart-template.d.ts +9 -0
- package/dist/components/templates/chart-template/chart-template.d.ts.map +1 -0
- package/dist/components/templates/chart-template/index.d.ts +2 -0
- package/dist/components/templates/chart-template/index.d.ts.map +1 -0
- package/dist/components/templates/hint-template/hint-template.d.ts +2 -2
- package/dist/components/templates/hint-template/hint-template.d.ts.map +1 -1
- package/dist/components/templates/image-template/image-template.d.ts +9 -0
- package/dist/components/templates/image-template/image-template.d.ts.map +1 -0
- package/dist/components/templates/image-template/index.d.ts +2 -0
- package/dist/components/templates/image-template/index.d.ts.map +1 -0
- package/dist/components/templates/index.d.ts +2 -1
- package/dist/components/templates/index.d.ts.map +1 -1
- package/dist/components/templates/quick-replies/quick-replies.d.ts.map +1 -1
- package/dist/components/templates/template-box/template-box-content.d.ts.map +1 -1
- package/dist/components/templates/template-box/template-box.d.ts +5 -1
- package/dist/components/templates/template-box/template-box.d.ts.map +1 -1
- package/dist/components/templates/{bot-typing-box → text-template}/bot-typing-box.d.ts +2 -2
- package/dist/components/templates/text-template/bot-typing-box.d.ts.map +1 -0
- package/dist/components/templates/text-template/bot-typing-placeholder.d.ts +8 -0
- package/dist/components/templates/text-template/bot-typing-placeholder.d.ts.map +1 -0
- package/dist/components/templates/text-template/index.d.ts +2 -0
- package/dist/components/templates/text-template/index.d.ts.map +1 -1
- package/dist/components/templates/text-template/text-template.d.ts +2 -2
- package/dist/components/templates/text-template/text-template.d.ts.map +1 -1
- package/dist/components/templates/time/time.d.ts.map +1 -1
- package/dist/context/asgard-app-initialization-context.d.ts +54 -0
- package/dist/context/asgard-app-initialization-context.d.ts.map +1 -0
- package/dist/context/asgard-service-context.d.ts +22 -13
- package/dist/context/asgard-service-context.d.ts.map +1 -1
- package/dist/context/asgard-template-context.d.ts +28 -0
- package/dist/context/asgard-template-context.d.ts.map +1 -0
- package/dist/context/asgard-theme-context.d.ts +127 -0
- package/dist/context/asgard-theme-context.d.ts.map +1 -0
- package/dist/context/index.d.ts +5 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/use-asgard-service-client.d.ts +1 -1
- package/dist/hooks/use-asgard-service-client.d.ts.map +1 -1
- package/dist/hooks/use-channel.d.ts +15 -10
- package/dist/hooks/use-channel.d.ts.map +1 -1
- package/dist/hooks/use-deep-compare-memo.d.ts +7 -0
- package/dist/hooks/use-deep-compare-memo.d.ts.map +1 -0
- package/dist/hooks/use-is-on-screen-keyboard-open.d.ts +2 -0
- package/dist/hooks/use-is-on-screen-keyboard-open.d.ts.map +1 -0
- package/dist/hooks/use-on-screen-keyboard-scroll-fix.d.ts +2 -0
- package/dist/hooks/use-on-screen-keyboard-scroll-fix.d.ts.map +1 -0
- package/dist/hooks/use-prevent-over-scrolling.d.ts +4 -0
- package/dist/hooks/use-prevent-over-scrolling.d.ts.map +1 -0
- package/dist/hooks/use-react-markdown-renderer.d.ts +11 -0
- package/dist/hooks/use-react-markdown-renderer.d.ts.map +1 -0
- package/dist/hooks/use-resize-observer.d.ts +9 -0
- package/dist/hooks/use-resize-observer.d.ts.map +1 -0
- package/dist/hooks/use-update-vh.d.ts +4 -0
- package/dist/hooks/use-update-vh.d.ts.map +1 -0
- package/dist/hooks/use-viewport-size.d.ts +6 -0
- package/dist/hooks/use-viewport-size.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +77820 -503
- package/dist/models/bot-provider.d.ts +26 -0
- package/dist/models/bot-provider.d.ts.map +1 -0
- package/dist/style.css +1 -1
- package/dist/test-setup.d.ts +2 -0
- package/dist/test-setup.d.ts.map +1 -0
- package/dist/utils/deep-merge.d.ts +3 -0
- package/dist/utils/deep-merge.d.ts.map +1 -0
- package/dist/utils/extractors.d.ts +2 -0
- package/dist/utils/extractors.d.ts.map +1 -0
- package/dist/utils/is.d.ts +8 -0
- package/dist/utils/is.d.ts.map +1 -0
- package/dist/utils/selectors.d.ts +4 -0
- package/dist/utils/selectors.d.ts.map +1 -0
- package/dist/utils/uri-validation.d.ts +20 -0
- package/dist/utils/uri-validation.d.ts.map +1 -0
- package/eslint.config.cjs +12 -0
- package/package.json +39 -3
- package/src/components/chatbot/chatbot-body/chatbot-body.module.scss +13 -0
- package/src/components/chatbot/chatbot-body/chatbot-body.tsx +45 -0
- package/src/components/chatbot/chatbot-body/conversation-message-renderer.tsx +55 -0
- package/src/components/chatbot/chatbot-body/index.ts +1 -0
- package/src/components/chatbot/chatbot-container/chatbot-container.module.scss +41 -0
- package/src/components/chatbot/chatbot-container/chatbot-container.tsx +49 -0
- package/src/components/chatbot/chatbot-container/chatbot-full-screen-container.tsx +54 -0
- package/src/components/chatbot/chatbot-footer/chatbot-footer.module.scss +67 -0
- package/src/components/chatbot/chatbot-footer/chatbot-footer.tsx +140 -0
- package/src/components/chatbot/chatbot-footer/index.ts +1 -0
- package/src/components/chatbot/chatbot-footer/speech-input-button.tsx +132 -0
- package/src/components/chatbot/chatbot-header/chatbot-header.module.scss +48 -0
- package/src/components/chatbot/chatbot-header/chatbot-header.tsx +98 -0
- package/src/components/chatbot/chatbot-header/index.ts +1 -0
- package/src/components/chatbot/chatbot.spec.tsx +8 -0
- package/src/components/chatbot/chatbot.tsx +121 -0
- package/src/components/chatbot/profile-icon.tsx +26 -0
- package/src/components/index.ts +2 -0
- package/src/components/templates/avatar/avatar.module.scss +6 -0
- package/src/components/templates/avatar/avatar.tsx +28 -0
- package/src/components/templates/avatar/index.ts +1 -0
- package/src/components/templates/button-template/button-template.module.scss +0 -0
- package/src/components/templates/button-template/button-template.tsx +45 -0
- package/src/components/templates/button-template/card.module.scss +58 -0
- package/src/components/templates/button-template/card.spec.tsx +213 -0
- package/src/components/templates/button-template/card.tsx +123 -0
- package/src/components/templates/button-template/index.ts +1 -0
- package/src/components/templates/carousel-template/carousel-template.module.scss +15 -0
- package/src/components/templates/carousel-template/carousel-template.tsx +49 -0
- package/src/components/templates/carousel-template/index.ts +1 -0
- package/src/components/templates/chart-template/chart-template.module.scss +52 -0
- package/src/components/templates/chart-template/chart-template.tsx +75 -0
- package/src/components/templates/chart-template/index.ts +1 -0
- package/src/components/templates/hint-template/hint-template.module.scss +39 -0
- package/src/components/templates/hint-template/hint-template.tsx +71 -0
- package/src/components/templates/hint-template/index.ts +1 -0
- package/src/components/templates/image-template/image-template.module.scss +67 -0
- package/src/components/templates/image-template/image-template.tsx +58 -0
- package/src/components/templates/image-template/index.ts +1 -0
- package/src/components/templates/index.ts +10 -0
- package/src/components/templates/quick-replies/index.ts +1 -0
- package/src/components/templates/quick-replies/quick-replies.module.scss +16 -0
- package/src/components/templates/quick-replies/quick-replies.tsx +44 -0
- package/src/components/templates/template-box/index.ts +2 -0
- package/src/components/templates/template-box/template-box-content.module.scss +13 -0
- package/src/components/templates/template-box/template-box-content.tsx +30 -0
- package/src/components/templates/template-box/template-box.module.scss +19 -0
- package/src/components/templates/template-box/template-box.tsx +48 -0
- package/src/components/templates/text-template/bot-typing-box.tsx +81 -0
- package/src/components/templates/text-template/bot-typing-placeholder.tsx +28 -0
- package/src/components/templates/text-template/index.ts +3 -0
- package/src/components/templates/text-template/text-template.module.scss +131 -0
- package/src/components/templates/text-template/text-template.tsx +90 -0
- package/src/components/templates/text-template/use-react-markdown-renderer.spec.tsx +758 -0
- package/src/components/templates/time/index.ts +1 -0
- package/src/components/templates/time/time.module.scss +6 -0
- package/src/components/templates/time/time.tsx +34 -0
- package/src/context/asgard-app-initialization-context.tsx +154 -0
- package/src/context/asgard-service-context.tsx +145 -0
- package/src/context/asgard-template-context.tsx +83 -0
- package/src/context/asgard-theme-context.tsx +401 -0
- package/src/context/index.ts +4 -0
- package/src/hooks/index.ts +11 -0
- package/src/hooks/use-asgard-service-client.ts +68 -0
- package/src/hooks/use-channel.ts +154 -0
- package/src/hooks/use-debounce.ts +18 -0
- package/src/hooks/use-deep-compare-memo.ts +19 -0
- package/src/hooks/use-is-on-screen-keyboard-open.ts +43 -0
- package/src/hooks/use-on-screen-keyboard-scroll-fix.ts +15 -0
- package/src/hooks/use-prevent-over-scrolling.ts +77 -0
- package/src/hooks/use-react-markdown-renderer.tsx +266 -0
- package/src/hooks/use-resize-observer.tsx +27 -0
- package/src/hooks/use-update-vh.ts +30 -0
- package/src/hooks/use-viewport-size.ts +51 -0
- package/src/icons/add_a_photo.svg +3 -0
- package/src/icons/bot.svg +14 -0
- package/src/icons/close.svg +3 -0
- package/src/icons/distance.svg +3 -0
- package/src/icons/mic.svg +3 -0
- package/src/icons/photo_library.svg +3 -0
- package/src/icons/profile.svg +28 -0
- package/src/icons/refresh.svg +3 -0
- package/src/icons/send.svg +3 -0
- package/src/icons/stop.svg +22 -0
- package/src/icons/volume_up.svg +3 -0
- package/src/index.ts +4 -0
- package/src/models/bot-provider.ts +108 -0
- package/src/styles/_index.scss +1 -0
- package/src/styles/_styles.scss +11 -0
- package/src/styles/colors/_colors.scss +10 -0
- package/src/styles/colors/_index.scss +1 -0
- package/src/styles/colors/_variables.scss +72 -0
- package/src/styles/palette/_index.scss +1 -0
- package/src/styles/palette/_palette.scss +42 -0
- package/src/styles/palette/_variables.scss +40 -0
- package/src/styles/radius/_index.scss +1 -0
- package/src/styles/radius/_radius.scss +8 -0
- package/src/styles/radius/_variables.scss +12 -0
- package/src/styles/spacing/_index.scss +1 -0
- package/src/styles/spacing/_spacing.scss +8 -0
- package/src/styles/spacing/_variables.scss +13 -0
- package/src/styles/utils/_index.scss +1 -0
- package/src/styles/utils/_map.scss +22 -0
- package/src/test-setup.ts +1 -0
- package/src/utils/deep-merge.ts +26 -0
- package/src/utils/extractors.ts +20 -0
- package/src/utils/format-time.ts +8 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/is.ts +72 -0
- package/src/utils/selectors.ts +7 -0
- package/src/utils/uri-validation.spec.ts +208 -0
- package/src/utils/uri-validation.ts +103 -0
- package/tsconfig.json +16 -0
- package/tsconfig.lib.json +63 -0
- package/tsconfig.spec.json +36 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/vite.config.ts +63 -0
- package/dist/components/chatbot/chatbot-body.d.ts.map +0 -1
- package/dist/components/chatbot/chatbot-footer.d.ts.map +0 -1
- package/dist/components/chatbot/chatbot-header.d.ts +0 -8
- package/dist/components/chatbot/chatbot-header.d.ts.map +0 -1
- package/dist/components/conversation-message-renderer/conversation-message-renderer.d.ts.map +0 -1
- package/dist/components/conversation-message-renderer/index.d.ts +0 -2
- package/dist/components/conversation-message-renderer/index.d.ts.map +0 -1
- package/dist/components/templates/bot-typing-box/bot-typing-box.d.ts.map +0 -1
- package/dist/components/templates/bot-typing-box/index.d.ts +0 -2
- package/dist/components/templates/bot-typing-box/index.d.ts.map +0 -1
- package/dist/components/templates/bot-typing-box/resize-observer-box.d.ts +0 -9
- package/dist/components/templates/bot-typing-box/resize-observer-box.d.ts.map +0 -1
- /package/dist/components/chatbot/{chatbot-body.d.ts → chatbot-body/chatbot-body.d.ts} +0 -0
- /package/dist/components/chatbot/{chatbot-footer.d.ts → chatbot-footer/chatbot-footer.d.ts} +0 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
+
import { render, screen, fireEvent } from '@testing-library/react';
|
|
3
|
+
import { Card } from './card';
|
|
4
|
+
import { MessageTemplateType } from '@asgard-js/core';
|
|
5
|
+
import { useAsgardContext } from '../../../context/asgard-service-context';
|
|
6
|
+
import { useAsgardTemplateContext } from '../../../context/asgard-template-context';
|
|
7
|
+
import * as uriValidation from '../../../utils/uri-validation';
|
|
8
|
+
|
|
9
|
+
// Mock the contexts
|
|
10
|
+
vi.mock('../../../context/asgard-service-context');
|
|
11
|
+
vi.mock('../../../context/asgard-template-context');
|
|
12
|
+
|
|
13
|
+
// Mock the URI validation utility
|
|
14
|
+
vi.mock('../../../utils/uri-validation');
|
|
15
|
+
|
|
16
|
+
const mockUseAsgardContext = vi.mocked(useAsgardContext);
|
|
17
|
+
const mockUseAsgardTemplateContext = vi.mocked(useAsgardTemplateContext);
|
|
18
|
+
const mockSafeWindowOpen = vi.mocked(uriValidation.safeWindowOpen);
|
|
19
|
+
|
|
20
|
+
describe('Card Component - Security Tests', () => {
|
|
21
|
+
const mockSendMessage = vi.fn();
|
|
22
|
+
const mockOnTemplateBtnClick = vi.fn();
|
|
23
|
+
const defaultLinkTarget = '_blank';
|
|
24
|
+
|
|
25
|
+
// Define malicious URI as variable to avoid ESLint script URL warning
|
|
26
|
+
// eslint-disable-next-line no-script-url
|
|
27
|
+
const maliciousJsUri = 'javascript:alert("xss")';
|
|
28
|
+
|
|
29
|
+
const baseTemplate = {
|
|
30
|
+
type: MessageTemplateType.BUTTON,
|
|
31
|
+
title: 'Test Card',
|
|
32
|
+
text: 'Test description',
|
|
33
|
+
thumbnailImageUrl: 'https://example.com/image.jpg',
|
|
34
|
+
imageAspectRatio: 'rectangle' as const,
|
|
35
|
+
imageSize: 'cover' as const,
|
|
36
|
+
imageBackgroundColor: '#ffffff',
|
|
37
|
+
defaultAction: {
|
|
38
|
+
type: 'message' as const,
|
|
39
|
+
text: 'Default action',
|
|
40
|
+
},
|
|
41
|
+
quickReplies: [],
|
|
42
|
+
buttons: [
|
|
43
|
+
{
|
|
44
|
+
label: 'Safe Link',
|
|
45
|
+
action: {
|
|
46
|
+
type: 'uri' as const,
|
|
47
|
+
uri: 'https://example.com',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
label: 'Malicious Link',
|
|
52
|
+
action: {
|
|
53
|
+
type: 'uri' as const,
|
|
54
|
+
uri: maliciousJsUri,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
mockUseAsgardContext.mockReturnValue({
|
|
62
|
+
sendMessage: mockSendMessage,
|
|
63
|
+
client: null,
|
|
64
|
+
isOpen: false,
|
|
65
|
+
isResetting: false,
|
|
66
|
+
isConnecting: false,
|
|
67
|
+
conversation: null,
|
|
68
|
+
resetChannel: vi.fn(),
|
|
69
|
+
closeChannel: vi.fn(),
|
|
70
|
+
avatar: null,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
mockUseAsgardTemplateContext.mockReturnValue({
|
|
74
|
+
onTemplateBtnClick: mockOnTemplateBtnClick,
|
|
75
|
+
defaultLinkTarget,
|
|
76
|
+
onErrorClick: undefined,
|
|
77
|
+
errorMessageRenderer: undefined,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
mockSafeWindowOpen.mockReturnValue(null);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
afterEach(() => {
|
|
84
|
+
vi.clearAllMocks();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('URI security validation', () => {
|
|
88
|
+
it('should call safeWindowOpen for URI actions instead of window.open directly', () => {
|
|
89
|
+
render(<Card template={baseTemplate} />);
|
|
90
|
+
|
|
91
|
+
const safeButton = screen.getByText('Safe Link');
|
|
92
|
+
fireEvent.click(safeButton);
|
|
93
|
+
|
|
94
|
+
expect(mockSafeWindowOpen).toHaveBeenCalledWith(
|
|
95
|
+
'https://example.com',
|
|
96
|
+
'_blank'
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should call safeWindowOpen for malicious URIs (letting validation utility handle security)', () => {
|
|
101
|
+
render(<Card template={baseTemplate} />);
|
|
102
|
+
|
|
103
|
+
const maliciousButton = screen.getByText('Malicious Link');
|
|
104
|
+
fireEvent.click(maliciousButton);
|
|
105
|
+
|
|
106
|
+
expect(mockSafeWindowOpen).toHaveBeenCalledWith(
|
|
107
|
+
maliciousJsUri,
|
|
108
|
+
'_blank'
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should use action target if provided', () => {
|
|
113
|
+
const templateWithTarget = {
|
|
114
|
+
...baseTemplate,
|
|
115
|
+
buttons: [
|
|
116
|
+
{
|
|
117
|
+
label: 'Link with Target',
|
|
118
|
+
action: {
|
|
119
|
+
type: 'uri' as const,
|
|
120
|
+
uri: 'https://example.com',
|
|
121
|
+
target: '_self' as const,
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
render(<Card template={templateWithTarget} />);
|
|
128
|
+
|
|
129
|
+
const button = screen.getByText('Link with Target');
|
|
130
|
+
fireEvent.click(button);
|
|
131
|
+
|
|
132
|
+
expect(mockSafeWindowOpen).toHaveBeenCalledWith(
|
|
133
|
+
'https://example.com',
|
|
134
|
+
'_self'
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should fallback to defaultLinkTarget when no action target', () => {
|
|
139
|
+
mockUseAsgardTemplateContext.mockReturnValue({
|
|
140
|
+
onTemplateBtnClick: mockOnTemplateBtnClick,
|
|
141
|
+
defaultLinkTarget: '_parent',
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
render(<Card template={baseTemplate} />);
|
|
145
|
+
|
|
146
|
+
const safeButton = screen.getByText('Safe Link');
|
|
147
|
+
fireEvent.click(safeButton);
|
|
148
|
+
|
|
149
|
+
expect(mockSafeWindowOpen).toHaveBeenCalledWith(
|
|
150
|
+
'https://example.com',
|
|
151
|
+
'_parent'
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should fallback to _blank when no action target or defaultLinkTarget', () => {
|
|
156
|
+
mockUseAsgardTemplateContext.mockReturnValue({
|
|
157
|
+
onTemplateBtnClick: mockOnTemplateBtnClick,
|
|
158
|
+
defaultLinkTarget: undefined,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
render(<Card template={baseTemplate} />);
|
|
162
|
+
|
|
163
|
+
const safeButton = screen.getByText('Safe Link');
|
|
164
|
+
fireEvent.click(safeButton);
|
|
165
|
+
|
|
166
|
+
expect(mockSafeWindowOpen).toHaveBeenCalledWith(
|
|
167
|
+
'https://example.com',
|
|
168
|
+
'_blank'
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should handle uppercase URI action type', () => {
|
|
173
|
+
const templateWithUppercase = {
|
|
174
|
+
...baseTemplate,
|
|
175
|
+
buttons: [
|
|
176
|
+
{
|
|
177
|
+
label: 'Uppercase URI',
|
|
178
|
+
action: {
|
|
179
|
+
type: 'URI' as const,
|
|
180
|
+
uri: 'https://example.com',
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
],
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
render(<Card template={templateWithUppercase} />);
|
|
187
|
+
|
|
188
|
+
const button = screen.getByText('Uppercase URI');
|
|
189
|
+
fireEvent.click(button);
|
|
190
|
+
|
|
191
|
+
expect(mockSafeWindowOpen).toHaveBeenCalledWith(
|
|
192
|
+
'https://example.com',
|
|
193
|
+
'_blank'
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe('basic rendering', () => {
|
|
199
|
+
it('should render card with title and description', () => {
|
|
200
|
+
render(<Card template={baseTemplate} />);
|
|
201
|
+
|
|
202
|
+
expect(screen.getByText('Test Card')).toBeDefined();
|
|
203
|
+
expect(screen.getByText('Test description')).toBeDefined();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('should render buttons', () => {
|
|
207
|
+
render(<Card template={baseTemplate} />);
|
|
208
|
+
|
|
209
|
+
expect(screen.getByText('Safe Link')).toBeDefined();
|
|
210
|
+
expect(screen.getByText('Malicious Link')).toBeDefined();
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MouseEventHandler,
|
|
3
|
+
ReactNode,
|
|
4
|
+
useCallback,
|
|
5
|
+
useMemo,
|
|
6
|
+
CSSProperties,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import styles from './card.module.scss';
|
|
9
|
+
import {
|
|
10
|
+
ButtonAction,
|
|
11
|
+
ButtonMessageTemplate,
|
|
12
|
+
CarouselMessageTemplate,
|
|
13
|
+
} from '@asgard-js/core';
|
|
14
|
+
import { useAsgardContext } from '../../../context/asgard-service-context';
|
|
15
|
+
import { useAsgardTemplateContext } from '../../../context/asgard-template-context';
|
|
16
|
+
import { safeWindowOpen } from '../../../utils/uri-validation';
|
|
17
|
+
import clsx from 'clsx';
|
|
18
|
+
|
|
19
|
+
interface CardProps {
|
|
20
|
+
template: ButtonMessageTemplate | CarouselMessageTemplate['columns'][number];
|
|
21
|
+
customStyle?: {
|
|
22
|
+
style?: CSSProperties;
|
|
23
|
+
button?: {
|
|
24
|
+
style?: CSSProperties;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function Card(props: CardProps): ReactNode {
|
|
30
|
+
const { template, customStyle } = props;
|
|
31
|
+
|
|
32
|
+
const { sendMessage } = useAsgardContext();
|
|
33
|
+
const { onTemplateBtnClick, defaultLinkTarget } = useAsgardTemplateContext();
|
|
34
|
+
|
|
35
|
+
const src = useMemo(() => {
|
|
36
|
+
return (
|
|
37
|
+
template?.thumbnailImageUrl
|
|
38
|
+
?.replace(/^http:/, '')
|
|
39
|
+
.replace(/^https:/, '') ||
|
|
40
|
+
'https://via.assets.so/img.jpg?w=200&h=270&tc=white&bg=#eeeeee'
|
|
41
|
+
);
|
|
42
|
+
}, [template]);
|
|
43
|
+
|
|
44
|
+
const aspectRatio = useMemo(() => {
|
|
45
|
+
switch (template?.imageAspectRatio) {
|
|
46
|
+
case 'square':
|
|
47
|
+
return '1 / 1';
|
|
48
|
+
case 'rectangle':
|
|
49
|
+
default:
|
|
50
|
+
return '1.51 / 1';
|
|
51
|
+
}
|
|
52
|
+
}, [template]);
|
|
53
|
+
|
|
54
|
+
const handleClick = useCallback(
|
|
55
|
+
(action: ButtonAction): MouseEventHandler<HTMLButtonElement> => {
|
|
56
|
+
return function clickHandler() {
|
|
57
|
+
switch (action.type) {
|
|
58
|
+
case 'message':
|
|
59
|
+
case 'MESSAGE':
|
|
60
|
+
sendMessage?.({ text: action.text });
|
|
61
|
+
|
|
62
|
+
return;
|
|
63
|
+
case 'uri':
|
|
64
|
+
case 'URI':
|
|
65
|
+
safeWindowOpen(
|
|
66
|
+
action.uri,
|
|
67
|
+
action.target || defaultLinkTarget || '_blank'
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
return;
|
|
71
|
+
case 'emit':
|
|
72
|
+
case 'EMIT':
|
|
73
|
+
onTemplateBtnClick?.(action.payload, {
|
|
74
|
+
sse: {
|
|
75
|
+
sendMessage: (payload) => {
|
|
76
|
+
sendMessage?.(payload);
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
[sendMessage, onTemplateBtnClick, defaultLinkTarget]
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<div
|
|
90
|
+
className={clsx('asgard-card', styles.card_root)}
|
|
91
|
+
style={customStyle?.style}
|
|
92
|
+
>
|
|
93
|
+
{template?.thumbnailImageUrl && (
|
|
94
|
+
<img
|
|
95
|
+
alt={template?.title}
|
|
96
|
+
src={src}
|
|
97
|
+
style={{
|
|
98
|
+
display: 'block',
|
|
99
|
+
width: '100%',
|
|
100
|
+
maxHeight: '170px',
|
|
101
|
+
objectFit: template?.imageSize,
|
|
102
|
+
aspectRatio,
|
|
103
|
+
}}
|
|
104
|
+
/>
|
|
105
|
+
)}
|
|
106
|
+
<div className={styles.card_content}>
|
|
107
|
+
<h5 className={styles.card_title}>{template?.title}</h5>
|
|
108
|
+
<div className={styles.card_description}>{template?.text}</div>
|
|
109
|
+
<div className={styles.card_actions}>
|
|
110
|
+
{template?.buttons?.map((btn: { label: string; action: ButtonAction }, index: number) => (
|
|
111
|
+
<button
|
|
112
|
+
key={index}
|
|
113
|
+
onClick={handleClick(btn.action)}
|
|
114
|
+
style={customStyle?.button?.style}
|
|
115
|
+
>
|
|
116
|
+
{btn.label}
|
|
117
|
+
</button>
|
|
118
|
+
))}
|
|
119
|
+
</div>
|
|
120
|
+
</div>
|
|
121
|
+
</div>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './button-template';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { TemplateBox, TemplateBoxContent } from '../template-box';
|
|
3
|
+
import { Avatar } from '../avatar';
|
|
4
|
+
import styles from './carousel-template.module.scss';
|
|
5
|
+
import { Card } from '../button-template/card';
|
|
6
|
+
import {
|
|
7
|
+
CarouselMessageTemplate,
|
|
8
|
+
ConversationBotMessage,
|
|
9
|
+
ButtonMessageTemplate,
|
|
10
|
+
} from '@asgard-js/core';
|
|
11
|
+
import { Time } from '../time';
|
|
12
|
+
import { useAsgardContext } from '../../../context/asgard-service-context';
|
|
13
|
+
import { useAsgardThemeContext } from '../../../context/asgard-theme-context';
|
|
14
|
+
|
|
15
|
+
interface CarouselTemplateProps {
|
|
16
|
+
message: ConversationBotMessage;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function CarouselTemplate(props: CarouselTemplateProps): ReactNode {
|
|
20
|
+
const { message } = props;
|
|
21
|
+
|
|
22
|
+
const { template: themeTemplate } = useAsgardThemeContext();
|
|
23
|
+
const { avatar } = useAsgardContext();
|
|
24
|
+
|
|
25
|
+
const template = message.message.template as CarouselMessageTemplate;
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<TemplateBox
|
|
29
|
+
className="asgard-carousel-template"
|
|
30
|
+
type="bot"
|
|
31
|
+
direction="vertical"
|
|
32
|
+
style={themeTemplate?.CarouselMessageTemplate?.style}
|
|
33
|
+
>
|
|
34
|
+
<Avatar avatar={avatar} />
|
|
35
|
+
<TemplateBoxContent quickReplies={template.quickReplies}>
|
|
36
|
+
<div className={styles.carousel_root}>
|
|
37
|
+
{template.columns?.map((column: Omit<ButtonMessageTemplate, 'type' | 'quickReplies'>, index: number) => (
|
|
38
|
+
<Card
|
|
39
|
+
key={index}
|
|
40
|
+
template={column}
|
|
41
|
+
customStyle={themeTemplate?.CarouselMessageTemplate?.card}
|
|
42
|
+
/>
|
|
43
|
+
))}
|
|
44
|
+
</div>
|
|
45
|
+
</TemplateBoxContent>
|
|
46
|
+
<Time className={styles.carousel_time} time={message.time} />
|
|
47
|
+
</TemplateBox>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './carousel-template';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
.text {
|
|
2
|
+
display: inline-block;
|
|
3
|
+
padding: 8px 12px;
|
|
4
|
+
border-radius: 8px;
|
|
5
|
+
color: white;
|
|
6
|
+
word-break: break-all;
|
|
7
|
+
|
|
8
|
+
> span {
|
|
9
|
+
word-break: break-all;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.md_container {
|
|
13
|
+
display: inline-block;
|
|
14
|
+
|
|
15
|
+
p {
|
|
16
|
+
margin: 0;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.text--user {
|
|
22
|
+
max-width: 75%;
|
|
23
|
+
background: #4767eb;
|
|
24
|
+
border-top-right-radius: 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.text--bot {
|
|
28
|
+
max-width: 70%;
|
|
29
|
+
background: #585858;
|
|
30
|
+
border-top-left-radius: 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.chart_time {
|
|
34
|
+
justify-content: flex-end;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.quick_replies_box {
|
|
38
|
+
display: flex;
|
|
39
|
+
flex-wrap: wrap;
|
|
40
|
+
gap: 8px;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.quick_reply {
|
|
44
|
+
font: inherit;
|
|
45
|
+
font-size: 13px;
|
|
46
|
+
padding: 4px 8px;
|
|
47
|
+
border-radius: 8px;
|
|
48
|
+
border: 1px solid #434343;
|
|
49
|
+
background: rgba(88, 88, 88, 0.2);
|
|
50
|
+
color: white;
|
|
51
|
+
cursor: pointer;
|
|
52
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ReactNode, useMemo, useState, CSSProperties } from 'react';
|
|
2
|
+
import { TemplateBox, TemplateBoxContent } from '../template-box';
|
|
3
|
+
import { Avatar } from '../avatar';
|
|
4
|
+
import { ConversationBotMessage, ChartMessageTemplate } from '@asgard-js/core';
|
|
5
|
+
import { Time } from '../time';
|
|
6
|
+
import { useAsgardContext } from '../../../context/asgard-service-context';
|
|
7
|
+
import { VegaLite, VisualizationSpec } from 'react-vega';
|
|
8
|
+
import clsx from 'clsx';
|
|
9
|
+
import classes from './chart-template.module.scss';
|
|
10
|
+
import { useAsgardThemeContext } from '../../../context/asgard-theme-context';
|
|
11
|
+
|
|
12
|
+
interface ChartTemplateProps {
|
|
13
|
+
message: ConversationBotMessage;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function ChartTemplate(props: ChartTemplateProps): ReactNode {
|
|
17
|
+
const { message } = props;
|
|
18
|
+
const template = message.message.template as ChartMessageTemplate;
|
|
19
|
+
|
|
20
|
+
const { template: themeTemplate, botMessage } = useAsgardThemeContext();
|
|
21
|
+
const { avatar } = useAsgardContext();
|
|
22
|
+
|
|
23
|
+
const [option, setOption] = useState(
|
|
24
|
+
template?.defaultChart ?? template?.chartOptions?.[0]?.type
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const options = useMemo(() => template.chartOptions, [template]);
|
|
28
|
+
|
|
29
|
+
const spec = useMemo(
|
|
30
|
+
() =>
|
|
31
|
+
(template?.chartOptions?.find((item: { type: string; title: string; spec: Record<string, unknown> }) => item.type === option)?.spec ??
|
|
32
|
+
options[0].spec) as VisualizationSpec,
|
|
33
|
+
[option, template.chartOptions, options]
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const styles = useMemo<CSSProperties>(
|
|
37
|
+
() => ({
|
|
38
|
+
color: botMessage?.color,
|
|
39
|
+
backgroundColor: botMessage?.backgroundColor,
|
|
40
|
+
}),
|
|
41
|
+
[botMessage]
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<TemplateBox
|
|
46
|
+
className="asgard-chart-template"
|
|
47
|
+
type="bot"
|
|
48
|
+
direction="vertical"
|
|
49
|
+
style={themeTemplate?.ChartMessageTemplate?.style}
|
|
50
|
+
>
|
|
51
|
+
<Avatar avatar={avatar} />
|
|
52
|
+
<div className={clsx(classes.text, classes['text--bot'])} style={styles}>
|
|
53
|
+
<div>{template.title}</div>
|
|
54
|
+
<div>{template.text}</div>
|
|
55
|
+
</div>
|
|
56
|
+
{options.length > 1 && (
|
|
57
|
+
<div className={classes.quick_replies_box}>
|
|
58
|
+
{options.map((option: { type: string; title: string; spec: Record<string, unknown> }) => (
|
|
59
|
+
<button
|
|
60
|
+
key={option.type}
|
|
61
|
+
className={classes.quick_reply}
|
|
62
|
+
onClick={() => setOption(option.type)}
|
|
63
|
+
>
|
|
64
|
+
{option.title}
|
|
65
|
+
</button>
|
|
66
|
+
))}
|
|
67
|
+
</div>
|
|
68
|
+
)}
|
|
69
|
+
<TemplateBoxContent quickReplies={template?.quickReplies}>
|
|
70
|
+
<VegaLite spec={spec} />
|
|
71
|
+
</TemplateBoxContent>
|
|
72
|
+
<Time className={classes.chart_time} time={message.time} />
|
|
73
|
+
</TemplateBox>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './chart-template';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
.hint_root {
|
|
2
|
+
margin: 0 auto;
|
|
3
|
+
display: flex;
|
|
4
|
+
flex-direction: column;
|
|
5
|
+
align-items: center;
|
|
6
|
+
padding: 4px 12px;
|
|
7
|
+
gap: 4px;
|
|
8
|
+
border-radius: 2px;
|
|
9
|
+
background: rgba(88, 88, 88, 0.2);
|
|
10
|
+
font-size: 13px;
|
|
11
|
+
color: white;
|
|
12
|
+
word-break: break-all;
|
|
13
|
+
|
|
14
|
+
&.hint_root__error {
|
|
15
|
+
color: var(--asg-color-error);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.error_hint_title {
|
|
19
|
+
display: flex;
|
|
20
|
+
align-items: center;
|
|
21
|
+
gap: var(--asg-spacing-2);
|
|
22
|
+
color: var(--asg-color-error);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.error_hint_message {
|
|
26
|
+
font-size: 12px;
|
|
27
|
+
color: white;
|
|
28
|
+
|
|
29
|
+
> span {
|
|
30
|
+
border-bottom: 1px solid white;
|
|
31
|
+
cursor: pointer;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.time {
|
|
37
|
+
font-size: 12px;
|
|
38
|
+
color: rgba(140, 140, 140, 1);
|
|
39
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ReactNode, useCallback } from 'react';
|
|
2
|
+
import classes from './hint-template.module.scss';
|
|
3
|
+
import { formatTime } from '../../../utils';
|
|
4
|
+
import {
|
|
5
|
+
ConversationErrorMessage,
|
|
6
|
+
ConversationMessage,
|
|
7
|
+
MessageTemplateType,
|
|
8
|
+
} from '@asgard-js/core';
|
|
9
|
+
import { useAsgardTemplateContext, useAsgardThemeContext } from '../../../context';
|
|
10
|
+
import clsx from 'clsx';
|
|
11
|
+
|
|
12
|
+
interface HintTemplateProps {
|
|
13
|
+
message: ConversationMessage;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function HintTemplate(props: HintTemplateProps): ReactNode {
|
|
17
|
+
const { message } = props;
|
|
18
|
+
|
|
19
|
+
const { template: themeTemplate } = useAsgardThemeContext();
|
|
20
|
+
const { onErrorClick, errorMessageRenderer } = useAsgardTemplateContext();
|
|
21
|
+
|
|
22
|
+
const onErrorHintClick = useCallback(() => {
|
|
23
|
+
onErrorClick?.(message as ConversationErrorMessage);
|
|
24
|
+
}, [message, onErrorClick]);
|
|
25
|
+
|
|
26
|
+
if (message.type === 'user') return null;
|
|
27
|
+
|
|
28
|
+
if (message.type === 'error')
|
|
29
|
+
return (
|
|
30
|
+
<div
|
|
31
|
+
className={clsx(
|
|
32
|
+
'asgard-hint-template asgard-hint-template--error',
|
|
33
|
+
classes.hint_root
|
|
34
|
+
)}
|
|
35
|
+
>
|
|
36
|
+
{errorMessageRenderer?.(message) ?? (
|
|
37
|
+
<>
|
|
38
|
+
<div className={classes.error_hint_title}>
|
|
39
|
+
<span className={classes.time}>{formatTime(message.time)}</span>
|
|
40
|
+
<span>Unexpected error</span>
|
|
41
|
+
</div>
|
|
42
|
+
{onErrorClick && (
|
|
43
|
+
<div
|
|
44
|
+
className={classes.error_hint_message}
|
|
45
|
+
onClick={onErrorHintClick}
|
|
46
|
+
>
|
|
47
|
+
Click <span>here</span> to view the report.
|
|
48
|
+
</div>
|
|
49
|
+
)}
|
|
50
|
+
</>
|
|
51
|
+
)}
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const template = message.message.template;
|
|
56
|
+
|
|
57
|
+
if (template.type !== MessageTemplateType.HINT) return null;
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<div
|
|
61
|
+
className={clsx(
|
|
62
|
+
'asgard-hint-template asgard-hint-template--hint',
|
|
63
|
+
classes.hint_root
|
|
64
|
+
)}
|
|
65
|
+
style={themeTemplate?.HintMessageTemplate?.style}
|
|
66
|
+
>
|
|
67
|
+
<div className={classes.time}>{formatTime(message.time)}</div>
|
|
68
|
+
{template.text}
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './hint-template';
|