@botonic/react 0.27.1-alpha.0 → 0.27.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.
Files changed (54) hide show
  1. package/lib/cjs/components/message/index.js +5 -5
  2. package/lib/cjs/components/message/index.js.map +1 -1
  3. package/lib/cjs/shared/styles.d.ts +1 -0
  4. package/lib/cjs/shared/styles.js +14 -0
  5. package/lib/cjs/shared/styles.js.map +1 -0
  6. package/lib/cjs/webchat/devices/device-adapter.d.ts +1 -1
  7. package/lib/cjs/webchat/devices/device-adapter.js +1 -1
  8. package/lib/cjs/webchat/devices/device-adapter.js.map +1 -1
  9. package/lib/cjs/webchat/message-list/index.d.ts +1 -1
  10. package/lib/cjs/webchat/message-list/index.js +40 -25
  11. package/lib/cjs/webchat/message-list/index.js.map +1 -1
  12. package/lib/cjs/webchat/message-list/intro-message.js +4 -5
  13. package/lib/cjs/webchat/message-list/intro-message.js.map +1 -1
  14. package/lib/cjs/webchat/message-list/scroll-button.js +2 -6
  15. package/lib/cjs/webchat/message-list/scroll-button.js.map +1 -1
  16. package/lib/cjs/webchat/message-list/unread-messages-banner.js +4 -15
  17. package/lib/cjs/webchat/message-list/unread-messages-banner.js.map +1 -1
  18. package/lib/cjs/webchat/message-list/use-notifications.d.ts +7 -0
  19. package/lib/cjs/webchat/message-list/use-notifications.js +24 -0
  20. package/lib/cjs/webchat/message-list/use-notifications.js.map +1 -0
  21. package/lib/cjs/webchat/webchat.js +2 -2
  22. package/lib/cjs/webchat/webchat.js.map +1 -1
  23. package/lib/esm/components/message/index.js +1 -1
  24. package/lib/esm/components/message/index.js.map +1 -1
  25. package/lib/esm/shared/styles.d.ts +1 -0
  26. package/lib/esm/shared/styles.js +10 -0
  27. package/lib/esm/shared/styles.js.map +1 -0
  28. package/lib/esm/webchat/devices/device-adapter.d.ts +1 -1
  29. package/lib/esm/webchat/devices/device-adapter.js +1 -1
  30. package/lib/esm/webchat/devices/device-adapter.js.map +1 -1
  31. package/lib/esm/webchat/message-list/index.d.ts +1 -1
  32. package/lib/esm/webchat/message-list/index.js +40 -25
  33. package/lib/esm/webchat/message-list/index.js.map +1 -1
  34. package/lib/esm/webchat/message-list/intro-message.js +1 -1
  35. package/lib/esm/webchat/message-list/intro-message.js.map +1 -1
  36. package/lib/esm/webchat/message-list/scroll-button.js +2 -6
  37. package/lib/esm/webchat/message-list/scroll-button.js.map +1 -1
  38. package/lib/esm/webchat/message-list/unread-messages-banner.js +5 -16
  39. package/lib/esm/webchat/message-list/unread-messages-banner.js.map +1 -1
  40. package/lib/esm/webchat/message-list/use-notifications.d.ts +7 -0
  41. package/lib/esm/webchat/message-list/use-notifications.js +20 -0
  42. package/lib/esm/webchat/message-list/use-notifications.js.map +1 -0
  43. package/lib/esm/webchat/webchat.js +2 -2
  44. package/lib/esm/webchat/webchat.js.map +1 -1
  45. package/package.json +1 -2
  46. package/src/components/message/index.jsx +1 -1
  47. package/src/shared/styles.ts +11 -0
  48. package/src/webchat/devices/device-adapter.js +1 -1
  49. package/src/webchat/message-list/index.tsx +49 -29
  50. package/src/webchat/message-list/intro-message.tsx +1 -1
  51. package/src/webchat/message-list/scroll-button.tsx +2 -11
  52. package/src/webchat/message-list/unread-messages-banner.tsx +6 -32
  53. package/src/webchat/message-list/use-notifications.ts +46 -0
  54. package/src/webchat/webchat.jsx +2 -2
@@ -2,15 +2,15 @@ import React, { useContext, useEffect, useRef, useState } from 'react'
2
2
 
3
3
  import { ROLES, WEBCHAT } from '../../constants'
4
4
  import { WebchatContext } from '../../contexts'
