@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,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 'src/context/asgard-service-context';
|
|
15
|
+
import { useAsgardTemplateContext } from 'src/context/asgard-template-context';
|
|
16
|
+
import { safeWindowOpen } from 'src/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, index) => (
|
|
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,48 @@
|
|
|
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
|
+
} from '@asgard-js/core';
|
|
10
|
+
import { Time } from '../time';
|
|
11
|
+
import { useAsgardContext } from 'src/context/asgard-service-context';
|
|
12
|
+
import { useAsgardThemeContext } from 'src/context/asgard-theme-context';
|
|
13
|
+
|
|
14
|
+
interface CarouselTemplateProps {
|
|
15
|
+
message: ConversationBotMessage;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function CarouselTemplate(props: CarouselTemplateProps): ReactNode {
|
|
19
|
+
const { message } = props;
|
|
20
|
+
|
|
21
|
+
const { template: themeTemplate } = useAsgardThemeContext();
|
|
22
|
+
const { avatar } = useAsgardContext();
|
|
23
|
+
|
|
24
|
+
const template = message.message.template as CarouselMessageTemplate;
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<TemplateBox
|
|
28
|
+
className="asgard-carousel-template"
|
|
29
|
+
type="bot"
|
|
30
|
+
direction="vertical"
|
|
31
|
+
style={themeTemplate?.CarouselMessageTemplate?.style}
|
|
32
|
+
>
|
|
33
|
+
<Avatar avatar={avatar} />
|
|
34
|
+
<TemplateBoxContent quickReplies={template.quickReplies}>
|
|
35
|
+
<div className={styles.carousel_root}>
|
|
36
|
+
{template.columns?.map((column, index) => (
|
|
37
|
+
<Card
|
|
38
|
+
key={index}
|
|
39
|
+
template={column}
|
|
40
|
+
customStyle={themeTemplate?.CarouselMessageTemplate?.card}
|
|
41
|
+
/>
|
|
42
|
+
))}
|
|
43
|
+
</div>
|
|
44
|
+
</TemplateBoxContent>
|
|
45
|
+
<Time className={styles.carousel_time} time={message.time} />
|
|
46
|
+
</TemplateBox>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -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,76 @@
|
|
|
1
|
+
import { ReactNode, useMemo, useState, CSSProperties } from 'react';
|
|
2
|
+
import { TemplateBox, TemplateBoxContent } from '../template-box';
|
|
3
|
+
import { Avatar } from '../avatar';
|
|
4
|
+
import { ConversationBotMessage } from '@asgard-js/core';
|
|
5
|
+
import { Time } from '../time';
|
|
6
|
+
import { useAsgardContext } from 'src/context/asgard-service-context';
|
|
7
|
+
import { ChartMessageTemplate } from '../../../../../core/src';
|
|
8
|
+
import { VegaLite, VisualizationSpec } from 'react-vega';
|
|
9
|
+
import clsx from 'clsx';
|
|
10
|
+
import classes from './chart-template.module.scss';
|
|
11
|
+
import { useAsgardThemeContext } from 'src/context/asgard-theme-context';
|
|
12
|
+
|
|
13
|
+
interface ChartTemplateProps {
|
|
14
|
+
message: ConversationBotMessage;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function ChartTemplate(props: ChartTemplateProps): ReactNode {
|
|
18
|
+
const { message } = props;
|
|
19
|
+
const template = message.message.template as ChartMessageTemplate;
|
|
20
|
+
|
|
21
|
+
const { template: themeTemplate, botMessage } = useAsgardThemeContext();
|
|
22
|
+
const { avatar } = useAsgardContext();
|
|
23
|
+
|
|
24
|
+
const [option, setOption] = useState(
|
|
25
|
+
template?.defaultChart ?? template?.chartOptions?.[0]?.type
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const options = useMemo(() => template.chartOptions, [template]);
|
|
29
|
+
|
|
30
|
+
const spec = useMemo(
|
|
31
|
+
() =>
|
|
32
|
+
(template?.chartOptions?.find((item) => item.type === option)?.spec ??
|
|
33
|
+
options[0].spec) as VisualizationSpec,
|
|
34
|
+
[option, template.chartOptions]
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const styles = useMemo<CSSProperties>(
|
|
38
|
+
() => ({
|
|
39
|
+
color: botMessage?.color,
|
|
40
|
+
backgroundColor: botMessage?.backgroundColor,
|
|
41
|
+
}),
|
|
42
|
+
[botMessage]
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<TemplateBox
|
|
47
|
+
className="asgard-chart-template"
|
|
48
|
+
type="bot"
|
|
49
|
+
direction="vertical"
|
|
50
|
+
style={themeTemplate?.ChartMessageTemplate?.style}
|
|
51
|
+
>
|
|
52
|
+
<Avatar avatar={avatar} />
|
|
53
|
+
<div className={clsx(classes.text, classes['text--bot'])} style={styles}>
|
|
54
|
+
<div>{template.title}</div>
|
|
55
|
+
<div>{template.text}</div>
|
|
56
|
+
</div>
|
|
57
|
+
{options.length > 1 && (
|
|
58
|
+
<div className={classes.quick_replies_box}>
|
|
59
|
+
{options.map((option) => (
|
|
60
|
+
<button
|
|
61
|
+
key={option.type}
|
|
62
|
+
className={classes.quick_reply}
|
|
63
|
+
onClick={() => setOption(option.type)}
|
|
64
|
+
>
|
|
65
|
+
{option.title}
|
|
66
|
+
</button>
|
|
67
|
+
))}
|
|
68
|
+
</div>
|
|
69
|
+
)}
|
|
70
|
+
<TemplateBoxContent quickReplies={template?.quickReplies}>
|
|
71
|
+
<VegaLite spec={spec} />
|
|
72
|
+
</TemplateBoxContent>
|
|
73
|
+
<Time className={classes.chart_time} time={message.time} />
|
|
74
|
+
</TemplateBox>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
@@ -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 'src/utils';
|
|
4
|
+
import {
|
|
5
|
+
ConversationErrorMessage,
|
|
6
|
+
ConversationMessage,
|
|
7
|
+
MessageTemplateType,
|
|
8
|
+
} from '@asgard-js/core';
|
|
9
|
+
import { useAsgardTemplateContext, useAsgardThemeContext } from 'src/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';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
.image_box {
|
|
2
|
+
display: flex;
|
|
3
|
+
align-items: center;
|
|
4
|
+
justify-content: flex-start;
|
|
5
|
+
max-width: 80%;
|
|
6
|
+
border-radius: 8px;
|
|
7
|
+
box-sizing: border-box;
|
|
8
|
+
> img {
|
|
9
|
+
width: 100%;
|
|
10
|
+
border-radius: 8px;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.time {
|
|
15
|
+
justify-content: flex-end;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.quick_replies_box {
|
|
19
|
+
display: flex;
|
|
20
|
+
flex-wrap: wrap;
|
|
21
|
+
gap: 8px;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.full_screen {
|
|
25
|
+
display: flex;
|
|
26
|
+
align-items: center;
|
|
27
|
+
justify-content: center;
|
|
28
|
+
z-index: 9999;
|
|
29
|
+
position: fixed;
|
|
30
|
+
top: 0;
|
|
31
|
+
left: 0;
|
|
32
|
+
background: rgba(30, 30, 30, 0.8);
|
|
33
|
+
backdrop-filter: blur(5px);
|
|
34
|
+
width: 100%;
|
|
35
|
+
height: 100%;
|
|
36
|
+
overflow: hidden;
|
|
37
|
+
box-sizing: border-box;
|
|
38
|
+
> .imageOrigin {
|
|
39
|
+
width: 100%;
|
|
40
|
+
max-width: 100%;
|
|
41
|
+
height: 100%;
|
|
42
|
+
max-height: 90%;
|
|
43
|
+
border-radius: 8px;
|
|
44
|
+
background-repeat: no-repeat;
|
|
45
|
+
background-size: contain;
|
|
46
|
+
background-position: center;
|
|
47
|
+
}
|
|
48
|
+
> .full_screen_close {
|
|
49
|
+
position: absolute;
|
|
50
|
+
top: 16px;
|
|
51
|
+
right: 16px;
|
|
52
|
+
font-size: 24px;
|
|
53
|
+
cursor: pointer;
|
|
54
|
+
z-index: 9999;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.quick_reply {
|
|
59
|
+
font: inherit;
|
|
60
|
+
font-size: 13px;
|
|
61
|
+
padding: 4px 8px;
|
|
62
|
+
border-radius: 8px;
|
|
63
|
+
border: 1px solid #434343;
|
|
64
|
+
background: rgba(88, 88, 88, 0.2);
|
|
65
|
+
color: white;
|
|
66
|
+
cursor: pointer;
|
|
67
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ReactNode, useState } from 'react';
|
|
2
|
+
import { TemplateBox, TemplateBoxContent } from '../template-box';
|
|
3
|
+
import { Avatar } from '../avatar';
|
|
4
|
+
import styles from './image-template.module.scss';
|
|
5
|
+
import { ConversationBotMessage } from '@asgard-js/core';
|
|
6
|
+
import { useAsgardContext } from 'src/context/asgard-service-context';
|
|
7
|
+
import { useAsgardThemeContext } from 'src/context/asgard-theme-context';
|
|
8
|
+
import { ImageMessageTemplate } from '../../../../../core/src';
|
|
9
|
+
import CloseSvg from 'src/icons/close.svg?react';
|
|
10
|
+
|
|
11
|
+
interface ImageTemplateProps {
|
|
12
|
+
message: ConversationBotMessage;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function ImageTemplate(props: ImageTemplateProps): ReactNode {
|
|
16
|
+
const { message } = props;
|
|
17
|
+
const template = message.message.template as ImageMessageTemplate;
|
|
18
|
+
const { previewImageUrl, originalContentUrl } = template;
|
|
19
|
+
|
|
20
|
+
const { template: themeTemplate } = useAsgardThemeContext();
|
|
21
|
+
|
|
22
|
+
const { avatar } = useAsgardContext();
|
|
23
|
+
const [isFullScreen, setIsFullScreen] = useState(false);
|
|
24
|
+
if (isFullScreen) {
|
|
25
|
+
return (
|
|
26
|
+
<div
|
|
27
|
+
className={styles.full_screen}
|
|
28
|
+
onClick={() => setIsFullScreen(false)}
|
|
29
|
+
>
|
|
30
|
+
<div className={styles.full_screen_close}>
|
|
31
|
+
<CloseSvg />
|
|
32
|
+
</div>
|
|
33
|
+
<div
|
|
34
|
+
className={styles.imageOrigin}
|
|
35
|
+
style={{ backgroundImage: `url(${originalContentUrl})` }}
|
|
36
|
+
/>
|
|
37
|
+
</div>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return (
|
|
41
|
+
<TemplateBox
|
|
42
|
+
className="asgard-image-template"
|
|
43
|
+
type="bot"
|
|
44
|
+
direction="horizontal"
|
|
45
|
+
style={themeTemplate?.ImageMessageTemplate?.style}
|
|
46
|
+
>
|
|
47
|
+
<Avatar avatar={avatar} />
|
|
48
|
+
<TemplateBoxContent
|
|
49
|
+
quickReplies={template.quickReplies}
|
|
50
|
+
time={message.time}
|
|
51
|
+
>
|
|
52
|
+
<div className={styles.image_box} onClick={() => setIsFullScreen(true)}>
|
|
53
|
+
<img src={previewImageUrl} alt="Message image" />
|
|
54
|
+
</div>
|
|
55
|
+
</TemplateBoxContent>
|
|
56
|
+
</TemplateBox>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './image-template';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './template-box';
|
|
2
|
+
|
|
3
|
+
export * from './button-template';
|
|
4
|
+
export * from './text-template';
|
|
5
|
+
export * from './carousel-template';
|
|
6
|
+
export * from './hint-template';
|
|
7
|
+
export * from './chart-template';
|
|
8
|
+
export * from './image-template';
|
|
9
|
+
|
|
10
|
+
export * from './quick-replies';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './quick-replies';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
.quick_replies_box {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-wrap: wrap;
|
|
4
|
+
gap: 8px;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.quick_reply {
|
|
8
|
+
font: inherit;
|
|
9
|
+
font-size: 13px;
|
|
10
|
+
padding: 4px 8px;
|
|
11
|
+
border-radius: 8px;
|
|
12
|
+
border: 1px solid #434343;
|
|
13
|
+
background: rgba(88, 88, 88, 0.2);
|
|
14
|
+
color: white;
|
|
15
|
+
cursor: pointer;
|
|
16
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ReactNode, useCallback } from 'react';
|
|
2
|
+
import styles from './quick-replies.module.scss';
|
|
3
|
+
import { useAsgardContext } from 'src/context/asgard-service-context';
|
|
4
|
+
import { useAsgardThemeContext } from 'src/context/asgard-theme-context';
|
|
5
|
+
import clsx from 'clsx';
|
|
6
|
+
|
|
7
|
+
interface QuickRepliesProps {
|
|
8
|
+
quickReplies: { text: string }[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function QuickReplies(props: QuickRepliesProps): ReactNode {
|
|
12
|
+
const { quickReplies } = props;
|
|
13
|
+
|
|
14
|
+
const { template } = useAsgardThemeContext();
|
|
15
|
+
const { sendMessage, isConnecting } = useAsgardContext();
|
|
16
|
+
|
|
17
|
+
const onClick = useCallback(
|
|
18
|
+
(text: string) => sendMessage?.({ text }),
|
|
19
|
+
[sendMessage]
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
if (!quickReplies?.length) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<div
|
|
28
|
+
className={clsx('asgard-quick-replies', styles.quick_replies_box)}
|
|
29
|
+
style={template?.quickReplies?.style}
|
|
30
|
+
>
|
|
31
|
+
{quickReplies.map((quickReply) => (
|
|
32
|
+
<button
|
|
33
|
+
key={quickReply.text}
|
|
34
|
+
className={styles.quick_reply}
|
|
35
|
+
style={template?.quickReplies?.button?.style}
|
|
36
|
+
disabled={isConnecting}
|
|
37
|
+
onClick={() => onClick(quickReply.text)}
|
|
38
|
+
>
|
|
39
|
+
{quickReply.text}
|
|
40
|
+
</button>
|
|
41
|
+
))}
|
|
42
|
+
</div>
|
|
43
|
+
);
|
|
44
|
+
}
|