@asgard-js/react 0.0.43-canary.9 → 0.0.44-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/README.md +45 -1
- package/dist/components/chatbot/api-key-input/api-key-input.d.ts.map +1 -1
- package/dist/components/chatbot/chatbot.d.ts +2 -3
- package/dist/components/chatbot/chatbot.d.ts.map +1 -1
- package/dist/context/asgard-service-context.d.ts +1 -1
- package/dist/context/asgard-service-context.d.ts.map +1 -1
- package/dist/context/asgard-theme-context.d.ts +2 -0
- package/dist/context/asgard-theme-context.d.ts.map +1 -1
- package/dist/hooks/use-channel.d.ts +1 -1
- package/dist/hooks/use-channel.d.ts.map +1 -1
- package/dist/hooks/use-on-screen-keyboard-scroll-fix.d.ts +1 -1
- package/dist/hooks/use-on-screen-keyboard-scroll-fix.d.ts.map +1 -1
- package/dist/{style.css → index.css} +1 -1
- package/dist/index.js +21396 -21771
- package/package.json +3 -3
- package/.babelrc +0 -12
- package/eslint.config.cjs +0 -12
- package/src/components/.DS_Store +0 -0
- package/src/components/chatbot/api-key-input/api-key-input.module.scss +0 -184
- package/src/components/chatbot/api-key-input/api-key-input.tsx +0 -129
- package/src/components/chatbot/api-key-input/index.ts +0 -1
- package/src/components/chatbot/chatbot-body/chatbot-body.module.scss +0 -13
- package/src/components/chatbot/chatbot-body/chatbot-body.tsx +0 -45
- package/src/components/chatbot/chatbot-body/conversation-message-renderer.tsx +0 -55
- package/src/components/chatbot/chatbot-body/index.ts +0 -1
- package/src/components/chatbot/chatbot-container/chatbot-container.module.scss +0 -41
- package/src/components/chatbot/chatbot-container/chatbot-container.tsx +0 -49
- package/src/components/chatbot/chatbot-container/chatbot-full-screen-container.tsx +0 -54
- package/src/components/chatbot/chatbot-footer/chatbot-footer.module.scss +0 -67
- package/src/components/chatbot/chatbot-footer/chatbot-footer.tsx +0 -140
- package/src/components/chatbot/chatbot-footer/index.ts +0 -1
- package/src/components/chatbot/chatbot-footer/speech-input-button.tsx +0 -132
- package/src/components/chatbot/chatbot-header/chatbot-header.module.scss +0 -48
- package/src/components/chatbot/chatbot-header/chatbot-header.tsx +0 -98
- package/src/components/chatbot/chatbot-header/index.ts +0 -1
- package/src/components/chatbot/chatbot.spec.tsx +0 -8
- package/src/components/chatbot/chatbot.tsx +0 -233
- package/src/components/chatbot/profile-icon.tsx +0 -26
- package/src/components/index.ts +0 -2
- package/src/components/templates/avatar/avatar.module.scss +0 -6
- package/src/components/templates/avatar/avatar.tsx +0 -28
- package/src/components/templates/avatar/index.ts +0 -1
- package/src/components/templates/button-template/button-template.module.scss +0 -0
- package/src/components/templates/button-template/button-template.tsx +0 -45
- package/src/components/templates/button-template/card.module.scss +0 -58
- package/src/components/templates/button-template/card.spec.tsx +0 -213
- package/src/components/templates/button-template/card.tsx +0 -123
- package/src/components/templates/button-template/index.ts +0 -1
- package/src/components/templates/carousel-template/carousel-template.module.scss +0 -15
- package/src/components/templates/carousel-template/carousel-template.tsx +0 -49
- package/src/components/templates/carousel-template/index.ts +0 -1
- package/src/components/templates/chart-template/chart-template.module.scss +0 -52
- package/src/components/templates/chart-template/chart-template.tsx +0 -75
- package/src/components/templates/chart-template/index.ts +0 -1
- package/src/components/templates/hint-template/hint-template.module.scss +0 -43
- package/src/components/templates/hint-template/hint-template.tsx +0 -76
- package/src/components/templates/hint-template/index.ts +0 -1
- package/src/components/templates/image-template/image-template.module.scss +0 -67
- package/src/components/templates/image-template/image-template.tsx +0 -58
- package/src/components/templates/image-template/index.ts +0 -1
- package/src/components/templates/index.ts +0 -10
- package/src/components/templates/quick-replies/index.ts +0 -1
- package/src/components/templates/quick-replies/quick-replies.module.scss +0 -16
- package/src/components/templates/quick-replies/quick-replies.tsx +0 -47
- package/src/components/templates/template-box/index.ts +0 -2
- package/src/components/templates/template-box/template-box-content.module.scss +0 -13
- package/src/components/templates/template-box/template-box-content.tsx +0 -30
- package/src/components/templates/template-box/template-box.module.scss +0 -19
- package/src/components/templates/template-box/template-box.tsx +0 -48
- package/src/components/templates/text-template/bot-typing-box.tsx +0 -81
- package/src/components/templates/text-template/bot-typing-placeholder.tsx +0 -28
- package/src/components/templates/text-template/index.ts +0 -3
- package/src/components/templates/text-template/text-template.module.scss +0 -131
- package/src/components/templates/text-template/text-template.tsx +0 -94
- package/src/components/templates/text-template/use-react-markdown-renderer.spec.tsx +0 -758
- package/src/components/templates/time/index.ts +0 -1
- package/src/components/templates/time/time.module.scss +0 -6
- package/src/components/templates/time/time.tsx +0 -34
- package/src/context/asgard-app-initialization-context.tsx +0 -154
- package/src/context/asgard-service-context.tsx +0 -148
- package/src/context/asgard-template-context.tsx +0 -83
- package/src/context/asgard-theme-context.tsx +0 -546
- package/src/context/index.ts +0 -4
- package/src/hooks/index.ts +0 -11
- package/src/hooks/use-asgard-service-client.ts +0 -68
- package/src/hooks/use-channel.ts +0 -160
- package/src/hooks/use-debounce.ts +0 -18
- package/src/hooks/use-deep-compare-memo.ts +0 -19
- package/src/hooks/use-is-on-screen-keyboard-open.ts +0 -43
- package/src/hooks/use-on-screen-keyboard-scroll-fix.ts +0 -15
- package/src/hooks/use-prevent-over-scrolling.ts +0 -77
- package/src/hooks/use-react-markdown-renderer.tsx +0 -278
- package/src/hooks/use-resize-observer.tsx +0 -27
- package/src/hooks/use-update-vh.ts +0 -30
- package/src/hooks/use-viewport-size.ts +0 -51
- package/src/icons/add_a_photo.svg +0 -3
- package/src/icons/bot.svg +0 -14
- package/src/icons/close.svg +0 -3
- package/src/icons/distance.svg +0 -3
- package/src/icons/mic.svg +0 -3
- package/src/icons/photo_library.svg +0 -3
- package/src/icons/profile.svg +0 -28
- package/src/icons/refresh.svg +0 -3
- package/src/icons/send.svg +0 -3
- package/src/icons/stop.svg +0 -22
- package/src/icons/volume_up.svg +0 -3
- package/src/index.ts +0 -4
- package/src/models/bot-provider.ts +0 -108
- package/src/styles/_index.scss +0 -1
- package/src/styles/_styles.scss +0 -11
- package/src/styles/colors/_colors.scss +0 -10
- package/src/styles/colors/_index.scss +0 -1
- package/src/styles/colors/_variables.scss +0 -72
- package/src/styles/palette/_index.scss +0 -1
- package/src/styles/palette/_palette.scss +0 -42
- package/src/styles/palette/_variables.scss +0 -40
- package/src/styles/radius/_index.scss +0 -1
- package/src/styles/radius/_radius.scss +0 -8
- package/src/styles/radius/_variables.scss +0 -12
- package/src/styles/spacing/_index.scss +0 -1
- package/src/styles/spacing/_spacing.scss +0 -8
- package/src/styles/spacing/_variables.scss +0 -13
- package/src/styles/utils/_index.scss +0 -1
- package/src/styles/utils/_map.scss +0 -22
- package/src/test-setup.ts +0 -1
- package/src/utils/color-utils.ts +0 -52
- package/src/utils/deep-merge.ts +0 -26
- package/src/utils/extractors.ts +0 -20
- package/src/utils/format-time.ts +0 -8
- package/src/utils/index.ts +0 -1
- package/src/utils/is.ts +0 -72
- package/src/utils/selectors.ts +0 -7
- package/src/utils/uri-validation.spec.ts +0 -208
- package/src/utils/uri-validation.ts +0 -103
- package/tsconfig.json +0 -16
- package/tsconfig.lib.json +0 -63
- package/tsconfig.spec.json +0 -36
- package/tsconfig.tsbuildinfo +0 -1
- package/vite.config.ts +0 -63
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ChangeEventHandler,
|
|
3
|
-
KeyboardEventHandler,
|
|
4
|
-
ReactNode,
|
|
5
|
-
useCallback,
|
|
6
|
-
useEffect,
|
|
7
|
-
useMemo,
|
|
8
|
-
useRef,
|
|
9
|
-
useState,
|
|
10
|
-
} from 'react';
|
|
11
|
-
import { useAsgardContext } from '../../../context/asgard-service-context';
|
|
12
|
-
import styles from './chatbot-footer.module.scss';
|
|
13
|
-
import SendSvg from '../../../icons/send.svg?react';
|
|
14
|
-
import { SpeechInputButton } from './speech-input-button';
|
|
15
|
-
import clsx from 'clsx';
|
|
16
|
-
import { useAsgardThemeContext } from '../../../context/asgard-theme-context';
|
|
17
|
-
|
|
18
|
-
export function ChatbotFooter(): ReactNode {
|
|
19
|
-
const { sendMessage, isConnecting, inputPlaceholder } = useAsgardContext();
|
|
20
|
-
|
|
21
|
-
const { chatbot } = useAsgardThemeContext();
|
|
22
|
-
|
|
23
|
-
const [value, setValue] = useState('');
|
|
24
|
-
const [isComposing, setIsComposing] = useState(false);
|
|
25
|
-
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
|
26
|
-
|
|
27
|
-
const disabled = useMemo(
|
|
28
|
-
() => isConnecting || !value.trim(),
|
|
29
|
-
[isConnecting, value]
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
const contentStyles = useMemo(
|
|
33
|
-
() => ({
|
|
34
|
-
maxWidth: chatbot?.contentMaxWidth ?? '1200px',
|
|
35
|
-
borderTopColor: chatbot?.borderColor,
|
|
36
|
-
}),
|
|
37
|
-
[chatbot]
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
const onChange = useCallback<ChangeEventHandler<HTMLTextAreaElement>>(
|
|
41
|
-
(event) => {
|
|
42
|
-
const element = event.target as HTMLTextAreaElement;
|
|
43
|
-
const value = element.value;
|
|
44
|
-
|
|
45
|
-
element.style.height = '36px';
|
|
46
|
-
|
|
47
|
-
if (value) {
|
|
48
|
-
element.style.height = `${element.scrollHeight}px`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
setValue(event.target.value);
|
|
52
|
-
},
|
|
53
|
-
[]
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
const onSubmit = useCallback(() => {
|
|
57
|
-
if (!isComposing && !isConnecting) {
|
|
58
|
-
sendMessage?.({ text: value });
|
|
59
|
-
setValue('');
|
|
60
|
-
|
|
61
|
-
if (textareaRef.current) {
|
|
62
|
-
textareaRef.current.style.height = '36px';
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}, [isComposing, isConnecting, sendMessage, value]);
|
|
66
|
-
|
|
67
|
-
const onKeyDown = useCallback<KeyboardEventHandler<HTMLTextAreaElement>>(
|
|
68
|
-
(event) => {
|
|
69
|
-
if (
|
|
70
|
-
event.key === 'Enter' &&
|
|
71
|
-
!isComposing &&
|
|
72
|
-
!isConnecting &&
|
|
73
|
-
value.trim()
|
|
74
|
-
) {
|
|
75
|
-
sendMessage?.({ text: value });
|
|
76
|
-
setValue('');
|
|
77
|
-
|
|
78
|
-
const element = event.target as HTMLTextAreaElement;
|
|
79
|
-
|
|
80
|
-
element.style.height = '36px';
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
[isComposing, isConnecting, sendMessage, value]
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
useEffect(() => {
|
|
87
|
-
if (textareaRef.current) {
|
|
88
|
-
textareaRef.current.style.setProperty(
|
|
89
|
-
'--asg-color-text-placeholder',
|
|
90
|
-
chatbot.footer?.textArea?.['::placeholder']?.color ??
|
|
91
|
-
'var(--asg-color-text-placeholder)'
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
}, [chatbot.footer?.textArea]);
|
|
95
|
-
|
|
96
|
-
return (
|
|
97
|
-
<div
|
|
98
|
-
className={clsx('asgard-chatbot-footer', styles.chatbot_footer)}
|
|
99
|
-
style={chatbot.footer?.style}
|
|
100
|
-
>
|
|
101
|
-
<div className={styles.chatbot_footer__content} style={contentStyles}>
|
|
102
|
-
<textarea
|
|
103
|
-
ref={textareaRef}
|
|
104
|
-
className={styles.chatbot_textarea}
|
|
105
|
-
style={chatbot.footer?.textArea?.style}
|
|
106
|
-
disabled={isConnecting}
|
|
107
|
-
cols={40}
|
|
108
|
-
value={value}
|
|
109
|
-
placeholder={inputPlaceholder || "Enter message"}
|
|
110
|
-
onChange={onChange}
|
|
111
|
-
onKeyDown={onKeyDown}
|
|
112
|
-
onCompositionStart={() => setIsComposing(true)}
|
|
113
|
-
onCompositionEnd={() => setIsComposing(false)}
|
|
114
|
-
/>
|
|
115
|
-
{value ? (
|
|
116
|
-
<button
|
|
117
|
-
className={clsx(
|
|
118
|
-
styles.chatbot_submit_button,
|
|
119
|
-
disabled && styles.chatbot_submit_button__disabled
|
|
120
|
-
)}
|
|
121
|
-
style={chatbot.footer?.submitButton?.style}
|
|
122
|
-
disabled={disabled}
|
|
123
|
-
onClick={onSubmit}
|
|
124
|
-
>
|
|
125
|
-
<SendSvg />
|
|
126
|
-
</button>
|
|
127
|
-
) : (
|
|
128
|
-
<SpeechInputButton
|
|
129
|
-
setValue={setValue}
|
|
130
|
-
className={clsx(
|
|
131
|
-
styles.chatbot_submit_button,
|
|
132
|
-
isConnecting && styles.chatbot_submit_button__disabled
|
|
133
|
-
)}
|
|
134
|
-
style={chatbot.footer?.speechInputButton?.style}
|
|
135
|
-
/>
|
|
136
|
-
)}
|
|
137
|
-
</div>
|
|
138
|
-
</div>
|
|
139
|
-
);
|
|
140
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './chatbot-footer';
|
|
@@ -1,132 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './chatbot-header';
|
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
forwardRef,
|
|
3
|
-
ForwardedRef,
|
|
4
|
-
ReactNode,
|
|
5
|
-
CSSProperties,
|
|
6
|
-
} from 'react';
|
|
7
|
-
import { ClientConfig, ConversationMessage } from '@asgard-js/core';
|
|
8
|
-
import {
|
|
9
|
-
AsgardThemeContextProvider,
|
|
10
|
-
AsgardThemeContextValue,
|
|
11
|
-
} from '../../context/asgard-theme-context';
|
|
12
|
-
import {
|
|
13
|
-
AsgardServiceContextProvider,
|
|
14
|
-
AsgardServiceContextValue,
|
|
15
|
-
AsgardTemplateContextProvider,
|
|
16
|
-
AsgardTemplateContextValue,
|
|
17
|
-
AsgardAppInitializationContextProvider,
|
|
18
|
-
AsgardServiceContextProviderProps,
|
|
19
|
-
} from '../../context';
|
|
20
|
-
import { ApiKeyInput } from './api-key-input';
|
|
21
|
-
import { ChatbotHeader } from './chatbot-header';
|
|
22
|
-
import { ChatbotBody } from './chatbot-body';
|
|
23
|
-
import { ChatbotFooter } from './chatbot-footer';
|
|
24
|
-
import { ChatbotContainer } from './chatbot-container/chatbot-container';
|
|
25
|
-
|
|
26
|
-
type AuthState = 'loading' | 'needApiKey' | 'authenticated' | 'error';
|
|
27
|
-
|
|
28
|
-
interface ChatbotProps extends AsgardTemplateContextValue {
|
|
29
|
-
className?: string;
|
|
30
|
-
style?: CSSProperties;
|
|
31
|
-
title?: string;
|
|
32
|
-
customActions?: ReactNode[];
|
|
33
|
-
theme?: Partial<AsgardThemeContextValue>;
|
|
34
|
-
config: ClientConfig;
|
|
35
|
-
customChannelId: string;
|
|
36
|
-
initMessages?: ConversationMessage[];
|
|
37
|
-
onSseMessage?: AsgardServiceContextProviderProps['onSseMessage'];
|
|
38
|
-
fullScreen?: boolean;
|
|
39
|
-
avatar?: string;
|
|
40
|
-
botTypingPlaceholder?: string;
|
|
41
|
-
inputPlaceholder?: string;
|
|
42
|
-
enableLoadConfigFromService?: boolean;
|
|
43
|
-
maintainConnectionWhenClosed?: boolean;
|
|
44
|
-
asyncInitializers?: Record<string, () => Promise<unknown>>;
|
|
45
|
-
onReset?: () => void;
|
|
46
|
-
onClose?: () => void;
|
|
47
|
-
loadingComponent?: ReactNode;
|
|
48
|
-
defaultLinkTarget?: '_blank' | '_self' | '_parent' | '_top';
|
|
49
|
-
|
|
50
|
-
// Auth state props
|
|
51
|
-
authState?: AuthState;
|
|
52
|
-
onApiKeySubmit?: (apiKey: string) => Promise<void>;
|
|
53
|
-
onAuthError?: (error: { isAuthError: boolean; isBotProviderError: boolean; errorDetail?: any }) => void;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export interface ChatbotRef {
|
|
57
|
-
serviceContext?: AsgardServiceContextValue;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export const Chatbot = forwardRef(function Chatbot(
|
|
61
|
-
props: ChatbotProps,
|
|
62
|
-
ref: ForwardedRef<ChatbotRef>
|
|
63
|
-
): ReactNode {
|
|
64
|
-
const {
|
|
65
|
-
title,
|
|
66
|
-
customActions,
|
|
67
|
-
theme,
|
|
68
|
-
config,
|
|
69
|
-
customChannelId,
|
|
70
|
-
initMessages,
|
|
71
|
-
onSseMessage,
|
|
72
|
-
fullScreen = false,
|
|
73
|
-
avatar,
|
|
74
|
-
botTypingPlaceholder,
|
|
75
|
-
inputPlaceholder,
|
|
76
|
-
enableLoadConfigFromService = false,
|
|
77
|
-
maintainConnectionWhenClosed = false,
|
|
78
|
-
asyncInitializers = {},
|
|
79
|
-
loadingComponent,
|
|
80
|
-
onReset,
|
|
81
|
-
onClose,
|
|
82
|
-
onTemplateBtnClick,
|
|
83
|
-
onErrorClick,
|
|
84
|
-
errorMessageRenderer,
|
|
85
|
-
className,
|
|
86
|
-
style,
|
|
87
|
-
defaultLinkTarget,
|
|
88
|
-
authState = 'authenticated',
|
|
89
|
-
onApiKeySubmit,
|
|
90
|
-
onAuthError,
|
|
91
|
-
} = props;
|
|
92
|
-
|
|
93
|
-
// Render different content based on authState
|
|
94
|
-
const renderContent = () => {
|
|
95
|
-
switch (authState) {
|
|
96
|
-
case 'loading':
|
|
97
|
-
return (
|
|
98
|
-
<div style={{
|
|
99
|
-
display: 'flex',
|
|
100
|
-
alignItems: 'center',
|
|
101
|
-
justifyContent: 'center',
|
|
102
|
-
flex: 1,
|
|
103
|
-
padding: '20px'
|
|
104
|
-
}}>
|
|
105
|
-
{loadingComponent || <div>Loading...</div>}
|
|
106
|
-
</div>
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
case 'needApiKey':
|
|
110
|
-
return (
|
|
111
|
-
<div style={{
|
|
112
|
-
display: 'flex',
|
|
113
|
-
alignItems: 'center',
|
|
114
|
-
justifyContent: 'center',
|
|
115
|
-
flex: 1,
|
|
116
|
-
padding: '20px'
|
|
117
|
-
}}>
|
|
118
|
-
<ApiKeyInput
|
|
119
|
-
title={title}
|
|
120
|
-
onSubmit={onApiKeySubmit || (() => {})}
|
|
121
|
-
placeholder="Enter your key"
|
|
122
|
-
/>
|
|
123
|
-
</div>
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
case 'error':
|
|
127
|
-
return (
|
|
128
|
-
<div style={{
|
|
129
|
-
display: 'flex',
|
|
130
|
-
alignItems: 'center',
|
|
131
|
-
justifyContent: 'center',
|
|
132
|
-
flex: 1,
|
|
133
|
-
padding: '20px',
|
|
134
|
-
color: '#ff6b6b'
|
|
135
|
-
}}>
|
|
136
|
-
<div style={{ textAlign: 'center' }}>
|
|
137
|
-
<div style={{ fontSize: '16px', marginBottom: '8px' }}>⚠️</div>
|
|
138
|
-
<div>Something went wrong. Please try again later.</div>
|
|
139
|
-
</div>
|
|
140
|
-
</div>
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
case 'authenticated':
|
|
144
|
-
default:
|
|
145
|
-
return (
|
|
146
|
-
<>
|
|
147
|
-
<AsgardTemplateContextProvider
|
|
148
|
-
onErrorClick={onErrorClick}
|
|
149
|
-
errorMessageRenderer={errorMessageRenderer}
|
|
150
|
-
onTemplateBtnClick={onTemplateBtnClick}
|
|
151
|
-
defaultLinkTarget={defaultLinkTarget}
|
|
152
|
-
>
|
|
153
|
-
<ChatbotBody />
|
|
154
|
-
</AsgardTemplateContextProvider>
|
|
155
|
-
<ChatbotFooter />
|
|
156
|
-
</>
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
// Don't initialize SSE connection when explicitly needing API key or in error state
|
|
162
|
-
if (authState !== 'needApiKey' && authState !== 'error') {
|
|
163
|
-
return (
|
|
164
|
-
<AsgardAppInitializationContextProvider
|
|
165
|
-
enabled={enableLoadConfigFromService}
|
|
166
|
-
config={config}
|
|
167
|
-
asyncInitializers={asyncInitializers}
|
|
168
|
-
loadingComponent={loadingComponent}
|
|
169
|
-
>
|
|
170
|
-
<AsgardThemeContextProvider theme={theme}>
|
|
171
|
-
<AsgardServiceContextProvider
|
|
172
|
-
parentRef={ref}
|
|
173
|
-
avatar={avatar}
|
|
174
|
-
config={config}
|
|
175
|
-
customChannelId={customChannelId}
|
|
176
|
-
initMessages={initMessages}
|
|
177
|
-
onSseMessage={onSseMessage}
|
|
178
|
-
onAuthError={onAuthError}
|
|
179
|
-
botTypingPlaceholder={botTypingPlaceholder}
|
|
180
|
-
inputPlaceholder={inputPlaceholder}
|
|
181
|
-
>
|
|
182
|
-
<ChatbotContainer
|
|
183
|
-
fullScreen={fullScreen}
|
|
184
|
-
className={className}
|
|
185
|
-
style={style}
|
|
186
|
-
>
|
|
187
|
-
<ChatbotHeader
|
|
188
|
-
title={title}
|
|
189
|
-
onReset={onReset}
|
|
190
|
-
onClose={onClose}
|
|
191
|
-
customActions={customActions}
|
|
192
|
-
maintainConnectionWhenClosed={maintainConnectionWhenClosed}
|
|
193
|
-
/>
|
|
194
|
-
{renderContent()}
|
|
195
|
-
</ChatbotContainer>
|
|
196
|
-
</AsgardServiceContextProvider>
|
|
197
|
-
</AsgardThemeContextProvider>
|
|
198
|
-
</AsgardAppInitializationContextProvider>
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// For non-authenticated states, provide AsgardServiceContextProvider but without SSE connection
|
|
203
|
-
return (
|
|
204
|
-
<AsgardThemeContextProvider theme={theme}>
|
|
205
|
-
<AsgardServiceContextProvider
|
|
206
|
-
parentRef={ref}
|
|
207
|
-
avatar={avatar}
|
|
208
|
-
config={config}
|
|
209
|
-
customChannelId={customChannelId}
|
|
210
|
-
initMessages={initMessages}
|
|
211
|
-
onSseMessage={onSseMessage}
|
|
212
|
-
onAuthError={onAuthError}
|
|
213
|
-
botTypingPlaceholder={botTypingPlaceholder}
|
|
214
|
-
inputPlaceholder={inputPlaceholder}
|
|
215
|
-
>
|
|
216
|
-
<ChatbotContainer
|
|
217
|
-
fullScreen={fullScreen}
|
|
218
|
-
className={className}
|
|
219
|
-
style={style}
|
|
220
|
-
>
|
|
221
|
-
<ChatbotHeader
|
|
222
|
-
title={title}
|
|
223
|
-
onReset={onReset}
|
|
224
|
-
onClose={onClose}
|
|
225
|
-
customActions={customActions}
|
|
226
|
-
maintainConnectionWhenClosed={maintainConnectionWhenClosed}
|
|
227
|
-
/>
|
|
228
|
-
{renderContent()}
|
|
229
|
-
</ChatbotContainer>
|
|
230
|
-
</AsgardServiceContextProvider>
|
|
231
|
-
</AsgardThemeContextProvider>
|
|
232
|
-
);
|
|
233
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
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
|
-
}
|
package/src/components/index.ts
DELETED