Skip to content

Commit 8a943f9

Browse files
authored
Merge pull request #89 from SAPConversationalAI/mergeToOpenSource
Merge to open source
2 parents 0638f93 + b61da88 commit 8a943f9

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webchat",
3-
"version": "1.4.46",
3+
"version": "1.4.47",
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {

src/components/Live/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import pathOr from 'ramda/es/pathOr'
66
import Message from 'components/Message'
77
import IsTyping from 'components/Message/isTyping'
88

9+
import { isLastMessageForIndex } from 'helpers'
10+
911
import './style.scss'
1012

1113
class Live extends Component {
@@ -119,7 +121,7 @@ class Live extends Component {
119121
sendMessage={sendMessage}
120122
preferences={preferences}
121123
onImageLoaded={this.onImageLoaded}
122-
isLastMessage={messages.length === index + 1}
124+
isLastMessage={isLastMessageForIndex(messages, index)}
123125
retry={message.retry}
124126
isSending={message.isSending}
125127
onRetrySendMessage={() => onRetrySendMessage(message)}

src/helpers.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import { pathOr } from 'ramda'
3+
24
export const truncate = (string, length) => {
35
// console.assert(typeof string === 'string', `Expected a 'string', but got a type:'${typeof string}' - '${string}'`)
46
if (typeof string === 'string') {
@@ -83,12 +85,40 @@ export const safeStringValue = (content) => {
8385
export const validButtonContent = (element) => {
8486
if (element) {
8587
const { type, value, title } = element
86-
const data = {
88+
return {
8789
type,
8890
value,
8991
title,
9092
}
91-
return data
9293
}
9394
return element
9495
}
96+
97+
/**
98+
* Determine if this is the last message, ignore client_data types at the end
99+
* Only for Quick Replies
100+
*
101+
* @param {*} messages - Array of messages
102+
* @param {*} index - 0 based index to test
103+
* @returns
104+
*/
105+
export const isLastMessageForIndex = (messages, index) => {
106+
if (messages && Array.isArray(messages) && index >= 0 && index < messages.length) {
107+
let offset = 1
108+
const messageType = pathOr('', ['attachment', 'type'], messages[index])
109+
if (typeof messageType === 'string' && messageType.toLowerCase() === 'quickreplies') {
110+
// Loop back to the index to adjust offset for client_data type
111+
for (let i = messages.length - 1; i > index; --i) {
112+
const type = pathOr('', ['attachment', 'type'], messages[i])
113+
if (typeof type === 'string' && type.toLowerCase() === 'client_data') {
114+
offset++
115+
} else {
116+
break
117+
}
118+
}
119+
}
120+
// Should return true if this is a type client_data and is the last message?
121+
return messages.length === index + offset
122+
}
123+
return false // No messsage or index out of range
124+
}

src/test/helpers.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
validButtonContent,
88
getCredentialsFromLocalStorage,
99
storeCredentialsToLocalStorage,
10+
isLastMessageForIndex,
1011
} from 'helpers'
1112

1213
describe('Test the helper utilities', () => {
@@ -50,5 +51,46 @@ describe('Test the helper utilities', () => {
5051
storeCredentialsToLocalStorage('chatId', 'conversation-Id', 1, 'channelId')
5152
assert.propertyVal(getCredentialsFromLocalStorage('channelId', 1), 'conversationId', 'conversation-Id')
5253
})
54+
55+
it('Test isLastMessageForIndex', () => {
56+
assert.isFalse(isLastMessageForIndex(), 'No params')
57+
assert.isFalse(isLastMessageForIndex([], 0), 'Empty params')
58+
assert.isTrue(isLastMessageForIndex([{ attachment: { type: 'foo' } }], 0), 'Is first and last message')
59+
assert.isTrue(isLastMessageForIndex([
60+
{ attachment: { type: 'quickreplies' } },
61+
{ attachment: { type: 'client_data' } }], 0), 'Has client data after the first (quickreplies) message')
62+
assert.isFalse(isLastMessageForIndex([
63+
{ attachment: { type: 'text' } },
64+
{ attachment: { type: 'client_data' } }], 0), 'Has client data after the first (text) message')
65+
assert.isTrue(isLastMessageForIndex([
66+
{ attachment: { type: 'quickreplies' } },
67+
{ attachment: { type: 'client_data' } },
68+
{ attachment: { type: 'client_data' } }], 0), 'Has two client data after the first message')
69+
assert.isTrue(isLastMessageForIndex([{ attachment: { type: 'client_data' } }], 0), 'Only client data')
70+
assert.isFalse(isLastMessageForIndex([
71+
{ attachment: { type: 'quickreplies' } },
72+
{ attachment: { type: 'client_data' } },
73+
{ attachment: { type: 'buttons' } },
74+
{ attachment: { type: 'client_data' } }], 0))
75+
assert.isFalse(isLastMessageForIndex([
76+
{ attachment: { type: 'quickreplies' } },
77+
{ attachment: { type: 'client_data' } },
78+
{ attachment: { type: 'buttons' } },
79+
{ attachment: { type: 'client_data' } }], 1))
80+
assert.isTrue(isLastMessageForIndex([
81+
{ attachment: { type: 'button' } },
82+
{ attachment: { type: 'client_data' } },
83+
{ attachment: { type: 'quickreplies' } },
84+
{ attachment: { type: 'client_data' } }], 2))
85+
assert.isFalse(isLastMessageForIndex([
86+
{ attachment: { type: 'quickreplies' } },
87+
{ attachment: { type: 'buttons' } }], 2), 'Test out of range')
88+
assert.isTrue(isLastMessageForIndex([
89+
{ attachment: { type: 'quickreplies' } },
90+
{ attachment: { type: 'buttons' } }], 1), 'Test is Last one')
91+
assert.isFalse(isLastMessageForIndex([
92+
{ attachment: { type: 'quickreplies' } },
93+
{ attachment: { type: 'buttons' } }], 0), 'Is First message')
94+
})
5395
})
5496

0 commit comments

Comments
 (0)