|
@@ -7,13 +7,13 @@ import { styled } from '@mui/material/styles';
|
|
|
import Badge from '@mui/material/Badge';
|
|
|
import VolumeOffIcon from '@mui/icons-material/VolumeOff';
|
|
|
import { makeStyles, Typography } from '@material-ui/core'
|
|
|
-import { useState,useEffect } from 'react';
|
|
|
+import { useState,useEffect,useRef } from 'react';
|
|
|
import shortid from 'shortid';
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
|
|
|
|
|
import AlertInfo from '../../../reusableComponents/AlertInfo'
|
|
|
import DoneAllIcon from '@mui/icons-material/DoneAll';
|
|
|
-import { firstLetter, slicedWord, timeStamp } from '../../../../helpers'
|
|
|
+import { firstLetter, slicedWord, timeStamp,notification,playNotificationWithoutPermission } from '../../../../helpers'
|
|
|
import { getState } from '../../../../redux/chats/selector'
|
|
|
import { asyncGetChats } from '../../../../redux/chats/operations'
|
|
|
import { asyncStartChatById } from '../../../../redux/chat/operations'
|
|
@@ -160,6 +160,7 @@ const useStyles = makeStyles({
|
|
|
const ChatsList = () => {
|
|
|
const classes = useStyles()
|
|
|
const dispatch = useDispatch()
|
|
|
+ const ref = useRef<any>(null)
|
|
|
const [selectedIndex, setSelectedIndex] = useState<number>(1);
|
|
|
const { total, chats , lastMessages,lastOnline } = useSelector(getState)
|
|
|
|
|
@@ -170,7 +171,7 @@ const ChatsList = () => {
|
|
|
setSelectedIndex(i);
|
|
|
}
|
|
|
|
|
|
- const handleNewMsgS = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>, i: number,companionId: string) => {
|
|
|
+ const handleNewMsgs = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>, i: number,companionId: string) => {
|
|
|
e.stopPropagation()
|
|
|
dispatch(asyncStartChatById(companionId))
|
|
|
dispatch(actionScroll(true))
|
|
@@ -187,11 +188,29 @@ const ChatsList = () => {
|
|
|
dispatch(asyncGetChats())
|
|
|
}, [dispatch])
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
+ const handleNotification= (companionId: string) => {
|
|
|
+ dispatch(asyncStartChatById(companionId))
|
|
|
+ dispatch(actionScroll(true))
|
|
|
+ }
|
|
|
+ if (ref.current) {
|
|
|
+ ref.current.forEach(({total,seen}: any,i:number) => {
|
|
|
+ const oldDifferent = total - seen
|
|
|
+ const chat = chats[i]
|
|
|
+ const newDifferent = chat.total - chat.seen
|
|
|
+ if (newDifferent > oldDifferent && !chat.mute) {
|
|
|
+ playNotificationWithoutPermission('http://localhost:3000/recive.mp3')
|
|
|
+ notification(chat.name,() => handleNotification(chat.companionId))
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ ref.current = chats
|
|
|
+ }, [chats,dispatch])
|
|
|
|
|
|
return total !== '0' ? (
|
|
|
<List className={classes.list} component="nav"
|
|
|
aria-label="main mailbox folders">
|
|
|
- {chats.map(({name, lastName, avatarUrl, updatedAt, color,companionId,mute,seen,total }, i: number) =>
|
|
|
+ {chats.map(({name, lastName, avatarUrl, updatedAt, color,companionId,mute,seen,total,watched,typing }, i: number) =>
|
|
|
<ListItemButton
|
|
|
key={shortid.generate()}
|
|
|
selected={selectedIndex === i}
|
|
@@ -210,16 +229,17 @@ const ChatsList = () => {
|
|
|
<span>{`${firstLetter(name)}${slicedWord(name, 15, 1)}
|
|
|
${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}</span>
|
|
|
{mute&&<VolumeOffIcon className={classes.listItemInnerText__icon} fontSize='small' />}</div>}
|
|
|
- secondary={lastMessages[i] ? slicedWord(lastMessages[i].message, 35) :
|
|
|
- `${firstLetter(name)}${slicedWord(name, 8, 1)} joined Telegram`} />
|
|
|
+ secondary={typing?'typing...':lastMessages[i] ? slicedWord(lastMessages[i].message, 35) :
|
|
|
+ `${firstLetter(name)}${slicedWord(name, 8, 1)} joined Telegram`}
|
|
|
+ secondaryTypographyProps={{color: "#0379af"}}/>
|
|
|
<ListItemIcon className={classes.listItem_iconRight}>
|
|
|
<div className={classes.listItem_iconTimeChecked}>
|
|
|
- {total-seen === 0 && <DoneAllIcon style={{ color: '#18bd03' }} fontSize='small' />}
|
|
|
+ {watched&& <DoneAllIcon style={{ color: '#18bd03' }} fontSize='small' />}
|
|
|
<Typography className={classes.listItem_icon_time} variant="h6" color="initial">
|
|
|
{lastMessages[i] ? timeStamp(lastMessages[i].updatedAt) :
|
|
|
timeStamp(updatedAt)}</Typography>
|
|
|
</div>
|
|
|
- {lastMessages[i] && total > seen ? <button onClick={(e) => handleNewMsgS(e, i,companionId)}
|
|
|
+ {lastMessages[i] && total > seen ? <button onClick={(e) => handleNewMsgs(e, i,companionId)}
|
|
|
className={mute?classes.listItem_iconRightBtnMute:classes.listItem_iconRightBtn}>{total-seen}</button> :
|
|
|
<button className={classes.listItem_iconRightBtnHidden}/>}
|
|
|
</ListItemIcon>
|