UNPKG

@botonic/react

Version:

Build Chatbots using React

36 lines 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useTyping = useTyping; /* eslint-disable no-empty */ const react_1 = require("react"); function useTyping({ webchatState, updateTyping, updateMessage, }) { (0, react_1.useEffect)(() => { let delayTimeout = null; let typingTimeout = null; try { const nextMsg = webchatState.messagesJSON.filter(m => !m.display)[0]; if (nextMsg) { if (nextMsg.delay && nextMsg.typing) { delayTimeout = setTimeout(() => updateTyping(true), nextMsg.delay * 1000); } else if (nextMsg.typing) { updateTyping(true); } const totalDelay = nextMsg.delay + nextMsg.typing; if (totalDelay) { typingTimeout = setTimeout(() => { updateMessage({ ...nextMsg, display: true }); updateTyping(false); }, totalDelay * 1000); } } // biome-ignore lint/suspicious/noEmptyBlockStatements: error is not used } catch (_e) { } return () => { delayTimeout && clearTimeout(delayTimeout); typingTimeout && clearTimeout(typingTimeout); }; }, [webchatState.messagesJSON, webchatState.typing]); } //# sourceMappingURL=use-typing.js.map