index.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import List from '@mui/material/List';
  2. import { makeStyles } from '@material-ui/core'
  3. import { useState,useEffect,useRef,useCallback } from 'react';
  4. import { useSelector, useDispatch } from 'react-redux';
  5. import AlertInfo from '../../../reusableComponents/AlertInfo'
  6. import ChatItem from './ChatItem';
  7. import { notification,playNotificationWithoutPermission,sortByRecent } from '../../../../helpers'
  8. import { getStateMemo } from '../../../../redux/chats/selector'
  9. import { getChatMemo } from '../../../../redux/chat/selector'
  10. import { asyncGetChats } from '../../../../redux/chats/operations'
  11. import { asyncStartChatById } from '../../../../redux/chat/operations'
  12. import { actionRemoveChat } from '../../../../redux/chat/action'
  13. import { actionScroll, actionIsOpen } from '../../../../redux/control/action'
  14. import { getIsOpen } from '../../../../redux/control/selector';
  15. import { TChats } from '../../../../typescript/redux/chats/types';
  16. import { timeStampFilter } from '../../../../helpers';
  17. const useStyles = makeStyles({
  18. list: {
  19. width: '100%',
  20. maxHeight: '93vh',
  21. overflowY: 'scroll',
  22. '&::-webkit-scrollbar': {
  23. width: '0.4em'
  24. },
  25. '&::-webkit-scrollbar-track': {
  26. boxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  27. webkitBoxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  28. backgroundColor: '#eceeec',
  29. },
  30. '&::-webkit-scrollbar-thumb': {
  31. backgroundColor: '#ccc8c8',
  32. },
  33. "&::-webkit-scrollbar-thumb:focus": {
  34. backgroundColor: "#959595",
  35. },
  36. "&::-webkit-scrollbar-thumb:active": {
  37. backgroundColor: "#959595",
  38. },
  39. },
  40. })
  41. interface IChatsList {
  42. sort: boolean,
  43. date: any,
  44. value: string
  45. }
  46. const ChatsList = ({sort,date,value}:IChatsList) => {
  47. const classes = useStyles()
  48. const dispatch = useDispatch()
  49. const ref = useRef<any>(null)
  50. const [sortedChats, setSortedChats] = useState<TChats>([]);
  51. const { total, chats } = useSelector(getStateMemo)
  52. const chat = useSelector(getChatMemo)
  53. const isOpen = useSelector(getIsOpen)
  54. const filteredChats = useCallback((arr: TChats) => arr.filter((el) => {
  55. if (!date) {
  56. return el
  57. } else if (timeStampFilter(date) === timeStampFilter(el.lastMessageCreatedAt ?
  58. el.lastMessageCreatedAt : el.createdAt)) {
  59. return el
  60. }
  61. }),[date])
  62. const handleListItemClick = (companionId: string) => {
  63. isOpen&&dispatch(actionIsOpen(''))
  64. dispatch(asyncStartChatById(companionId))
  65. }
  66. const handleNewMsgs = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>, companionId: string) => {
  67. e.stopPropagation()
  68. dispatch(actionScroll(true))
  69. isOpen&&dispatch(actionIsOpen(''))
  70. dispatch(asyncStartChatById(companionId))
  71. }
  72. useEffect(() => {
  73. dispatch(asyncGetChats())
  74. const handleReset = () => dispatch(asyncGetChats())
  75. const idInterval = setInterval(handleReset, 3000);
  76. return () => clearInterval(idInterval);
  77. }, [dispatch]);
  78. useEffect(() => {
  79. const handleNotification= (companionId: string) => {
  80. dispatch(asyncStartChatById(companionId))
  81. dispatch(actionScroll(true))
  82. }
  83. const sorted = filteredChats(sortByRecent(chats,sort))
  84. setSortedChats(sorted)
  85. if (chat.companionId&&!sorted.find((el) => el.companionId === chat.companionId))dispatch(actionRemoveChat())
  86. if (ref.current) {
  87. ref.current.forEach(({total,seen}: any,i:number) => {
  88. const oldDifferent = total - seen
  89. const chat = sorted[i]
  90. if(chat === undefined) return
  91. const newDifferent = chat.total - chat.seen
  92. if (newDifferent > oldDifferent && !chat.mute) {
  93. playNotificationWithoutPermission('http://localhost:3000/recive.mp3')
  94. notification(chat.name,() => handleNotification(chat.companionId))
  95. }
  96. })
  97. }
  98. ref.current = sorted
  99. }, [chats, chat,sort,filteredChats,dispatch])
  100. return total !== '0' ? (
  101. <List className={classes.list} component="nav"
  102. aria-label="main mailbox folders">
  103. {sortedChats.length > 0 ? sortedChats.map((el) => <ChatItem key={el.number} chat={el}
  104. handleListItemClick={handleListItemClick} handleNewMsgs={handleNewMsgs} />):
  105. <AlertInfo name={`Can not find Chats by request : ${value}`} />}
  106. </List>
  107. ):<AlertInfo name='You do not have any Chats yet!' />;
  108. }
  109. export default ChatsList