@asgard-js/react 0.0.29 → 0.0.32-canary.0
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 +13 -13
- package/dist/components/chatbot/chatbot-header/chatbot-header.d.ts +1 -1
- package/dist/components/chatbot/chatbot-header/chatbot-header.d.ts.map +1 -1
- package/dist/components/chatbot/chatbot.d.ts +2 -1
- package/dist/components/chatbot/chatbot.d.ts.map +1 -1
- package/dist/components/templates/button-template/card.d.ts.map +1 -1
- package/dist/components/templates/text-template/{use-markdown-renderer.d.ts → use-react-markdown-renderer.d.ts} +3 -1
- package/dist/components/templates/text-template/use-react-markdown-renderer.d.ts.map +1 -0
- package/dist/context/asgard-template-context.d.ts +2 -0
- package/dist/context/asgard-template-context.d.ts.map +1 -1
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.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/index.js +74570 -54207
- package/dist/style.css +1 -1
- 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 +15 -9
- 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 +118 -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 +48 -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 +76 -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/text-template/use-react-markdown-renderer.tsx +264 -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 +139 -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-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 +23 -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 +62 -0
- package/tsconfig.spec.json +36 -0
- package/vite.config.ts +63 -0
- package/dist/components/templates/text-template/use-markdown-renderer.d.ts.map +0 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import styles from './template-box-content.module.scss';
|
|
3
|
+
import { QuickReplies } from '../quick-replies';
|
|
4
|
+
import { Time } from '../time';
|
|
5
|
+
import clsx from 'clsx';
|
|
6
|
+
|
|
7
|
+
interface TemplateBoxContentProps {
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
time?: Date;
|
|
10
|
+
quickReplies?: { text: string }[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function TemplateBoxContent(props: TemplateBoxContentProps): ReactNode {
|
|
14
|
+
const { quickReplies, time, children } = props;
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<div
|
|
18
|
+
className={clsx(
|
|
19
|
+
'asgard-template-box-content',
|
|
20
|
+
styles.template_box_content
|
|
21
|
+
)}
|
|
22
|
+
>
|
|
23
|
+
<div className={styles.content}>
|
|
24
|
+
{children}
|
|
25
|
+
<Time time={time} />
|
|
26
|
+
</div>
|
|
27
|
+
{!!quickReplies?.length && <QuickReplies quickReplies={quickReplies} />}
|
|
28
|
+
</div>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
.template_box {
|
|
2
|
+
width: 100%;
|
|
3
|
+
display: flex;
|
|
4
|
+
gap: 8px;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.template_box--bot {
|
|
8
|
+
&.template_box--horizontal {
|
|
9
|
+
flex-direction: row;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
&.template_box--vertical {
|
|
13
|
+
flex-direction: column;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.template_box--user {
|
|
18
|
+
flex-direction: row-reverse;
|
|
19
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { CSSProperties, ReactNode, useMemo } from 'react';
|
|
2
|
+
import styles from './template-box.module.scss';
|
|
3
|
+
import clsx from 'clsx';
|
|
4
|
+
|
|
5
|
+
type TemplateBoxProps =
|
|
6
|
+
| {
|
|
7
|
+
type: 'user';
|
|
8
|
+
direction: 'horizontal';
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
className?: string;
|
|
11
|
+
style?: CSSProperties;
|
|
12
|
+
}
|
|
13
|
+
| {
|
|
14
|
+
type: 'bot';
|
|
15
|
+
direction: 'horizontal' | 'vertical';
|
|
16
|
+
children: ReactNode;
|
|
17
|
+
className?: string;
|
|
18
|
+
style?: CSSProperties;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export function TemplateBox(props: TemplateBoxProps): ReactNode {
|
|
22
|
+
const { type, direction = 'horizontal', children, style, className } = props;
|
|
23
|
+
|
|
24
|
+
const boxClassName = useMemo(() => {
|
|
25
|
+
switch (type) {
|
|
26
|
+
case 'user':
|
|
27
|
+
return clsx(styles.template_box, styles['template_box--user']);
|
|
28
|
+
case 'bot':
|
|
29
|
+
default:
|
|
30
|
+
return clsx(
|
|
31
|
+
styles.template_box,
|
|
32
|
+
styles['template_box--bot'],
|
|
33
|
+
direction === 'horizontal'
|
|
34
|
+
? styles['template_box--horizontal']
|
|
35
|
+
: styles['template_box--vertical']
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
}, [direction, type]);
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<div
|
|
42
|
+
className={clsx('asgard-template-box', boxClassName, className)}
|
|
43
|
+
style={style}
|
|
44
|
+
>
|
|
45
|
+
{children}
|
|
46
|
+
</div>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { CSSProperties, ReactNode, useCallback, useMemo, useRef } from 'react';
|
|
2
|
+
import { useAsgardContext } from 'src/context/asgard-service-context';
|
|
3
|
+
import clsx from 'clsx';
|
|
4
|
+
import { TemplateBox, TemplateBoxContent } from '../template-box';
|
|
5
|
+
import { Avatar } from '../avatar';
|
|
6
|
+
import { useDebounce, useResizeObserver } from 'src/hooks';
|
|
7
|
+
import classes from './text-template.module.scss';
|
|
8
|
+
import { useAsgardThemeContext } from 'src/context/asgard-theme-context';
|
|
9
|
+
import { useMarkdownRenderer } from './use-react-markdown-renderer';
|
|
10
|
+
interface BotTypingBoxProps {
|
|
11
|
+
isTyping: boolean;
|
|
12
|
+
typingText: string | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function BotTypingBox(props: BotTypingBoxProps): ReactNode {
|
|
16
|
+
const { isTyping, typingText } = props;
|
|
17
|
+
const { messageBoxBottomRef, avatar } = useAsgardContext();
|
|
18
|
+
|
|
19
|
+
const theme = useAsgardThemeContext();
|
|
20
|
+
|
|
21
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
22
|
+
|
|
23
|
+
const onResize = useCallback(() => {
|
|
24
|
+
messageBoxBottomRef.current?.scrollIntoView({ behavior: 'smooth' });
|
|
25
|
+
}, [messageBoxBottomRef]);
|
|
26
|
+
|
|
27
|
+
useResizeObserver({ ref, onResize });
|
|
28
|
+
|
|
29
|
+
const _isTyping = useDebounce(isTyping, 500);
|
|
30
|
+
|
|
31
|
+
const { htmlBlocks, lastTypingText } = useMarkdownRenderer(
|
|
32
|
+
typingText || '',
|
|
33
|
+
20
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const styles = useMemo<CSSProperties>(
|
|
37
|
+
() => ({
|
|
38
|
+
color: theme?.botMessage?.color,
|
|
39
|
+
backgroundColor: theme?.botMessage?.backgroundColor,
|
|
40
|
+
}),
|
|
41
|
+
[theme]
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const dotStyles = useMemo<CSSProperties>(
|
|
45
|
+
() => ({
|
|
46
|
+
backgroundColor: theme?.botMessage?.color,
|
|
47
|
+
}),
|
|
48
|
+
[theme]
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
if (!_isTyping) return null;
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<TemplateBox
|
|
55
|
+
className="asgard-text-template asgard-text-template--bot"
|
|
56
|
+
type="bot"
|
|
57
|
+
direction="horizontal"
|
|
58
|
+
>
|
|
59
|
+
<Avatar avatar={avatar} />
|
|
60
|
+
<TemplateBoxContent time={new Date()}>
|
|
61
|
+
<div
|
|
62
|
+
ref={ref}
|
|
63
|
+
className={clsx(classes.text, classes['text--bot'])}
|
|
64
|
+
style={styles}
|
|
65
|
+
>
|
|
66
|
+
<span>
|
|
67
|
+
{htmlBlocks}
|
|
68
|
+
{lastTypingText ?? ''}
|
|
69
|
+
{_isTyping && (
|
|
70
|
+
<span className={classes['typing-indicator']}>
|
|
71
|
+
<div className={classes.dot} style={dotStyles} />
|
|
72
|
+
<div className={classes.dot} style={dotStyles} />
|
|
73
|
+
<div className={classes.dot} style={dotStyles} />
|
|
74
|
+
</span>
|
|
75
|
+
)}
|
|
76
|
+
</span>
|
|
77
|
+
</div>
|
|
78
|
+
</TemplateBoxContent>
|
|
79
|
+
</TemplateBox>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ReactNode, useMemo } from 'react';
|
|
2
|
+
import { BotTypingBox } from './bot-typing-box';
|
|
3
|
+
import { useAsgardContext } from 'src/context/asgard-service-context';
|
|
4
|
+
|
|
5
|
+
interface BotTypingPlaceholderProps {
|
|
6
|
+
placeholder: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function BotTypingPlaceholder(
|
|
10
|
+
props: BotTypingPlaceholderProps
|
|
11
|
+
): ReactNode {
|
|
12
|
+
const { placeholder } = props;
|
|
13
|
+
|
|
14
|
+
const { isConnecting, messages } = useAsgardContext();
|
|
15
|
+
|
|
16
|
+
const hasTypingMessage = useMemo(
|
|
17
|
+
() =>
|
|
18
|
+
Array.from(messages?.values() ?? []).some(
|
|
19
|
+
(message) => message.type === 'bot' && message.isTyping
|
|
20
|
+
),
|
|
21
|
+
[messages]
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
if (isConnecting && !hasTypingMessage)
|
|
25
|
+
return <BotTypingBox isTyping typingText={placeholder} />;
|
|
26
|
+
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
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
|
+
width: 100%;
|
|
15
|
+
|
|
16
|
+
p {
|
|
17
|
+
margin: 0;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.text--user {
|
|
23
|
+
max-width: 75%;
|
|
24
|
+
background: #4767eb;
|
|
25
|
+
border-top-right-radius: 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.text--bot {
|
|
29
|
+
max-width: 70%;
|
|
30
|
+
background: #585858;
|
|
31
|
+
border-top-left-radius: 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.content {
|
|
35
|
+
display: flex;
|
|
36
|
+
flex-direction: column;
|
|
37
|
+
gap: 8px;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.typing-indicator {
|
|
41
|
+
display: inline-flex;
|
|
42
|
+
align-items: flex-end;
|
|
43
|
+
justify-content: center;
|
|
44
|
+
|
|
45
|
+
.dot {
|
|
46
|
+
width: 2px;
|
|
47
|
+
height: 2px;
|
|
48
|
+
margin: 0 2px;
|
|
49
|
+
background-color: white;
|
|
50
|
+
border-radius: 50%;
|
|
51
|
+
|
|
52
|
+
animation: blink 1.4s infinite ease-in-out both;
|
|
53
|
+
|
|
54
|
+
&:nth-child(1) {
|
|
55
|
+
animation-delay: 0s;
|
|
56
|
+
}
|
|
57
|
+
&:nth-child(2) {
|
|
58
|
+
animation-delay: 0.2s;
|
|
59
|
+
}
|
|
60
|
+
&:nth-child(3) {
|
|
61
|
+
animation-delay: 0.4s;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.table_container {
|
|
67
|
+
overflow-x: scroll;
|
|
68
|
+
|
|
69
|
+
table {
|
|
70
|
+
width: 100%;
|
|
71
|
+
border-collapse: collapse;
|
|
72
|
+
border-spacing: 0;
|
|
73
|
+
min-width: 600px;
|
|
74
|
+
font-size: 14px;
|
|
75
|
+
background-color: #1f1f1f;
|
|
76
|
+
color: rgba(255, 255, 255, 0.85);
|
|
77
|
+
border: 1px solid #333;
|
|
78
|
+
border-radius: 4px;
|
|
79
|
+
overflow: hidden;
|
|
80
|
+
|
|
81
|
+
thead {
|
|
82
|
+
position: sticky;
|
|
83
|
+
top: 0;
|
|
84
|
+
z-index: 1;
|
|
85
|
+
background-color: #141414;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
th,
|
|
89
|
+
td {
|
|
90
|
+
padding: 12px 16px;
|
|
91
|
+
border-bottom: 1px solid #333;
|
|
92
|
+
text-align: left;
|
|
93
|
+
white-space: nowrap;
|
|
94
|
+
transition: background-color 0.3s, color 0.3s;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
th {
|
|
98
|
+
font-weight: 600;
|
|
99
|
+
color: rgba(255, 255, 255, 0.95);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
tbody tr:nth-child(odd) {
|
|
103
|
+
background-color: #1c1c1c;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
tbody tr:nth-child(even) {
|
|
107
|
+
background-color: #111;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
tbody tr:hover td {
|
|
111
|
+
background-color: #262626;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
tbody tr:last-child td {
|
|
115
|
+
border-bottom: none;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@keyframes blink {
|
|
121
|
+
0%,
|
|
122
|
+
80%,
|
|
123
|
+
100% {
|
|
124
|
+
opacity: 0.2;
|
|
125
|
+
transform: translateY(0);
|
|
126
|
+
}
|
|
127
|
+
40% {
|
|
128
|
+
opacity: 1;
|
|
129
|
+
transform: translateY(-5px);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { CSSProperties, ReactNode, useMemo } from 'react';
|
|
2
|
+
import clsx from 'clsx';
|
|
3
|
+
import { ConversationBotMessage, ConversationMessage } from '@asgard-js/core';
|
|
4
|
+
import { TemplateBox, TemplateBoxContent } from '../template-box';
|
|
5
|
+
import classes from './text-template.module.scss';
|
|
6
|
+
import { Avatar } from '../avatar';
|
|
7
|
+
import { Time } from '../time';
|
|
8
|
+
import { useAsgardContext } from 'src/context/asgard-service-context';
|
|
9
|
+
import { useAsgardThemeContext } from 'src/context/asgard-theme-context';
|
|
10
|
+
import { useMarkdownRenderer } from './use-react-markdown-renderer';
|
|
11
|
+
|
|
12
|
+
interface TextTemplateProps {
|
|
13
|
+
message: ConversationMessage;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function TextTemplate(props: TextTemplateProps): ReactNode {
|
|
17
|
+
const { message } = props;
|
|
18
|
+
|
|
19
|
+
const { avatar } = useAsgardContext();
|
|
20
|
+
|
|
21
|
+
const theme = useAsgardThemeContext();
|
|
22
|
+
|
|
23
|
+
const { htmlBlocks, lastTypingText } = useMarkdownRenderer(
|
|
24
|
+
(message as ConversationBotMessage)?.message?.text || '',
|
|
25
|
+
20
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const rootStyle = theme?.template?.TextMessageTemplate?.style;
|
|
29
|
+
|
|
30
|
+
const styles = useMemo<CSSProperties>(() => {
|
|
31
|
+
switch (message.type) {
|
|
32
|
+
case 'user':
|
|
33
|
+
return {
|
|
34
|
+
color: theme?.userMessage?.color,
|
|
35
|
+
backgroundColor: theme?.userMessage?.backgroundColor,
|
|
36
|
+
};
|
|
37
|
+
case 'bot':
|
|
38
|
+
return {
|
|
39
|
+
color: theme?.botMessage?.color,
|
|
40
|
+
backgroundColor: theme?.botMessage?.backgroundColor,
|
|
41
|
+
};
|
|
42
|
+
default:
|
|
43
|
+
return {};
|
|
44
|
+
}
|
|
45
|
+
}, [message, theme]);
|
|
46
|
+
|
|
47
|
+
if (message.type === 'error') return null;
|
|
48
|
+
|
|
49
|
+
if (message.type === 'user') {
|
|
50
|
+
return (
|
|
51
|
+
<TemplateBox
|
|
52
|
+
className="asgard-text-template asgard-text-template--user"
|
|
53
|
+
type="user"
|
|
54
|
+
direction="horizontal"
|
|
55
|
+
style={rootStyle}
|
|
56
|
+
>
|
|
57
|
+
<div
|
|
58
|
+
className={clsx(classes.text, classes['text--user'])}
|
|
59
|
+
style={styles}
|
|
60
|
+
>
|
|
61
|
+
{message.text}
|
|
62
|
+
</div>
|
|
63
|
+
<Time time={message.time} />
|
|
64
|
+
</TemplateBox>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<TemplateBox
|
|
70
|
+
className="asgard-text-template asgard-text-template--bot"
|
|
71
|
+
type="bot"
|
|
72
|
+
direction="horizontal"
|
|
73
|
+
style={rootStyle}
|
|
74
|
+
>
|
|
75
|
+
<Avatar avatar={avatar} />
|
|
76
|
+
<TemplateBoxContent
|
|
77
|
+
time={message.time}
|
|
78
|
+
quickReplies={message.message.template?.quickReplies}
|
|
79
|
+
>
|
|
80
|
+
<div
|
|
81
|
+
className={clsx(classes.text, classes['text--bot'])}
|
|
82
|
+
style={styles}
|
|
83
|
+
>
|
|
84
|
+
{htmlBlocks}
|
|
85
|
+
{lastTypingText ?? ''}
|
|
86
|
+
</div>
|
|
87
|
+
</TemplateBoxContent>
|
|
88
|
+
</TemplateBox>
|
|
89
|
+
);
|
|
90
|
+
}
|