fix(widget): support showing quick replies after a refresh

This commit is contained in:
yassinedorbozgithub 2025-05-09 15:29:56 +01:00
parent 76ecb900c5
commit 0eaef3cf60

View File

@ -20,7 +20,9 @@ import { useSubscribeBroadcastChannel } from "../hooks/useSubscribeBroadcastChan
import { StdEventType } from "../types/chat-io-messages.types"; import { StdEventType } from "../types/chat-io-messages.types";
import { import {
Direction, Direction,
IncomingMessageType,
IPayload, IPayload,
IQuickReply,
ISubscriber, ISubscriber,
ISuggestion, ISuggestion,
QuickReplyType, QuickReplyType,
@ -35,6 +37,16 @@ import { useSettings } from "./SettingsProvider";
import { useSocket, useSubscribe } from "./SocketProvider"; import { useSocket, useSubscribe } from "./SocketProvider";
import { useWidget } from "./WidgetProvider"; import { useWidget } from "./WidgetProvider";
const formatQuickReplies = (quick_replies?: IQuickReply[]): ISuggestion[] =>
(quick_replies || []).map(
(qr) =>
({
content_type: QuickReplyType.text,
text: qr.title,
payload: qr.payload,
} as ISuggestion),
);
interface Participant { interface Participant {
id: string; id: string;
name: string; name: string;
@ -262,16 +274,11 @@ const ChatProvider: React.FC<{
"data" in newIOMessage && "data" in newIOMessage &&
"quick_replies" in newIOMessage.data "quick_replies" in newIOMessage.data
) { ) {
setSuggestions( const formattedSuggestions = formatQuickReplies(
(newIOMessage.data.quick_replies || []).map( newIOMessage.data.quick_replies,
(qr) =>
({
content_type: QuickReplyType.text,
text: qr.title,
payload: qr.payload,
} as ISuggestion),
),
); );
setSuggestions(formattedSuggestions);
} else { } else {
setSuggestions([]); setSuggestions([]);
} }
@ -327,6 +334,18 @@ const ChatProvider: React.FC<{
queryParams, queryParams,
).toString()}`, ).toString()}`,
); );
const mostRecentMessage = body.messages.at(-1);
if (
mostRecentMessage &&
mostRecentMessage.type === IncomingMessageType.quick_replies
) {
const formattedSuggestions = formatQuickReplies(
mostRecentMessage.data["quick_replies"],
);
setSuggestions(formattedSuggestions);
}
localStorage.setItem("profile", JSON.stringify(body.profile)); localStorage.setItem("profile", JSON.stringify(body.profile));
setMessages( setMessages(