UNPKG

@botonic/react

Version:

Build Chatbots using React

167 lines 5.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useWebchat = exports.webchatInitialState = void 0; const react_1 = require("react"); const constants_1 = require("../../constants"); const actions_1 = require("../actions"); const webchat_reducer_1 = require("../webchat-reducer"); exports.webchatInitialState = { width: constants_1.WEBCHAT.DEFAULTS.WIDTH, height: constants_1.WEBCHAT.DEFAULTS.HEIGHT, messagesJSON: [], messagesComponents: [], replies: [], latestInput: {}, typing: false, webview: null, webviewParams: null, session: { user: undefined }, lastRoutePath: null, handoff: false, theme: { headerTitle: constants_1.WEBCHAT.DEFAULTS.TITLE, brandColor: constants_1.COLORS.BOTONIC_BLUE, brandImage: constants_1.WEBCHAT.DEFAULTS.LOGO, triggerButtonImage: undefined, textPlaceholder: constants_1.WEBCHAT.DEFAULTS.PLACEHOLDER, style: { fontFamily: constants_1.WEBCHAT.DEFAULTS.FONT_FAMILY, }, }, themeUpdates: {}, error: {}, online: true, devSettings: { keepSessionOnReload: false }, isWebchatOpen: false, isEmojiPickerOpen: false, isPersistentMenuOpen: false, isCoverComponentOpen: false, isCustomComponentRendered: false, lastMessageUpdate: undefined, currentAttachment: undefined, jwt: undefined, unreadMessages: 0, }; function useWebchat() { const [webchatState, webchatDispatch] = (0, react_1.useReducer)(webchat_reducer_1.webchatReducer, exports.webchatInitialState); const addMessage = (message) => webchatDispatch({ type: actions_1.WebchatAction.ADD_MESSAGE, payload: message }); const addMessageComponent = (message) => webchatDispatch({ type: actions_1.WebchatAction.ADD_MESSAGE_COMPONENT, payload: message, }); const updateMessage = (message) => webchatDispatch({ type: actions_1.WebchatAction.UPDATE_MESSAGE, payload: message }); const updateReplies = replies => webchatDispatch({ type: actions_1.WebchatAction.UPDATE_REPLIES, payload: replies }); const updateLatestInput = (input) => webchatDispatch({ type: actions_1.WebchatAction.UPDATE_LATEST_INPUT, payload: input }); const updateTyping = (typing) => webchatDispatch({ type: actions_1.WebchatAction.UPDATE_TYPING, payload: typing }); const updateWebview = (webview, params) => webchatDispatch({ type: actions_1.WebchatAction.UPDATE_WEBVIEW, payload: { webview, webviewParams: params }, }); const updateSession = (session) => { webchatDispatch({ type: actions_1.WebchatAction.UPDATE_SESSION, payload: session, }); }; const updateLastRoutePath = (path) => webchatDispatch({ type: actions_1.WebchatAction.UPDATE_LAST_ROUTE_PATH, payload: path, }); const updateHandoff = (handoff) => webchatDispatch({ type: actions_1.WebchatAction.UPDATE_HANDOFF, payload: handoff, }); const updateTheme = (theme, themeUpdates) => { const payload = themeUpdates !== undefined ? { theme, themeUpdates } : { theme }; webchatDispatch({ type: actions_1.WebchatAction.UPDATE_THEME, payload, }); }; const updateDevSettings = (settings) => webchatDispatch({ type: actions_1.WebchatAction.UPDATE_DEV_SETTINGS, payload: settings, }); const toggleWebchat = (toggle) => { webchatDispatch({ type: actions_1.WebchatAction.TOGGLE_WEBCHAT, payload: toggle, }); }; const toggleEmojiPicker = (toggle) => webchatDispatch({ type: actions_1.WebchatAction.TOGGLE_EMOJI_PICKER, payload: toggle, }); const togglePersistentMenu = (toggle) => webchatDispatch({ type: actions_1.WebchatAction.TOGGLE_PERSISTENT_MENU, payload: toggle, }); const toggleCoverComponent = (toggle) => webchatDispatch({ type: actions_1.WebchatAction.TOGGLE_COVER_COMPONENT, payload: toggle, }); const doRenderCustomComponent = (toggle) => webchatDispatch({ type: actions_1.WebchatAction.DO_RENDER_CUSTOM_COMPONENT, payload: toggle, }); const setError = (error) => webchatDispatch({ type: actions_1.WebchatAction.SET_ERROR, payload: error, }); const setOnline = (online) => webchatDispatch({ type: actions_1.WebchatAction.SET_ONLINE, payload: online, }); const clearMessages = () => { webchatDispatch({ type: actions_1.WebchatAction.CLEAR_MESSAGES, }); }; const updateLastMessageDate = (date) => { webchatDispatch({ type: actions_1.WebchatAction.UPDATE_LAST_MESSAGE_DATE, payload: date, }); }; const setCurrentAttachment = (attachment) => { webchatDispatch({ type: actions_1.WebchatAction.SET_CURRENT_ATTACHMENT, payload: attachment, }); }; const updateJwt = (jwt) => { webchatDispatch({ type: actions_1.WebchatAction.UPDATE_JWT, payload: jwt, }); }; return { addMessage, addMessageComponent, clearMessages, doRenderCustomComponent, setCurrentAttachment, setError, setOnline, toggleCoverComponent, toggleEmojiPicker, togglePersistentMenu, toggleWebchat, updateDevSettings, updateHandoff, updateJwt, updateLastMessageDate, updateLastRoutePath, updateLatestInput, updateMessage, updateReplies, updateSession, updateTheme, updateTyping, updateWebview, webchatDispatch, webchatState, }; } exports.useWebchat = useWebchat; //# sourceMappingURL=use-webchat.js.map