import { makeStyles } from "@material-ui/core/styles"; import { useState, useEffect, useRef, useCallback } from "react"; import { useSelector,useDispatch } from "react-redux"; import SendMessage from "./SendMessage"; import MessageLeftText from './Messages/MessageLeftText' import MessageLeftImage from './Messages/MessageLeftImage' import MessageLeftAudio from './Messages/MessageLeftAudio' import MessageLeftVideo from './Messages/MessageLeftVideo' import MessageLeftFile from "./Messages/MessageLeftFile"; import MessageRightText from './Messages/MessageRightText' import MessageRightImage from './Messages/MessageRightImage' import MessageRightAudio from './Messages/MessageRightAudio' import MessageRightVideo from './Messages/MessageRightVideo' import MessageRightFile from "./Messages/MessageRightFile"; import MessageTime from "./Messages/MessageTime"; import AlertInfo from "../../../reusableComponents/AlertInfo"; import { getMessagesMemo } from '../../../../redux/messages/selector' import { getNumber } from '../../../../redux/authorization/selector' import { getChat } from '../../../../redux/chat/selector' import { getScroll } from '../../../../redux/control/selector' import { actionScroll } from '../../../../redux/control/action' import { asyncGetMessagesById } from '../../../../redux/messages/operations' import { seenChat } from "../../../../api-data"; import { timeStampFilter } from "../../../../helpers"; const debounce = require('lodash.debounce'); const useStyles = makeStyles({ container: { width: "100%", height: "100%", maxWidth: "100%", maxHeight: "100%", display: "flex", alignItems: "center", alignContent:"center", flexDirection: "column", position: "relative", overflowY: "scroll", paddingTop: 60, }, messagesBody: { width: "60%", height: "80%", }, }); const ChatBar = () => { const classes = useStyles(); const dispatch = useDispatch() const messages = useSelector(getMessagesMemo) const userNumber = useSelector(getNumber) const { companionId } = useSelector(getChat) const scroll = useSelector(getScroll) const [isArrow, setIsArrow] = useState(false) const [isNew, setIsNew] = useState<{new:number,mute:boolean}>({new:0,mute:false}) const divRef = useRef(null) let time:any const handleScrollTo = () => { divRef.current&&divRef.current.scrollTo({ top: divRef.current.scrollHeight, behavior: 'smooth' }) } const handleScroll = ({ target }: any) => { const different = target.scrollHeight - target.scrollTop if (different < 900) seenChat(companionId) setIsArrow(different < 1500 ? false : true) } const debouncedHandleScroll = useCallback(debounce(handleScroll, 300), []); useEffect(() => { if (scroll) { dispatch(asyncGetMessagesById(companionId, handleScrollTo)) dispatch(actionScroll(false)) } }, [dispatch,scroll, companionId]) useEffect(() => { dispatch(asyncGetMessagesById(companionId, handleScrollTo)) const handleReset = () => { dispatch(asyncGetMessagesById(companionId, null)) const arr: any = localStorage.getItem('isNew') if(arr) setIsNew(JSON.parse(arr)) } const idInterval = setInterval(handleReset, 3000); return () => clearInterval(idInterval); }, [dispatch, companionId]); return (
{messages.length > 0 ? messages.map(({ message, name, lastName, color, updatedAt, createdAt,number, type,fullType }) => { let isTime if (!time) { isTime = true time = updatedAt } else if (timeStampFilter(time) !== timeStampFilter(updatedAt)) { time = updatedAt isTime = true } const url = `http://localhost:3000/${message}` if (number !== userNumber) { if (type === 'text') return (
{isTime&&}
) if (type === 'image') return (
{isTime&&}
) if (type === 'audio') return (
{isTime&&}
) if (type === 'video') return (
{isTime&&}
) if (type) return (
{isTime&&}
) } else { if (type === 'text') return (
{isTime&&}
) if (type === 'image') return (
{isTime&&}
) if (type === 'audio') return (
{isTime&&}
) if (type === 'video') return (
{isTime&&}
) if (type) return (
{isTime&&}
) } }) : }
); } export default ChatBar