@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.
Files changed (138) hide show
  1. package/.babelrc +12 -0
  2. package/README.md +13 -13
  3. package/dist/components/chatbot/chatbot-header/chatbot-header.d.ts +1 -1
  4. package/dist/components/chatbot/chatbot-header/chatbot-header.d.ts.map +1 -1
  5. package/dist/components/chatbot/chatbot.d.ts +2 -1
  6. package/dist/components/chatbot/chatbot.d.ts.map +1 -1
  7. package/dist/components/templates/button-template/card.d.ts.map +1 -1
  8. package/dist/components/templates/text-template/{use-markdown-renderer.d.ts → use-react-markdown-renderer.d.ts} +3 -1
  9. package/dist/components/templates/text-template/use-react-markdown-renderer.d.ts.map +1 -0
  10. package/dist/context/asgard-template-context.d.ts +2 -0
  11. package/dist/context/asgard-template-context.d.ts.map +1 -1
  12. package/dist/hooks/index.d.ts +1 -1
  13. package/dist/hooks/index.d.ts.map +1 -1
  14. package/dist/hooks/use-channel.d.ts +1 -1
  15. package/dist/hooks/use-channel.d.ts.map +1 -1
  16. package/dist/index.js +74570 -54207
  17. package/dist/style.css +1 -1
  18. package/dist/utils/uri-validation.d.ts +20 -0
  19. package/dist/utils/uri-validation.d.ts.map +1 -0
  20. package/eslint.config.cjs +12 -0
  21. package/package.json +15 -9
  22. package/src/components/chatbot/chatbot-body/chatbot-body.module.scss +13 -0
  23. package/src/components/chatbot/chatbot-body/chatbot-body.tsx +45 -0
  24. package/src/components/chatbot/chatbot-body/conversation-message-renderer.tsx +55 -0
  25. package/src/components/chatbot/chatbot-body/index.ts +1 -0
  26. package/src/components/chatbot/chatbot-container/chatbot-container.module.scss +41 -0
  27. package/src/components/chatbot/chatbot-container/chatbot-container.tsx +49 -0
  28. package/src/components/chatbot/chatbot-container/chatbot-full-screen-container.tsx +54 -0
  29. package/src/components/chatbot/chatbot-footer/chatbot-footer.module.scss +67 -0
  30. package/src/components/chatbot/chatbot-footer/chatbot-footer.tsx +140 -0
  31. package/src/components/chatbot/chatbot-footer/index.ts +1 -0
  32. package/src/components/chatbot/chatbot-footer/speech-input-button.tsx +132 -0
  33. package/src/components/chatbot/chatbot-header/chatbot-header.module.scss +48 -0
  34. package/src/components/chatbot/chatbot-header/chatbot-header.tsx +98 -0
  35. package/src/components/chatbot/chatbot-header/index.ts +1 -0
  36. package/src/components/chatbot/chatbot.spec.tsx +8 -0
  37. package/src/components/chatbot/chatbot.tsx +118 -0
  38. package/src/components/chatbot/profile-icon.tsx +26 -0
  39. package/src/components/index.ts +2 -0
  40. package/src/components/templates/avatar/avatar.module.scss +6 -0
  41. package/src/components/templates/avatar/avatar.tsx +28 -0
  42. package/src/components/templates/avatar/index.ts +1 -0
  43. package/src/components/templates/button-template/button-template.module.scss +0 -0
  44. package/src/components/templates/button-template/button-template.tsx +45 -0
  45. package/src/components/templates/button-template/card.module.scss +58 -0
  46. package/src/components/templates/button-template/card.spec.tsx +213 -0
  47. package/src/components/templates/button-template/card.tsx +123 -0
  48. package/src/components/templates/button-template/index.ts +1 -0
  49. package/src/components/templates/carousel-template/carousel-template.module.scss +15 -0
  50. package/src/components/templates/carousel-template/carousel-template.tsx +48 -0
  51. package/src/components/templates/carousel-template/index.ts +1 -0
  52. package/src/components/templates/chart-template/chart-template.module.scss +52 -0
  53. package/src/components/templates/chart-template/chart-template.tsx +76 -0
  54. package/src/components/templates/chart-template/index.ts +1 -0
  55. package/src/components/templates/hint-template/hint-template.module.scss +39 -0
  56. package/src/components/templates/hint-template/hint-template.tsx +71 -0
  57. package/src/components/templates/hint-template/index.ts +1 -0
  58. package/src/components/templates/image-template/image-template.module.scss +67 -0
  59. package/src/components/templates/image-template/image-template.tsx +58 -0
  60. package/src/components/templates/image-template/index.ts +1 -0
  61. package/src/components/templates/index.ts +10 -0
  62. package/src/components/templates/quick-replies/index.ts +1 -0
  63. package/src/components/templates/quick-replies/quick-replies.module.scss +16 -0
  64. package/src/components/templates/quick-replies/quick-replies.tsx +44 -0
  65. package/src/components/templates/template-box/index.ts +2 -0
  66. package/src/components/templates/template-box/template-box-content.module.scss +13 -0
  67. package/src/components/templates/template-box/template-box-content.tsx +30 -0
  68. package/src/components/templates/template-box/template-box.module.scss +19 -0
  69. package/src/components/templates/template-box/template-box.tsx +48 -0
  70. package/src/components/templates/text-template/bot-typing-box.tsx +81 -0
  71. package/src/components/templates/text-template/bot-typing-placeholder.tsx +28 -0
  72. package/src/components/templates/text-template/index.ts +3 -0
  73. package/src/components/templates/text-template/text-template.module.scss +131 -0
  74. package/src/components/templates/text-template/text-template.tsx +90 -0
  75. package/src/components/templates/text-template/use-react-markdown-renderer.spec.tsx +758 -0
  76. package/src/components/templates/text-template/use-react-markdown-renderer.tsx +264 -0
  77. package/src/components/templates/time/index.ts +1 -0
  78. package/src/components/templates/time/time.module.scss +6 -0
  79. package/src/components/templates/time/time.tsx +34 -0
  80. package/src/context/asgard-app-initialization-context.tsx +154 -0
  81. package/src/context/asgard-service-context.tsx +139 -0
  82. package/src/context/asgard-template-context.tsx +83 -0
  83. package/src/context/asgard-theme-context.tsx +401 -0
  84. package/src/context/index.ts +4 -0
  85. package/src/hooks/index.ts +11 -0
  86. package/src/hooks/use-asgard-service-client.ts +68 -0
  87. package/src/hooks/use-channel.ts +154 -0
  88. package/src/hooks/use-debounce.ts +18 -0
  89. package/src/hooks/use-deep-compare-memo.ts +19 -0
  90. package/src/hooks/use-is-on-screen-keyboard-open.ts +43 -0
  91. package/src/hooks/use-on-screen-keyboard-scroll-fix.ts +15 -0
  92. package/src/hooks/use-prevent-over-scrolling.ts +77 -0
  93. package/src/hooks/use-resize-observer.tsx +27 -0
  94. package/src/hooks/use-update-vh.ts +30 -0
  95. package/src/hooks/use-viewport-size.ts +51 -0
  96. package/src/icons/add_a_photo.svg +3 -0
  97. package/src/icons/bot.svg +14 -0
  98. package/src/icons/close.svg +3 -0
  99. package/src/icons/distance.svg +3 -0
  100. package/src/icons/mic.svg +3 -0
  101. package/src/icons/photo_library.svg +3 -0
  102. package/src/icons/profile.svg +28 -0
  103. package/src/icons/refresh.svg +3 -0
  104. package/src/icons/send.svg +3 -0
  105. package/src/icons/stop.svg +22 -0
  106. package/src/icons/volume_up.svg +3 -0
  107. package/src/index.ts +4 -0
  108. package/src/models/bot-provider.ts +108 -0
  109. package/src/styles/_index.scss +1 -0
  110. package/src/styles/_styles.scss +11 -0
  111. package/src/styles/colors/_colors.scss +10 -0
  112. package/src/styles/colors/_index.scss +1 -0
  113. package/src/styles/colors/_variables.scss +72 -0
  114. package/src/styles/palette/_index.scss +1 -0
  115. package/src/styles/palette/_palette.scss +42 -0
  116. package/src/styles/palette/_variables.scss +40 -0
  117. package/src/styles/radius/_index.scss +1 -0
  118. package/src/styles/radius/_radius.scss +8 -0
  119. package/src/styles/radius/_variables.scss +12 -0
  120. package/src/styles/spacing/_index.scss +1 -0
  121. package/src/styles/spacing/_spacing.scss +8 -0
  122. package/src/styles/spacing/_variables.scss +13 -0
  123. package/src/styles/utils/_index.scss +1 -0
  124. package/src/styles/utils/_map.scss +22 -0
  125. package/src/test-setup.ts +1 -0
  126. package/src/utils/deep-merge.ts +23 -0
  127. package/src/utils/extractors.ts +20 -0
  128. package/src/utils/format-time.ts +8 -0
  129. package/src/utils/index.ts +1 -0
  130. package/src/utils/is.ts +72 -0
  131. package/src/utils/selectors.ts +7 -0
  132. package/src/utils/uri-validation.spec.ts +208 -0
  133. package/src/utils/uri-validation.ts +103 -0
  134. package/tsconfig.json +16 -0
  135. package/tsconfig.lib.json +62 -0
  136. package/tsconfig.spec.json +36 -0
  137. package/vite.config.ts +63 -0
  138. 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,15 @@
1
+ .carousel_root {
2
+ width: 100%;
3
+ display: flex;
4
+ flex-wrap: nowrap;
5
+ overflow-x: scroll;
6
+ gap: 8px;
7
+
8
+ > div {
9
+ flex: 0 0 auto;
10
+ }
11
+ }
12
+
13
+ .carousel_time {
14
+ justify-content: flex-end;
15
+ }
@@ -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
+ }
@@ -0,0 +1,2 @@
1
+ export * from './template-box';
2
+ export * from './template-box-content';
@@ -0,0 +1,13 @@
1
+ .template_box_content {
2
+ width: 100%;
3
+ display: flex;
4
+ flex-direction: column;
5
+ gap: 8px;
6
+ }
7
+
8
+ .content {
9
+ width: 100%;
10
+ display: flex;
11
+ flex-direction: row;
12
+ gap: 8px;
13
+ }