UNPKG

@yext/chat-headless

Version:

A state manager library powered by Redux for Yext Chat integrations

93 lines (90 loc) 3.76 kB
import { createSlice } from '@reduxjs/toolkit'; const BASE_STATE_LOCAL_STORAGE_KEY = "yext_chat_state"; const initialState = { messages: [], isLoading: false, canSendMessage: true, }; function getStateLocalStorageKey(hostname, botId) { return `${BASE_STATE_LOCAL_STORAGE_KEY}__${hostname}__${botId}`; } /** * Loads the {@link ConversationState} from local storage. */ const loadSessionState = (botId) => { if (!localStorage) { console.warn("Local storage is not available. State will not be persisted while navigating across pages."); return initialState; } const hostname = window?.location?.hostname; if (!hostname) { console.warn("Unable to get hostname of current page. State will not be persisted while navigating across pages."); return initialState; } const savedState = localStorage.getItem(getStateLocalStorageKey(hostname, botId)); if (savedState) { try { const parsedState = JSON.parse(savedState); if (parsedState.messages.length > 0) { const lastTimestamp = parsedState.messages[parsedState.messages.length - 1].timestamp; const currentDate = new Date(); const lastDate = new Date(lastTimestamp || 0); const diff = currentDate.getTime() - lastDate.getTime(); // If the last message was sent within the last day, we consider the session to be active if (diff < 24 * 60 * 60 * 1000) { return parsedState; } localStorage.removeItem(getStateLocalStorageKey(hostname, botId)); } } catch (e) { console.warn("Unabled to load saved state: error parsing state. Starting with a fresh state."); localStorage.removeItem(getStateLocalStorageKey(hostname, botId)); } } return initialState; }; const saveSessionState = (botId, state) => { if (!localStorage) { console.warn("Local storage is not available. State will not be persisted while navigating across pages."); return initialState; } const hostname = window?.location?.hostname; if (!hostname) { console.warn("Unable to get hostname of current page. State will not be persisted across page refreshes."); return initialState; } localStorage.setItem(getStateLocalStorageKey(hostname, botId), JSON.stringify(state)); }; /** * Registers with Redux the slice of {@link State} pertaining to the loading status * of Chat Headless. */ const conversationSlice = createSlice({ name: "conversation", initialState, reducers: { setConversationId: (state, action) => { state.conversationId = action.payload; }, setMessages: (state, action) => { state.messages = action.payload; }, addMessage: (state, action) => { state.messages.push(action.payload); }, setMessageNotes: (state, action) => { state.notes = action.payload; }, setIsLoading: (state, action) => { state.isLoading = action.payload; }, setCanSendMessage: (state, action) => { state.canSendMessage = action.payload; }, }, }); const { setMessages, addMessage, setMessageNotes, setIsLoading, setConversationId, setCanSendMessage, } = conversationSlice.actions; var conversationReducer = conversationSlice.reducer; export { addMessage, conversationSlice, conversationReducer as default, getStateLocalStorageKey, initialState, loadSessionState, saveSessionState, setCanSendMessage, setConversationId, setIsLoading, setMessageNotes, setMessages }; //# sourceMappingURL=conversation.mjs.map