import React, { useEffect, useState } from 'react'; import ConditionallyRender from 'react-conditionally-render'; import ChatbotMessageAvatar from './ChatBotMessageAvatar/ChatbotMessageAvatar'; import Loader from '../Loader/Loader'; import './ChatbotMessage.css'; import { callIfExists } from '../Chat/chatUtils'; import { ICustomComponents, ICustomStyles } from '../../interfaces/IConfig'; interface IChatbotMessageProps { message: string; withAvatar?: boolean; loading?: boolean; messages: any[]; delay?: number; id: number; setState?: React.Dispatch>; customComponents?: ICustomComponents; customStyles: { backgroundColor: string }; } const ChatbotMessage = ({ message, withAvatar = true, loading, messages, customComponents, setState, customStyles, delay, id, }: IChatbotMessageProps) => { const [show, toggleShow] = useState(false); useEffect(() => { let timeoutId: any; const disableLoading = ( messages: any[], setState: React.Dispatch> ) => { let defaultDisableTime = 750; if (delay) defaultDisableTime += delay; timeoutId = setTimeout(() => { const newMessages = [...messages]; const message = newMessages.find((message) => message.id === id); if (!message) return; message.loading = false; message.delay = undefined; setState((state: any) => { const freshMessages = state.messages; const messageIdx = freshMessages.findIndex( (message: any) => message.id === id ); freshMessages[messageIdx] = message; return { ...state, messages: freshMessages }; }); }, defaultDisableTime); }; disableLoading(messages, setState); return () => { clearTimeout(timeoutId); }; }, [delay, id]); useEffect(() => { if (delay) { setTimeout(() => toggleShow(true), delay); } else { toggleShow(true); } }, [delay]); const chatBoxCustomStyles = { backgroundColor: '' }; const arrowCustomStyles = { borderRightColor: '' }; if (customStyles) { chatBoxCustomStyles.backgroundColor = customStyles.backgroundColor; arrowCustomStyles.borderRightColor = customStyles.backgroundColor; } return ( } /> } /> , })} elseShow={
} elseShow={{message}} />
} /> } /> } /> ); }; export default ChatbotMessage;