@yext/chat-headless
Version:
A state manager library powered by Redux for Yext Chat integrations
108 lines (103 loc) • 4.12 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var toolkit = require('@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 = toolkit.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;
exports.addMessage = addMessage;
exports.conversationSlice = conversationSlice;
exports.default = conversationReducer;
exports.getStateLocalStorageKey = getStateLocalStorageKey;
exports.initialState = initialState;
exports.loadSessionState = loadSessionState;
exports.saveSessionState = saveSessionState;
exports.setCanSendMessage = setCanSendMessage;
exports.setConversationId = setConversationId;
exports.setIsLoading = setIsLoading;
exports.setMessageNotes = setMessageNotes;
exports.setMessages = setMessages;
//# sourceMappingURL=conversation.js.map