// ** React Imports import { useRef, useEffect, Ref, ReactNode } from 'react' // ** MUI Imports import Box from '@mui/material/Box' import { styled } from '@mui/material/styles' import Typography from '@mui/material/Typography' // ** Icon Imports import Icon from 'src/@core/components/icon' // ** Third Party Components import PerfectScrollbarComponent, { ScrollBarProps } from 'react-perfect-scrollbar' // ** Custom Components Imports import CustomAvatar from 'src/@core/components/mui/avatar' // ** Utils Imports import { getInitials } from 'src/@core/utils/get-initials' // ** Types Imports import { ChatLogType, MessageType, MsgFeedbackType, ChatLogChatType, MessageGroupType, FormattedChatsType } from 'src/types/apps/chatTypes' const PerfectScrollbar = styled(PerfectScrollbarComponent) }>(({ theme }) => ({ padding: theme.spacing(5) })) const ChatLog = (props: ChatLogType) => { // ** Props const { data, hidden } = props // ** Ref const chatArea = useRef(null) // ** Scroll to chat bottom const scrollToBottom = () => { if (chatArea.current) { if (hidden) { // @ts-ignore chatArea.current.scrollTop = chatArea.current.scrollHeight } else { // @ts-ignore chatArea.current._container.scrollTop = chatArea.current._container.scrollHeight } } } // ** Formats chat data based on sender const formattedChatData = () => { let chatLog: MessageType[] | [] = [] if (data.chat) { chatLog = data.chat.chat } const formattedChatLog: FormattedChatsType[] = [] let chatMessageSenderId = chatLog[0] ? chatLog[0].senderId : 11 let msgGroup: MessageGroupType = { senderId: chatMessageSenderId, messages: [] } chatLog.forEach((msg: MessageType, index: number) => { if (chatMessageSenderId === msg.senderId) { msgGroup.messages.push({ time: msg.time, msg: msg.message, feedback: msg.feedback }) } else { chatMessageSenderId = msg.senderId formattedChatLog.push(msgGroup) msgGroup = { senderId: msg.senderId, messages: [ { time: msg.time, msg: msg.message, feedback: msg.feedback } ] } } if (index === chatLog.length - 1) formattedChatLog.push(msgGroup) }) return formattedChatLog } const renderMsgFeedback = (isSender: boolean, feedback: MsgFeedbackType) => { if (isSender) { if (feedback.isSent && !feedback.isDelivered) { return ( ) } else if (feedback.isSent && feedback.isDelivered) { return ( ) } else { return null } } } useEffect(() => { if (data && data.chat && data.chat.chat.length) { scrollToBottom() } // eslint-disable-next-line react-hooks/exhaustive-deps }, [data]) // ** Renders user chat const renderChats = () => { return formattedChatData().map((item: FormattedChatsType, index: number) => { const isSender = item.senderId === data.userContact.id return (
{data.contact.avatarColor ? getInitials(data.contact.fullName) : null}
{item.messages.map((chat: ChatLogChatType, index: number, { length }: { length: number }) => { const time = new Date(chat.time) return (
theme.spacing(3, 4), ml: isSender ? 'auto' : undefined, borderTopLeftRadius: !isSender ? 0 : undefined, borderTopRightRadius: isSender ? 0 : undefined, color: isSender ? 'common.white' : 'text.primary', backgroundColor: isSender ? 'primary.main' : 'background.paper' }} > {chat.msg}
{index + 1 === length ? ( {renderMsgFeedback(isSender, chat.feedback)} {time ? new Date(time).toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }) : null} ) : null}
) })}
) }) } const ScrollWrapper = ({ children }: { children: ReactNode }) => { if (hidden) { return ( {children} ) } else { return ( {children} ) } } return ( {renderChats()} ) } export default ChatLog