UNPKG

@botonic/react

Version:

Build Chatbots using React

187 lines 6.41 kB
import { useReducer, useRef } from 'react'; import { defaultTheme } from '../theme/default-theme'; import { WebchatAction } from './actions'; import { webchatReducer } from './webchat-reducer'; function getWebchatInitialState(initialTheme) { return { replies: [], messagesJSON: [], messagesComponents: [], latestInput: {}, typing: false, webview: null, webviewParams: null, session: { user: undefined }, lastRoutePath: undefined, handoff: false, theme: initialTheme, themeUpdates: {}, error: {}, online: true, devSettings: { keepSessionOnReload: false }, isWebchatOpen: false, isEmojiPickerOpen: false, isPersistentMenuOpen: false, isCoverComponentOpen: false, isCustomComponentRendered: false, lastMessageUpdate: undefined, currentAttachment: undefined, numUnreadMessages: 0, isLastMessageVisible: true, isInputFocused: false, }; } export function useWebchat(theme) { const initialTheme = theme || defaultTheme; const webchatInitialState = getWebchatInitialState(initialTheme); const [webchatState, webchatDispatch] = useReducer(webchatReducer, webchatInitialState); const webchatContainerRef = useRef(null); const chatAreaRef = useRef(null); const inputPanelRef = useRef(null); const headerRef = useRef(null); const scrollableMessagesListRef = useRef(null); const repliesRef = useRef(null); const addMessage = (message) => webchatDispatch({ type: WebchatAction.ADD_MESSAGE, payload: message }); const addMessageComponent = (message) => webchatDispatch({ type: WebchatAction.ADD_MESSAGE_COMPONENT, payload: message, }); const updateMessage = (message) => webchatDispatch({ type: WebchatAction.UPDATE_MESSAGE, payload: message }); const updateReplies = (replies) => webchatDispatch({ type: WebchatAction.UPDATE_REPLIES, payload: replies }); const removeReplies = () => webchatDispatch({ type: WebchatAction.REMOVE_REPLIES, payload: [] }); const updateLatestInput = (input) => webchatDispatch({ type: WebchatAction.UPDATE_LATEST_INPUT, payload: input }); const updateTyping = (typing) => webchatDispatch({ type: WebchatAction.UPDATE_TYPING, payload: typing }); const updateWebview = (webview, params) => webchatDispatch({ type: WebchatAction.UPDATE_WEBVIEW, payload: { webview, webviewParams: params }, }); const removeWebview = () => webchatDispatch({ type: WebchatAction.REMOVE_WEBVIEW, }); const updateSession = (session) => { webchatDispatch({ type: WebchatAction.UPDATE_SESSION, payload: session, }); }; const updateLastRoutePath = (path) => webchatDispatch({ type: WebchatAction.UPDATE_LAST_ROUTE_PATH, payload: path, }); const updateHandoff = (handoff) => webchatDispatch({ type: WebchatAction.UPDATE_HANDOFF, payload: handoff, }); const updateTheme = (theme, themeUpdates) => { const payload = themeUpdates !== undefined ? { theme, themeUpdates } : { theme }; webchatDispatch({ type: WebchatAction.UPDATE_THEME, payload, }); }; const updateDevSettings = (settings) => webchatDispatch({ type: WebchatAction.UPDATE_DEV_SETTINGS, payload: settings, }); const toggleWebchat = (toggle) => { webchatDispatch({ type: WebchatAction.TOGGLE_WEBCHAT, payload: toggle, }); }; const toggleEmojiPicker = (toggle) => webchatDispatch({ type: WebchatAction.TOGGLE_EMOJI_PICKER, payload: toggle, }); const togglePersistentMenu = (toggle) => webchatDispatch({ type: WebchatAction.TOGGLE_PERSISTENT_MENU, payload: toggle, }); const toggleCoverComponent = (toggle) => webchatDispatch({ type: WebchatAction.TOGGLE_COVER_COMPONENT, payload: toggle, }); const doRenderCustomComponent = (toggle) => webchatDispatch({ type: WebchatAction.DO_RENDER_CUSTOM_COMPONENT, payload: toggle, }); const setError = (error) => webchatDispatch({ type: WebchatAction.SET_ERROR, payload: error, }); const setOnline = (online) => webchatDispatch({ type: WebchatAction.SET_ONLINE, payload: online, }); const clearMessages = () => { webchatDispatch({ type: WebchatAction.CLEAR_MESSAGES, }); }; const updateLastMessageDate = (date) => { webchatDispatch({ type: WebchatAction.UPDATE_LAST_MESSAGE_DATE, payload: date, }); }; const setCurrentAttachment = (attachment) => { webchatDispatch({ type: WebchatAction.SET_CURRENT_ATTACHMENT, payload: attachment, }); }; const resetUnreadMessages = () => { webchatDispatch({ type: WebchatAction.RESET_UNREAD_MESSAGES, }); }; const setLastMessageVisible = (isLastMessageVisible) => { webchatDispatch({ type: WebchatAction.SET_LAST_MESSAGE_VISIBLE, payload: isLastMessageVisible, }); }; const setIsInputFocused = (isInputFocused) => { webchatDispatch({ type: WebchatAction.SET_IS_INPUT_FOCUSED, payload: isInputFocused, }); }; return { addMessage, addMessageComponent, clearMessages, doRenderCustomComponent, resetUnreadMessages, setCurrentAttachment, setError, setIsInputFocused, setLastMessageVisible, setOnline, toggleCoverComponent, toggleEmojiPicker, togglePersistentMenu, toggleWebchat, updateDevSettings, updateHandoff, updateLastMessageDate, updateLastRoutePath, updateLatestInput, updateMessage, updateReplies, updateSession, updateTheme, updateTyping, updateWebview, removeReplies, removeWebview, webchatState, webchatContainerRef, headerRef, chatAreaRef, scrollableMessagesListRef, repliesRef, inputPanelRef, }; } //# sourceMappingURL=use-webchat.js.map