@botonic/react
Version:
Build Chatbots using React
28 lines • 1.15 kB
JavaScript
import { useEffect } from 'react';
export function useTyping({ webchatState, updateTyping, updateMessage, }) {
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]);
}
//# sourceMappingURL=use-typing.js.map