Skip to content

Commit 1f5c09e

Browse files
authored
Merge pull request #17 from Cognigy/bug/74861-fix-qr-buttons-disable-issue
Fix issues with controlCommands messages
2 parents aebcc4a + 548143e commit 1f5c09e

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

src/webchat-ui/components/WebchatUI.tsx

+9-5
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import XAppOverlay from "./functional/xapp-overlay/XAppOverlay";
6767
import { getSourceBackgroundColor } from "../utils/sourceMapping";
6868
import type { Options } from "@cognigy/socket-client/lib/interfaces/options";
6969
import speechOutput from "./plugins/speech-output";
70-
import getMessagesListWithoutPrivacyMessage from "../utils/filter-out-privacy-message";
70+
import getMessagesListWithoutControlCommands from "../utils/filter-out-control-commands";
7171

7272
export interface WebchatUIProps {
7373
currentSession: string;
@@ -1382,7 +1382,7 @@ export class WebchatUI extends React.PureComponent<
13821382

13831383
// Find privacy message and remove it from the messages list (these message types are not displayed in the chat log).
13841384
// If we do not remove, it will cause the collatation of the first user message.
1385-
const messagesExcludingPrivacyMessage = getMessagesListWithoutPrivacyMessage(messages);
1385+
const messagesExcludingPrivacyMessage = getMessagesListWithoutControlCommands(messages, ["acceptPrivacyPolicy"]);
13861386

13871387
return (
13881388
<>
@@ -1391,9 +1391,13 @@ export class WebchatUI extends React.PureComponent<
13911391
</TopStatusMessage>
13921392
{messagesExcludingPrivacyMessage.map((message, index) => {
13931393
// Lookahead if there is a user reply
1394-
const hasReply = messages
1394+
const hasReply = messagesExcludingPrivacyMessage
13951395
.slice(index + 1)
1396-
.some(message => message.source === "user");
1396+
.some(
1397+
message =>
1398+
message.source === "user" &&
1399+
!(message?.data?._cognigy as any)?.controlCommands,
1400+
);
13971401

13981402
return (
13991403
<Message
@@ -1408,7 +1412,7 @@ export class WebchatUI extends React.PureComponent<
14081412
onSetFullscreen={() => this.props.onSetFullscreenMessage(message)}
14091413
openXAppOverlay={openXAppOverlay}
14101414
plugins={messagePlugins}
1411-
prevMessage={messages?.[index - 1]}
1415+
prevMessage={messagesExcludingPrivacyMessage?.[index - 1]}
14121416
theme={this.state.theme}
14131417
/>
14141418
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { IMessageEvent } from "../../common/interfaces/event";
2+
import { IMessage } from "../../common/interfaces/message";
3+
4+
const EXCLUDE_CC_TYPES = ["acceptPrivacyPolicy", "setRating"];
5+
6+
const getMessagesListWithoutControlCommands = (
7+
messages: (IMessage | IMessageEvent)[],
8+
types: string[] = EXCLUDE_CC_TYPES,
9+
) => {
10+
return messages.filter(message => {
11+
if (message.data?._cognigy && (message.data._cognigy as any).controlCommands) {
12+
return !(message.data._cognigy as any).controlCommands.some((controlCommand: any) =>
13+
types.includes(controlCommand.type),
14+
);
15+
}
16+
return true;
17+
});
18+
};
19+
20+
export default getMessagesListWithoutControlCommands;

src/webchat-ui/utils/filter-out-privacy-message.ts

-12
This file was deleted.

src/webchat/store/autoinject/autoinject-middleware.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { StoreState } from "../store";
33
import { autoInjectHandled, TAutoInjectAction, triggerAutoInject } from './autoinject-reducer';
44
import { Webchat } from "../../components/Webchat";
55
import { IWebchatConfig } from "../../../common/interfaces/webchat-config";
6-
import getMessagesListWithoutPrivacyMessage from "../../../webchat-ui/utils/filter-out-privacy-message";
6+
import getMessagesListWithoutControlCommands from "../../../webchat-ui/utils/filter-out-control-commands";
77

88
export const createAutoInjectMiddleware = (webchat: Webchat): Middleware<unknown, StoreState> => api => next => (action: TAutoInjectAction) => {
99
switch (action.type) {
@@ -50,11 +50,11 @@ export const createAutoInjectMiddleware = (webchat: Webchat): Middleware<unknown
5050
if (!config.settings.widgetSettings.enableInjectionWithoutEmptyHistory) {
5151
// Exclude engagement messages from state.messages
5252
const messagesExcludingEngagementMessages = state.messages?.filter(message => message.source !== 'engagement');
53-
// Exclude privacy policy accepted message type from filtered message list
54-
const messagesExcludingEngagementAndPrivacyMessage = getMessagesListWithoutPrivacyMessage(messagesExcludingEngagementMessages);
55-
const isEmptyExceptEngagementAndPrivacyMessage = messagesExcludingEngagementAndPrivacyMessage.length === 0;
53+
// Exclude controlCommands messages from filtered message list
54+
const messagesExcludingControlCommands = getMessagesListWithoutControlCommands(messagesExcludingEngagementMessages);
55+
const isEmptyExceptEngagementAndControlCommands = messagesExcludingControlCommands.length === 0;
5656

57-
if (!isEmptyExceptEngagementAndPrivacyMessage) {
57+
if (!isEmptyExceptEngagementAndControlCommands) {
5858
break;
5959
}
6060
}

0 commit comments

Comments
 (0)