@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,132 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Dispatch,
|
|
3
|
+
MouseEventHandler,
|
|
4
|
+
ReactNode,
|
|
5
|
+
SetStateAction,
|
|
6
|
+
TouchEventHandler,
|
|
7
|
+
useCallback,
|
|
8
|
+
useEffect,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
CSSProperties,
|
|
12
|
+
} from 'react';
|
|
13
|
+
import MicSvg from '../../../icons/mic.svg?react';
|
|
14
|
+
import StopSvg from '../../../icons/stop.svg?react';
|
|
15
|
+
|
|
16
|
+
interface SpeechInputButtonProps {
|
|
17
|
+
setValue: Dispatch<SetStateAction<string>>;
|
|
18
|
+
className?: string;
|
|
19
|
+
style?: CSSProperties;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function SpeechInputButton(props: SpeechInputButtonProps): ReactNode {
|
|
23
|
+
const { setValue, className, style } = props;
|
|
24
|
+
|
|
25
|
+
const [listening, setListening] = useState(false);
|
|
26
|
+
const recognitionRef = useRef<SpeechRecognition | null>(null);
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
const SpeechRecognition =
|
|
30
|
+
window.SpeechRecognition || window.webkitSpeechRecognition;
|
|
31
|
+
|
|
32
|
+
if (!SpeechRecognition) return;
|
|
33
|
+
|
|
34
|
+
const recognition = new SpeechRecognition();
|
|
35
|
+
recognition.lang = 'zh-TW';
|
|
36
|
+
recognition.continuous = true;
|
|
37
|
+
recognition.interimResults = true;
|
|
38
|
+
|
|
39
|
+
recognition.onresult = (event: SpeechRecognitionEvent): void => {
|
|
40
|
+
for (let i = event.resultIndex; i < event.results.length; i++) {
|
|
41
|
+
if (event.results[i].isFinal) {
|
|
42
|
+
setValue((prev) => prev + event.results[i][0].transcript);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
recognition.onerror = (event: SpeechRecognitionErrorEvent): void => {
|
|
48
|
+
alert(`語音識別錯誤: ${JSON.stringify(event.error)}`);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
recognition.onend = (): void => {
|
|
52
|
+
setListening(false);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
recognitionRef.current = recognition;
|
|
56
|
+
}, [setValue]);
|
|
57
|
+
|
|
58
|
+
const startListening = useCallback(() => {
|
|
59
|
+
if (!recognitionRef.current) {
|
|
60
|
+
alert('無法開始辨識: 語音識別器未初始化,請檢查是否有授權使用麥克風');
|
|
61
|
+
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
recognitionRef.current.start();
|
|
67
|
+
setListening(true);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
alert(`無法開始辨識: ${JSON.stringify(error)}`);
|
|
70
|
+
}
|
|
71
|
+
}, []);
|
|
72
|
+
|
|
73
|
+
const stopListening = useCallback(() => {
|
|
74
|
+
if (!recognitionRef.current) return;
|
|
75
|
+
|
|
76
|
+
recognitionRef.current.stop();
|
|
77
|
+
setListening(false);
|
|
78
|
+
}, []);
|
|
79
|
+
|
|
80
|
+
const onMouseDown = useCallback<MouseEventHandler<HTMLDivElement>>(
|
|
81
|
+
(event) => {
|
|
82
|
+
if (!listening) {
|
|
83
|
+
event.preventDefault();
|
|
84
|
+
startListening();
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
[listening, startListening]
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const onMouseUp = useCallback<MouseEventHandler<HTMLDivElement>>(
|
|
91
|
+
(event) => {
|
|
92
|
+
if (listening) {
|
|
93
|
+
event.preventDefault();
|
|
94
|
+
stopListening();
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
[listening, stopListening]
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const onTouchStart = useCallback<TouchEventHandler<HTMLDivElement>>(
|
|
101
|
+
(event) => {
|
|
102
|
+
if (!listening) {
|
|
103
|
+
event.preventDefault();
|
|
104
|
+
startListening();
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
[listening, startListening]
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const onTouchEnd = useCallback<TouchEventHandler<HTMLDivElement>>(
|
|
111
|
+
(event) => {
|
|
112
|
+
if (listening) {
|
|
113
|
+
event.preventDefault();
|
|
114
|
+
stopListening();
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
[listening, stopListening]
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
return (
|
|
121
|
+
<div
|
|
122
|
+
className={className}
|
|
123
|
+
style={style}
|
|
124
|
+
onMouseDown={onMouseDown}
|
|
125
|
+
onMouseUp={onMouseUp}
|
|
126
|
+
onTouchStart={onTouchStart}
|
|
127
|
+
onTouchEnd={onTouchEnd}
|
|
128
|
+
>
|
|
129
|
+
{listening ? <StopSvg /> : <MicSvg />}
|
|
130
|
+
</div>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
.chatbot_header {
|
|
2
|
+
border-bottom: 1px solid #434343;
|
|
3
|
+
|
|
4
|
+
.chatbot_header__content {
|
|
5
|
+
margin: 0 auto;
|
|
6
|
+
max-width: 1200px;
|
|
7
|
+
padding: var(--asg-spacing-4);
|
|
8
|
+
display: flex;
|
|
9
|
+
justify-content: space-between;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.chatbot_header__title {
|
|
14
|
+
display: flex;
|
|
15
|
+
flex-direction: row;
|
|
16
|
+
align-items: center;
|
|
17
|
+
gap: var(--asg-spacing-2);
|
|
18
|
+
|
|
19
|
+
> h4 {
|
|
20
|
+
margin: 0;
|
|
21
|
+
color: white;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.chatbot_header__extra {
|
|
26
|
+
display: flex;
|
|
27
|
+
flex-direction: row;
|
|
28
|
+
align-items: center;
|
|
29
|
+
gap: var(--asg-spacing-3);
|
|
30
|
+
|
|
31
|
+
> div {
|
|
32
|
+
width: 24px;
|
|
33
|
+
height: 24px;
|
|
34
|
+
display: flex;
|
|
35
|
+
justify-content: center;
|
|
36
|
+
align-items: center;
|
|
37
|
+
|
|
38
|
+
> svg {
|
|
39
|
+
width: 15px;
|
|
40
|
+
height: 15px;
|
|
41
|
+
cursor: pointer;
|
|
42
|
+
|
|
43
|
+
&:hover {
|
|
44
|
+
opacity: 0.5;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { MouseEventHandler, ReactNode, useCallback, useMemo } from 'react';
|
|
2
|
+
import styles from './chatbot-header.module.scss';
|
|
3
|
+
import { ProfileIcon } from '../profile-icon';
|
|
4
|
+
import RefreshSvg from '../../../icons/refresh.svg?react';
|
|
5
|
+
import CloseSvg from '../../../icons/close.svg?react';
|
|
6
|
+
import {
|
|
7
|
+
useAsgardAppInitializationContext,
|
|
8
|
+
useAsgardThemeContext,
|
|
9
|
+
useAsgardContext,
|
|
10
|
+
} from '../../../context/';
|
|
11
|
+
import clsx from 'clsx';
|
|
12
|
+
|
|
13
|
+
interface ChatbotHeaderProps {
|
|
14
|
+
title?: string;
|
|
15
|
+
customActions?: ReactNode[];
|
|
16
|
+
maintainConnectionWhenClosed?: boolean;
|
|
17
|
+
onClose?: () => void;
|
|
18
|
+
onReset?: () => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function ChatbotHeader(props: ChatbotHeaderProps): ReactNode {
|
|
22
|
+
const {
|
|
23
|
+
title,
|
|
24
|
+
onReset,
|
|
25
|
+
onClose,
|
|
26
|
+
customActions,
|
|
27
|
+
maintainConnectionWhenClosed,
|
|
28
|
+
} = props;
|
|
29
|
+
|
|
30
|
+
const { chatbot } = useAsgardThemeContext();
|
|
31
|
+
const {
|
|
32
|
+
data: { annotations },
|
|
33
|
+
} = useAsgardAppInitializationContext();
|
|
34
|
+
|
|
35
|
+
const { avatar, isResetting, resetChannel, closeChannel } =
|
|
36
|
+
useAsgardContext();
|
|
37
|
+
|
|
38
|
+
const contentStyles = useMemo(
|
|
39
|
+
() => ({
|
|
40
|
+
maxWidth: chatbot?.contentMaxWidth ?? '1200px',
|
|
41
|
+
borderBottomColor: chatbot?.borderColor,
|
|
42
|
+
}),
|
|
43
|
+
[chatbot]
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const _onReset = useCallback<MouseEventHandler<HTMLDivElement>>(
|
|
47
|
+
(e) => {
|
|
48
|
+
if (!isResetting) {
|
|
49
|
+
e.stopPropagation();
|
|
50
|
+
onReset?.();
|
|
51
|
+
resetChannel?.();
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
[isResetting, onReset, resetChannel]
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const _onClose = useCallback<MouseEventHandler<HTMLDivElement>>(
|
|
58
|
+
(e) => {
|
|
59
|
+
if (!isResetting) {
|
|
60
|
+
e.stopPropagation();
|
|
61
|
+
onClose?.();
|
|
62
|
+
|
|
63
|
+
if (!maintainConnectionWhenClosed) {
|
|
64
|
+
closeChannel?.();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
[isResetting, onClose, closeChannel, maintainConnectionWhenClosed]
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<div
|
|
73
|
+
className={clsx('asgard-chatbot-header', styles.chatbot_header)}
|
|
74
|
+
style={chatbot?.header?.style}
|
|
75
|
+
>
|
|
76
|
+
<div className={styles.chatbot_header__content} style={contentStyles}>
|
|
77
|
+
<div className={styles.chatbot_header__title}>
|
|
78
|
+
<ProfileIcon avatar={avatar} />
|
|
79
|
+
<h4 style={chatbot?.header?.title?.style}>
|
|
80
|
+
{annotations?.embedConfig?.title || title || 'Bot'}
|
|
81
|
+
</h4>
|
|
82
|
+
</div>
|
|
83
|
+
<div
|
|
84
|
+
className={styles.chatbot_header__extra}
|
|
85
|
+
style={chatbot?.header?.actionButton?.style}
|
|
86
|
+
>
|
|
87
|
+
{customActions}
|
|
88
|
+
<div onClick={_onReset}>
|
|
89
|
+
<RefreshSvg />
|
|
90
|
+
</div>
|
|
91
|
+
<div onClick={_onClose}>
|
|
92
|
+
<CloseSvg />
|
|
93
|
+
</div>
|
|
94
|
+
</div>
|
|
95
|
+
</div>
|
|
96
|
+
</div>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './chatbot-header';
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { forwardRef, ForwardedRef, ReactNode, CSSProperties } from 'react';
|
|
2
|
+
import { ClientConfig, ConversationMessage } from '@asgard-js/core';
|
|
3
|
+
import {
|
|
4
|
+
AsgardThemeContextProvider,
|
|
5
|
+
AsgardThemeContextValue,
|
|
6
|
+
} from '../../context/asgard-theme-context';
|
|
7
|
+
import {
|
|
8
|
+
AsgardServiceContextProvider,
|
|
9
|
+
AsgardServiceContextValue,
|
|
10
|
+
AsgardTemplateContextProvider,
|
|
11
|
+
AsgardTemplateContextValue,
|
|
12
|
+
AsgardAppInitializationContextProvider,
|
|
13
|
+
AsgardServiceContextProviderProps,
|
|
14
|
+
} from '../../context';
|
|
15
|
+
import { ChatbotHeader } from './chatbot-header';
|
|
16
|
+
import { ChatbotBody } from './chatbot-body';
|
|
17
|
+
import { ChatbotFooter } from './chatbot-footer';
|
|
18
|
+
import { ChatbotContainer } from './chatbot-container/chatbot-container';
|
|
19
|
+
|
|
20
|
+
interface ChatbotProps extends AsgardTemplateContextValue {
|
|
21
|
+
className?: string;
|
|
22
|
+
style?: CSSProperties;
|
|
23
|
+
title?: string;
|
|
24
|
+
customActions?: ReactNode[];
|
|
25
|
+
theme?: Partial<AsgardThemeContextValue>;
|
|
26
|
+
config: ClientConfig;
|
|
27
|
+
customChannelId: string;
|
|
28
|
+
initMessages?: ConversationMessage[];
|
|
29
|
+
onSseMessage?: AsgardServiceContextProviderProps['onSseMessage'];
|
|
30
|
+
fullScreen?: boolean;
|
|
31
|
+
avatar?: string;
|
|
32
|
+
botTypingPlaceholder?: string;
|
|
33
|
+
inputPlaceholder?: string;
|
|
34
|
+
enableLoadConfigFromService?: boolean;
|
|
35
|
+
maintainConnectionWhenClosed?: boolean;
|
|
36
|
+
asyncInitializers?: Record<string, () => Promise<unknown>>;
|
|
37
|
+
onReset?: () => void;
|
|
38
|
+
onClose?: () => void;
|
|
39
|
+
loadingComponent?: ReactNode;
|
|
40
|
+
defaultLinkTarget?: '_blank' | '_self' | '_parent' | '_top';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ChatbotRef {
|
|
44
|
+
serviceContext?: AsgardServiceContextValue;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const Chatbot = forwardRef(function Chatbot(
|
|
48
|
+
props: ChatbotProps,
|
|
49
|
+
ref: ForwardedRef<ChatbotRef>
|
|
50
|
+
): ReactNode {
|
|
51
|
+
const {
|
|
52
|
+
title,
|
|
53
|
+
customActions,
|
|
54
|
+
theme,
|
|
55
|
+
config,
|
|
56
|
+
customChannelId,
|
|
57
|
+
initMessages,
|
|
58
|
+
onSseMessage,
|
|
59
|
+
fullScreen = false,
|
|
60
|
+
avatar,
|
|
61
|
+
botTypingPlaceholder,
|
|
62
|
+
inputPlaceholder,
|
|
63
|
+
enableLoadConfigFromService = false,
|
|
64
|
+
maintainConnectionWhenClosed = false,
|
|
65
|
+
asyncInitializers = {},
|
|
66
|
+
loadingComponent,
|
|
67
|
+
onReset,
|
|
68
|
+
onClose,
|
|
69
|
+
onTemplateBtnClick,
|
|
70
|
+
onErrorClick,
|
|
71
|
+
errorMessageRenderer,
|
|
72
|
+
className,
|
|
73
|
+
style,
|
|
74
|
+
defaultLinkTarget,
|
|
75
|
+
} = props;
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<AsgardAppInitializationContextProvider
|
|
79
|
+
enabled={enableLoadConfigFromService}
|
|
80
|
+
config={config}
|
|
81
|
+
asyncInitializers={asyncInitializers}
|
|
82
|
+
loadingComponent={loadingComponent}
|
|
83
|
+
>
|
|
84
|
+
<AsgardThemeContextProvider theme={theme}>
|
|
85
|
+
<AsgardServiceContextProvider
|
|
86
|
+
parentRef={ref}
|
|
87
|
+
avatar={avatar}
|
|
88
|
+
config={config}
|
|
89
|
+
customChannelId={customChannelId}
|
|
90
|
+
initMessages={initMessages}
|
|
91
|
+
onSseMessage={onSseMessage}
|
|
92
|
+
botTypingPlaceholder={botTypingPlaceholder}
|
|
93
|
+
inputPlaceholder={inputPlaceholder}
|
|
94
|
+
>
|
|
95
|
+
<ChatbotContainer
|
|
96
|
+
fullScreen={fullScreen}
|
|
97
|
+
className={className}
|
|
98
|
+
style={style}
|
|
99
|
+
>
|
|
100
|
+
<ChatbotHeader
|
|
101
|
+
title={title}
|
|
102
|
+
onReset={onReset}
|
|
103
|
+
onClose={onClose}
|
|
104
|
+
customActions={customActions}
|
|
105
|
+
maintainConnectionWhenClosed={maintainConnectionWhenClosed}
|
|
106
|
+
/>
|
|
107
|
+
<AsgardTemplateContextProvider
|
|
108
|
+
onErrorClick={onErrorClick}
|
|
109
|
+
errorMessageRenderer={errorMessageRenderer}
|
|
110
|
+
onTemplateBtnClick={onTemplateBtnClick}
|
|
111
|
+
defaultLinkTarget={defaultLinkTarget}
|
|
112
|
+
>
|
|
113
|
+
<ChatbotBody />
|
|
114
|
+
</AsgardTemplateContextProvider>
|
|
115
|
+
<ChatbotFooter />
|
|
116
|
+
</ChatbotContainer>
|
|
117
|
+
</AsgardServiceContextProvider>
|
|
118
|
+
</AsgardThemeContextProvider>
|
|
119
|
+
</AsgardAppInitializationContextProvider>
|
|
120
|
+
);
|
|
121
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import ProfileSvg from '../../icons/profile.svg?react';
|
|
3
|
+
|
|
4
|
+
interface ProfileIconProps {
|
|
5
|
+
avatar?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function ProfileIcon(props: ProfileIconProps): ReactNode {
|
|
9
|
+
const { avatar } = props;
|
|
10
|
+
|
|
11
|
+
if (avatar) {
|
|
12
|
+
return (
|
|
13
|
+
<img
|
|
14
|
+
src={avatar}
|
|
15
|
+
alt="avatar"
|
|
16
|
+
style={{
|
|
17
|
+
width: 33,
|
|
18
|
+
height: 32,
|
|
19
|
+
borderRadius: '50%',
|
|
20
|
+
}}
|
|
21
|
+
/>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return <ProfileSvg />;
|
|
26
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { memo, ReactNode } from 'react';
|
|
2
|
+
import styles from './avatar.module.scss';
|
|
3
|
+
import BotSvg from '../../../icons/bot.svg?react';
|
|
4
|
+
import clsx from 'clsx';
|
|
5
|
+
|
|
6
|
+
interface AvatarProps {
|
|
7
|
+
avatar?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const Avatar = memo((props: AvatarProps): ReactNode => {
|
|
11
|
+
const { avatar } = props;
|
|
12
|
+
|
|
13
|
+
if (avatar) {
|
|
14
|
+
return (
|
|
15
|
+
<img
|
|
16
|
+
src={avatar}
|
|
17
|
+
alt="Bot Avatar"
|
|
18
|
+
className={clsx('asgard-avatar', styles.bot_avatar)}
|
|
19
|
+
/>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<div className={clsx('asgard-avatar', styles.bot_avatar)}>
|
|
25
|
+
<BotSvg />
|
|
26
|
+
</div>
|
|
27
|
+
);
|
|
28
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './avatar';
|
|
File without changes
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ButtonMessageTemplate, ConversationBotMessage } from '@asgard-js/core';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { TemplateBox, TemplateBoxContent } from '../template-box';
|
|
4
|
+
import { Avatar } from '../avatar';
|
|
5
|
+
import { Card } from './card';
|
|
6
|
+
import { useAsgardContext } from '../../../context/asgard-service-context';
|
|
7
|
+
import { useAsgardThemeContext } from '../../../context/asgard-theme-context';
|
|
8
|
+
|
|
9
|
+
interface ButtonTemplateProps {
|
|
10
|
+
message: ConversationBotMessage;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function ButtonTemplate(props: ButtonTemplateProps): ReactNode {
|
|
14
|
+
const { message } = props;
|
|
15
|
+
|
|
16
|
+
const { template: themeTemplate } = useAsgardThemeContext();
|
|
17
|
+
|
|
18
|
+
const { avatar } = useAsgardContext();
|
|
19
|
+
|
|
20
|
+
const template = message.message.template as ButtonMessageTemplate;
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<TemplateBox
|
|
24
|
+
className="asgard-button-template"
|
|
25
|
+
type="bot"
|
|
26
|
+
direction="horizontal"
|
|
27
|
+
style={themeTemplate?.ButtonMessageTemplate?.style}
|
|
28
|
+
>
|
|
29
|
+
<Avatar avatar={avatar} />
|
|
30
|
+
<TemplateBoxContent
|
|
31
|
+
time={message.time}
|
|
32
|
+
quickReplies={template?.quickReplies}
|
|
33
|
+
>
|
|
34
|
+
<Card
|
|
35
|
+
template={template}
|
|
36
|
+
customStyle={{
|
|
37
|
+
button: {
|
|
38
|
+
style: themeTemplate?.ButtonMessageTemplate?.button?.style ?? {},
|
|
39
|
+
},
|
|
40
|
+
}}
|
|
41
|
+
/>
|
|
42
|
+
</TemplateBoxContent>
|
|
43
|
+
</TemplateBox>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
.card_root {
|
|
2
|
+
width: 255px;
|
|
3
|
+
height: 368px;
|
|
4
|
+
max-height: 380px;
|
|
5
|
+
border-radius: 8px;
|
|
6
|
+
background: rgba(51, 51, 51, 1);
|
|
7
|
+
overflow: hidden;
|
|
8
|
+
display: grid;
|
|
9
|
+
grid-template-rows: max-content auto;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.card_content {
|
|
13
|
+
display: grid;
|
|
14
|
+
grid-template-rows: 1.5em auto max-content;
|
|
15
|
+
gap: var(--asg-spacing-2);
|
|
16
|
+
padding: 12px;
|
|
17
|
+
line-height: 1.5em;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.card_title {
|
|
21
|
+
color: white;
|
|
22
|
+
margin: 0;
|
|
23
|
+
font-size: 15px;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.card_description {
|
|
27
|
+
color: rgba(140, 140, 140, 1);
|
|
28
|
+
font-size: 13px;
|
|
29
|
+
font-weight: 400;
|
|
30
|
+
max-height: 4.5em;
|
|
31
|
+
overflow: hidden;
|
|
32
|
+
text-overflow: ellipsis;
|
|
33
|
+
display: -webkit-box;
|
|
34
|
+
line-clamp: 3;
|
|
35
|
+
-webkit-line-clamp: 3;
|
|
36
|
+
-webkit-box-orient: vertical;
|
|
37
|
+
text-underline-position: from-font;
|
|
38
|
+
text-decoration-skip-ink: none;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.card_actions {
|
|
42
|
+
display: flex;
|
|
43
|
+
flex-direction: column;
|
|
44
|
+
gap: 8px;
|
|
45
|
+
|
|
46
|
+
> button {
|
|
47
|
+
width: 100%;
|
|
48
|
+
height: 32px;
|
|
49
|
+
line-height: 32px;
|
|
50
|
+
text-align: center;
|
|
51
|
+
border: none;
|
|
52
|
+
border-radius: 4px;
|
|
53
|
+
background: rgba(71, 103, 235, 1);
|
|
54
|
+
color: white;
|
|
55
|
+
font-size: 15px;
|
|
56
|
+
cursor: pointer;
|
|
57
|
+
}
|
|
58
|
+
}
|