5
- import { scrollToBottom } from '../../util'
6
5
  import { StyledScrollbar } from '../components/styled-scrollbar'
7
6
  import { TypingIndicator } from '../components/typing-indicator'
8
7
  import { IntroMessage } from './intro-message'
9
8
  import { ScrollButton } from './scroll-button'
10
9
  import { ContainerMessage } from './styles'
11
10
  import { UnreadMessagesBanner } from './unread-messages-banner'
11
+ import { useNotifications } from './use-notifications'
12
12
 
13
- export const WebchatMessageList = props => {
13
+ export const WebchatMessageList = () => {
14
14
  const {
15
15
  webchatState,
16
16
  getThemeProperty,
@@ -25,11 +25,31 @@ export const WebchatMessageList = props => {
25
25
 
26
26
  const [firstUnreadMessageId, setFirstUnreadMessageId] = useState()
27
27
 
28
- const lastMessageRef = useRef<HTMLDivElement>(null)
28
+ const lastMessageBottomRef = useRef<HTMLDivElement>(null)
29
+
30
+ const scrollToBottom = () => {
31
+ setTimeout(() => {
32
+ lastMessageBottomRef.current?.scrollIntoView({
33
+ behavior: 'smooth',
34
+ block: 'end',
35
+ })
36
+ }, 100)
37
+ }
29
38
 
30
39
  const handleScrollToBottom = () => {
31
40
  resetUnreadMessages()
32
- scrollToBottom({ host: props.host })
41
+ scrollToBottom()
42
+ }
43
+
44
+ const unreadMessagesBannerRef = useRef<HTMLDivElement>(null)
45
+
46
+ const scrollToBanner = () => {
47
+ setTimeout(() => {
48
+ unreadMessagesBannerRef.current?.scrollIntoView({
49
+ behavior: 'smooth',
50
+ block: 'center',
51
+ })
52
+ }, 100)
33
53
  }
34
54
 
35
55
  const showUnreadMessagesBanner = (messageComponentId: string) =>
@@ -37,8 +57,6 @@ export const WebchatMessageList = props => {
37
57
  messageComponentId === firstUnreadMessageId &&
38
58
  webchatState.numUnreadMessages > 0
39
59
 
40
- const unreadMessagesBannerRef = useRef<HTMLDivElement>(null)
41
-
42
60
  useEffect(() => {
43
61
  const firstUnreadMessage = webchatState.messagesComponents.find(
44
62
  message => message.props.isUnread
@@ -47,39 +65,39 @@ export const WebchatMessageList = props => {
47
65
  }, [webchatState.messagesComponents])
48
66
 
49
67
  useEffect(() => {
50
- if (webchatState.messagesComponents.length > 0 && lastMessageRef.current) {
68
+ if (
69
+ webchatState.messagesComponents.length > 0 &&
70
+ lastMessageBottomRef.current
71
+ ) {
51
72
  const observer = new IntersectionObserver(entries => {
52
73
  entries.forEach(entry => {
53
74
  setLastMessageVisible(entry.isIntersecting)
54
75
  })
55
76
  })
56
- observer.observe(lastMessageRef.current)
77
+ observer.observe(lastMessageBottomRef.current)
57
78
  }
58
79
  }, [webchatState.messagesComponents])
59
80
 
81
+ const { notificationsEnabled } = useNotifications()
82
+
60
83
  useEffect(() => {
61
- setTimeout(() => {
62
- scrollToBottom({ host: props.host })
63
- }, 100)
84
+ if (!notificationsEnabled) {
85
+ scrollToBottom()
86
+ return
87
+ }
64
88
  }, [webchatState.typing])
65
89
 
66
90
  useEffect(() => {
67
- if (firstUnreadMessageId) {
68
- if (unreadMessagesBannerRef.current) {
69
- unreadMessagesBannerRef.current.scrollIntoView({
70
- behavior: 'smooth',
71
- block: 'center',
72
- })
91
+ if (notificationsEnabled) {
92
+ if (webchatState.isWebchatOpen && unreadMessagesBannerRef.current) {
93
+ scrollToBanner()
73
94
  return
74
95
  }
75
- } else if (lastMessageRef.current) {
76
- lastMessageRef.current.scrollIntoView({
77
- behavior: 'smooth',
78
- block: 'end',
79
- })
96
+
97
+ scrollToBottom()
80
98
  return
81
99
  }
82
- }, [firstUnreadMessageId])
100
+ }, [firstUnreadMessageId, webchatState.isWebchatOpen, webchatState.typing])
83
101
 
84
102
  const showScrollButton =
85
103
  webchatState.numUnreadMessages > 0 && !webchatState.isLastMessageVisible
@@ -94,9 +112,7 @@ export const WebchatMessageList = props => {
94
112
  scrollbar={scrollbarOptions}
95
113
  autoHide={scrollbarOptions.autoHide}
96
114
  ismessagescontainer={true.toString()}
97
- style={{
98
- ...props.style,
99
- }}
115
+ style={{ flex: 1 }}
100
116
  >
101
117
  <IntroMessage />
102
118
  {webchatState.messagesComponents.map((messageComponent, index) => {
@@ -107,11 +123,15 @@ export const WebchatMessageList = props => {
107
123
  unreadMessagesBannerRef={unreadMessagesBannerRef}
108
124
  />
109
125
  )}
110
-
126
+ {messageComponent}
111
127
  {index === webchatState.messagesComponents.length - 1 && (
112
- <div ref={lastMessageRef} style={{ content: '' }}></div>
128
+ <div
129
+ ref={lastMessageBottomRef}
130
+ style={{
131
+ content: '',
132
+ }}
133
+ ></div>
113
134
  )}
114
- {messageComponent}
115
135
  </ContainerMessage>
116
136
  )
117
137
  })}
@@ -1,8 +1,8 @@
1
1
  import React, { useContext } from 'react'
2
- import Fade from 'react-reveal/Fade'
3
2
 
4
3
  import { WEBCHAT } from '../../constants'
5
4
  import { WebchatContext } from '../../contexts'
5
+ import { Fade } from '../../shared/styles'
6
6
  import { resolveImage } from '../../util/environment'
7
7
  import { ConditionalWrapper } from '../../util/react'
8
8
  import { DefaultIntroImage } from './styles'
@@ -6,6 +6,7 @@ import { WebchatContext } from '../../contexts'
6
6
  import { resolveImage } from '../../util/environment'
7
7
  import { ContainerScrollButton } from './styles'
8
8
  import { useDebounce } from './use-debounce'
9
+ import { useNotifications } from './use-notifications'
9
10
 
10
11
  interface ScrollButtonProps {
11
12
  handleClick: () => void
@@ -14,20 +15,10 @@ interface ScrollButtonProps {
14
15
  export const ScrollButton = ({
15
16
  handleClick,
16
17
  }: ScrollButtonProps): JSX.Element => {
17
- const { getThemeProperty } = useContext(WebchatContext)
18
+ const { CustomScrollButton, scrollButtonEnabled } = useNotifications()
18
19
 
19
20
  const show = useDebounce()
20
21
 
21
- const CustomScrollButton = getThemeProperty(
22
- WEBCHAT.CUSTOM_PROPERTIES.scrollButtonCustom,
23
- undefined
24
- )
25
-
26
- const scrollButtonEnabled = getThemeProperty(
27
- WEBCHAT.CUSTOM_PROPERTIES.scrollButtonEnabled,
28
- CustomScrollButton
29
- )
30
-
31
22
  return (
32
23
  <>
33
24
  {scrollButtonEnabled && show ? (
@@ -1,10 +1,10 @@
1
- import React, { useContext, useEffect } from 'react'
1
+ import React, { useContext } from 'react'
2
2
 
3
3
  import ArrowDown from '../../assets/arrow-down.svg'
4
- import { WEBCHAT } from '../../constants'
5
4
  import { WebchatContext } from '../../contexts'
6
5
  import { resolveImage } from '../../util/environment'
7
6
  import { ContainerUnreadMessagesBanner } from './styles'
7
+ import { useNotifications } from './use-notifications'
8
8
 
9
9
  interface UnreadMessagesBannerProps {
10
10
  unreadMessagesBannerRef: React.RefObject<HTMLDivElement>
@@ -13,36 +13,10 @@ interface UnreadMessagesBannerProps {
13
13
  export const UnreadMessagesBanner = ({
14
14
  unreadMessagesBannerRef,
15
15
  }: UnreadMessagesBannerProps): JSX.Element => {
16
- const { getThemeProperty, webchatState } = useContext(WebchatContext)
16
+ const { webchatState } = useContext(WebchatContext)
17
17
 
18
- const CustomUnreadMessagesBanner = getThemeProperty(
19
- WEBCHAT.CUSTOM_PROPERTIES.notificationsBannerCustom,
20
- undefined
21
- )
22
-
23
- const notificationsBannerEnabled = getThemeProperty(
24
- WEBCHAT.CUSTOM_PROPERTIES.notificationsBannerEnabled,
25
- undefined
26
- )
27
-
28
- const notificationsEnabled = getThemeProperty(
29
- WEBCHAT.CUSTOM_PROPERTIES.notificationsEnabled,
30
- CustomUnreadMessagesBanner || notificationsBannerEnabled
31
- )
32
-
33
- const text = getThemeProperty(
34
- WEBCHAT.CUSTOM_PROPERTIES.notificationsBannerText,
35
- 'unread messages'
36
- )
37
-
38
- useEffect(() => {
39
- if (webchatState.isWebchatOpen && unreadMessagesBannerRef.current) {
40
- unreadMessagesBannerRef.current.scrollIntoView({
41
- behavior: 'smooth',
42
- block: 'center',
43
- })
44
- }
45
- }, [webchatState.isWebchatOpen, unreadMessagesBannerRef])
18
+ const { notificationsEnabled, CustomUnreadMessagesBanner, bannerText } =
19
+ useNotifications()
46
20
 
47
21
  return (
48
22
  <>
@@ -53,7 +27,7 @@ export const UnreadMessagesBanner = ({
53
27
  ) : (
54
28
  <ContainerUnreadMessagesBanner>
55
29
  <img src={resolveImage(ArrowDown)} />
56
- {webchatState.numUnreadMessages} {text}
30
+ {webchatState.numUnreadMessages} {bannerText}
57
31
  </ContainerUnreadMessagesBanner>
58
32
  )}
59
33
  </div>
@@ -0,0 +1,46 @@
1
+ import { useContext } from 'react'
2
+
3
+ import { WEBCHAT } from '../../constants'
4
+ import { WebchatContext } from '../../contexts'
5
+
6
+ export function useNotifications() {
7
+ const { getThemeProperty } = useContext(WebchatContext)
8
+
9
+ const CustomUnreadMessagesBanner = getThemeProperty(
10
+ WEBCHAT.CUSTOM_PROPERTIES.notificationsBannerCustom,
11
+ undefined
12
+ )
13
+
14
+ const notificationsBannerEnabled = getThemeProperty(
15
+ WEBCHAT.CUSTOM_PROPERTIES.notificationsBannerEnabled,
16
+ undefined
17
+ )
18
+
19
+ const notificationsEnabled = getThemeProperty(
20
+ WEBCHAT.CUSTOM_PROPERTIES.notificationsEnabled,
21
+ CustomUnreadMessagesBanner || notificationsBannerEnabled
22
+ )
23
+
24
+ const bannerText = getThemeProperty(
25
+ WEBCHAT.CUSTOM_PROPERTIES.notificationsBannerText,
26
+ 'unread messages'
27
+ )
28
+
29
+ const CustomScrollButton = getThemeProperty(
30
+ WEBCHAT.CUSTOM_PROPERTIES.scrollButtonCustom,
31
+ undefined
32
+ )
33
+
34
+ const scrollButtonEnabled = getThemeProperty(
35
+ WEBCHAT.CUSTOM_PROPERTIES.scrollButtonEnabled,
36
+ CustomScrollButton
37
+ )
38
+
39
+ return {
40
+ notificationsEnabled,
41
+ bannerText,
42
+ CustomUnreadMessagesBanner,
43
+ CustomScrollButton,
44
+ scrollButtonEnabled,
45
+ }
46
+ }
@@ -753,7 +753,7 @@ export const Webchat = forwardRef((props, ref) => {
753
753
  inputRef={textArea}
754
754
  name='text'
755
755
  onFocus={() => {
756
- deviceAdapter.onFocus(host)
756
+ deviceAdapter.onFocus()
757
757
  }}
758
758
  onBlur={() => {
759
759
  deviceAdapter.onBlur()
@@ -907,7 +907,7 @@ export const Webchat = forwardRef((props, ref) => {
907
907
  </ErrorMessageContainer>
908
908
  )}
909
909
 
910
- <WebchatMessageList style={{ flex: 1 }} host={host} />
910
+ <WebchatMessageList />
911
911
 
912
912
  {webchatState.replies &&
913
913
  Object.keys(webchatState.replies).length > 0 && (