UNPKG

@botonic/react

Version:

Build Chatbots using React

32 lines 1.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useTyping = void 0; const react_1 = require("react"); function useTyping({ webchatState, updateTyping, updateMessage, }) { (0, react_1.useEffect)(() => { let delayTimeout, typingTimeout; 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(Object.assign(Object.assign({}, nextMsg), { display: true })); updateTyping(false); }, totalDelay * 1000); } } catch (e) { } return () => { clearTimeout(delayTimeout); clearTimeout(typingTimeout); }; }, [webchatState.messagesJSON, webchatState.typing]); } exports.useTyping = useTyping; //# sourceMappingURL=use-typing.js.map