Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions src/components/Bot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ import { CircleDotIcon, SparklesIcon, TrashIcon } from './icons';
import { CancelButton } from './buttons/CancelButton';
import { cancelAudioRecording, startAudioRecording, stopAudioRecording } from '@/utils/audioRecording';
import { LeadCaptureBubble } from '@/components/bubbles/LeadCaptureBubble';
import { removeLocalStorageChatHistory, getLocalStorageChatflow, setLocalStorageChatflow, setCookie, getCookie } from '@/utils';
import {
removeLocalStorageChatHistory,
removeSessionStorageChatHistory,
getLocalStorageChatflow,
getSessionStorageChatflow,
setLocalStorageChatflow,
setSessionStorageChatflow,
setCookie,
getCookie,
} from '@/utils';
import { cloneDeep, merge } from 'lodash';
import { FollowUpPromptBubble } from '@/components/bubbles/FollowUpPromptBubble';
import { fetchEventSource, EventStreamContentType } from '@microsoft/fetch-event-source';
Expand Down Expand Up @@ -572,6 +581,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
}
return item;
});
setSessionStorageChatflow(props.chatflowid, chatId(), { chatHistory: messages });
setLocalStorageChatflow(props.chatflowid, chatId(), { chatHistory: messages });
};

Expand Down Expand Up @@ -789,7 +799,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
};

