123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- 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<boolean>(false)
- const [isNew, setIsNew] = useState<{new:number,mute:boolean}>({new:0,mute:false})
- const divRef = useRef<any | null>(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 (
- <div ref={divRef} className={classes.container} onScroll={debouncedHandleScroll}>
- <div className={classes.messagesBody}>
- {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 (<div key={createdAt}>
- {isTime&&<MessageTime message={timeStampFilter(updatedAt)}/>}
- <MessageLeftText
- message={message}
- updatedAt={updatedAt}
- name={name}
- lastName={lastName}
- /></div>)
- if (type === 'image') return (<div key={createdAt}>
- {isTime&&<MessageTime message={timeStampFilter(updatedAt)}/>}
- <MessageLeftImage
- url={url}
- updatedAt={updatedAt}
- color={color}
- message={message}
- messages={messages}
- fullType={fullType}
- /></div>)
- if (type === 'audio') return (<div key={createdAt}>
- {isTime&&<MessageTime message={timeStampFilter(updatedAt)}/>}
- <MessageLeftAudio
- url={url}
- updatedAt={updatedAt}
- fullType={fullType}
- /></div>)
- if (type === 'video') return (<div key={createdAt}>
- {isTime&&<MessageTime message={timeStampFilter(updatedAt)}/>}
- <MessageLeftVideo
- url={url}
- updatedAt={updatedAt}
- fullType={fullType}
- /></div>)
- if (type) return (<div key={createdAt}>
- {isTime&&<MessageTime message={timeStampFilter(updatedAt)}/>}
- <MessageLeftFile
- url={url}
- updatedAt={updatedAt}
- type={type}
- /></div>)
- } else {
- if (type === 'text') return (<div key={createdAt}>
- {isTime&&<MessageTime message={timeStampFilter(updatedAt)}/>}
- <MessageRightText
- message={message}
- updatedAt={updatedAt}
- name={name}
- lastName={lastName}
- /></div>)
- if (type === 'image') return (<div key={createdAt}>
- {isTime&&<MessageTime message={timeStampFilter(updatedAt)}/>}
- <MessageRightImage
- url={url}
- updatedAt={updatedAt}
- color={color}
- message={message}
- messages={messages}
- fullType={fullType}
- /></div>)
- if (type === 'audio') return (<div key={createdAt}>
- {isTime&&<MessageTime message={timeStampFilter(updatedAt)}/>}
- <MessageRightAudio
- url={url}
- updatedAt={updatedAt}
- fullType={fullType}
- /></div>)
- if (type === 'video') return (<div key={createdAt}>
- {isTime&&<MessageTime message={timeStampFilter(updatedAt)}/>}
- <MessageRightVideo
- url={url}
- updatedAt={updatedAt}
- fullType={fullType}
- /></div>)
- if (type) return (<div key={createdAt}>
- {isTime&&<MessageTime message={timeStampFilter(updatedAt)}/>}
- <MessageRightFile
- url={url}
- updatedAt={updatedAt}
- type={type}
- /></div>)
- }
- }) : <AlertInfo name='You do not have messages yet!' />}
- </div>
- <SendMessage isArrow={isArrow} isNew={isNew} handleScrollTo={handleScrollTo}/>
- </div>
- );
- }
- export default ChatBar
|