const fetchResponseFromEventStream = async (chatflowid: string, params: any) => {
const chatId = params.chatId;
const requestChatId = params.chatId;
const input = params.question;
params.streaming = true;
console.log('[fetchResponseFromEventStream]', { botProps, params, chatflowid, props });
Expand Down Expand Up @@ -869,7 +879,8 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
closeResponse();
break;
case 'end':
setLocalStorageChatflow(chatflowid, chatId);
setSessionStorageChatflow(chatflowid, chatId() || requestChatId);
setLocalStorageChatflow(chatflowid, chatId() || requestChatId);
closeResponse();
break;
}
Expand Down Expand Up @@ -1210,6 +1221,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {

const clearChat = () => {
try {
removeSessionStorageChatHistory(props.chatflowid);
removeLocalStorageChatHistory(props.chatflowid);
setChatId(
(props.chatflowConfig?.vars as any)?.customerId ? `${(props.chatflowConfig?.vars as any).customerId.toString()}+${uuidv4()}` : uuidv4(),
Expand Down Expand Up @@ -1285,18 +1297,17 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
setDisclaimerPopupOpen(false);
}

const chatMessage = getLocalStorageChatflow(props.chatflowid);
const sessionMessage = getSessionStorageChatflow(props.chatflowid);
const localStorageMessage = getLocalStorageChatflow(props.chatflowid);
const chatMessage = Object.keys(sessionMessage).length ? sessionMessage : localStorageMessage;
const customerId = (props.chatflowConfig?.vars as any)?.customerId;

// Initialize chatId from localStorage or generate new one
if (chatMessage?.chatId) {
setChatId(chatMessage.chatId);
} else {
setChatId(customerId ? `${customerId.toString()}+${uuidv4()}` : uuidv4());
}
const initialChatId = chatMessage?.chatId || (customerId ? `${customerId.toString()}+${uuidv4()}` : uuidv4());
setChatId(initialChatId);

if (chatMessage && Object.keys(chatMessage).length) {
const savedLead = chatMessage.lead;
const savedLead = chatMessage.lead || localStorageMessage?.lead;
if (savedLead) {
setIsLeadSaved(!!savedLead);
setLeadEmail(savedLead.email);
Expand Down Expand Up @@ -1330,6 +1341,14 @@ export const Bot = (botProps: BotProps & { class?: string }) => {

const filteredMessages = loadedMessages.filter((message) => message.type !== 'leadCaptureMessage');
setMessages([...filteredMessages]);

// Seed sessionStorage if we loaded from localStorage (returning user in new tab)
if (!Object.keys(sessionMessage).length && (chatMessage?.chatHistory?.length || savedLead)) {
setSessionStorageChatflow(props.chatflowid, initialChatId, {
...(chatMessage?.chatHistory?.length ? { chatHistory: chatMessage.chatHistory } : {}),
...(savedLead ? { lead: savedLead } : {}),
});
}
}

// Determine if particular chatflow is available for streaming
Expand Down
6 changes: 4 additions & 2 deletions src/components/bubbles/BotBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,17 @@ export const BotBubble = (props: Props) => {
};

const saveToLocalStorage = (rating: FeedbackRatingType) => {
const chatDetails = localStorage.getItem(`${props.chatflowid}_EXTERNAL`);
const chatDetails = sessionStorage.getItem(`${props.chatflowid}_EXTERNAL`) || localStorage.getItem(`${props.chatflowid}_EXTERNAL`);
if (!chatDetails) return;
try {
const parsedDetails = JSON.parse(chatDetails);
const messages: MessageType[] = parsedDetails.chatHistory || [];
const message = messages.find((msg) => msg.messageId === props.message.messageId);
if (!message) return;
message.rating = rating;
localStorage.setItem(`${props.chatflowid}_EXTERNAL`, JSON.stringify({ ...parsedDetails, chatHistory: messages }));
const updated = JSON.stringify({ ...parsedDetails, chatHistory: messages });
sessionStorage.setItem(`${props.chatflowid}_EXTERNAL`, updated);
localStorage.setItem(`${props.chatflowid}_EXTERNAL`, updated);
} catch (e) {
return;
}
Expand Down
8 changes: 5 additions & 3 deletions src/components/bubbles/LeadCaptureBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FormEvent, LeadsConfig, MessageType } from '@/components/Bot';
import { addLeadQuery, LeadCaptureInput } from '@/queries/sendMessageQuery';
import { SaveLeadButton } from '@/components/buttons/LeadCaptureButtons';
import { Avatar } from '@/components/avatars/Avatar';
import { getLocalStorageChatflow, setLocalStorageChatflow } from '@/utils';
import { getLocalStorageChatflow, setLocalStorageChatflow, setSessionStorageChatflow } from '@/utils';

type Props = {
message: MessageType;
Expand Down Expand Up @@ -65,13 +65,15 @@ export const LeadCaptureBubble = (props: Props) => {
});

if (result.data) {
setLocalStorageChatflow(props.chatflowid, props.chatId, {
const leadData = {
lead: {
name: leadName(),
email: leadEmail(),
phone: leadPhone(),
},
});
};
setSessionStorageChatflow(props.chatflowid, props.chatId, leadData);
setLocalStorageChatflow(props.chatflowid, props.chatId, leadData);
props.setIsLeadSaved(true);
props.setLeadEmail(leadEmail());
}
Expand Down
45 changes: 45 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,51 @@ export const removeLocalStorageChatHistory = (chatflowid: string) => {
}
};

export const setSessionStorageChatflow = (chatflowid: string, chatId: string, saveObj: Record<string, any> = {}) => {
const chatDetails = sessionStorage.getItem(`${chatflowid}_EXTERNAL`);
const obj = { ...saveObj };
if (chatId) obj.chatId = chatId;

if (!chatDetails) {
sessionStorage.setItem(`${chatflowid}_EXTERNAL`, JSON.stringify(obj));
} else {
try {
const parsedChatDetails = JSON.parse(chatDetails);
sessionStorage.setItem(`${chatflowid}_EXTERNAL`, JSON.stringify({ ...parsedChatDetails, ...obj }));
} catch (e) {
sessionStorage.setItem(`${chatflowid}_EXTERNAL`, JSON.stringify(obj));
}
}
};

export const getSessionStorageChatflow = (chatflowid: string) => {
const chatDetails = sessionStorage.getItem(`${chatflowid}_EXTERNAL`);
if (!chatDetails) return {};
try {
return JSON.parse(chatDetails);
} catch (e) {
return {};
}
};

export const removeSessionStorageChatHistory = (chatflowid: string) => {
const chatDetails = sessionStorage.getItem(`${chatflowid}_EXTERNAL`);
if (!chatDetails) return;
try {
const parsedChatDetails = JSON.parse(chatDetails);
if (parsedChatDetails.lead) {
// Dont remove lead when chat is cleared
const obj = { lead: parsedChatDetails.lead };
sessionStorage.removeItem(`${chatflowid}_EXTERNAL`);
sessionStorage.setItem(`${chatflowid}_EXTERNAL`, JSON.stringify(obj));
} else {
sessionStorage.removeItem(`${chatflowid}_EXTERNAL`);
}
} catch (e) {
return;
}
};

export const getBubbleButtonSize = (size: 'small' | 'medium' | 'large' | number | undefined) => {
if (!size) return 48;
if (typeof size === 'number') return size;
Expand Down
Loading