index.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import List from '@mui/material/List';
  2. import { makeStyles } from '@material-ui/core'
  3. import { 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 { asyncGetChatById } from '../../../../redux/chat/operations'
  11. import { asyncGetChats } from '../../../../redux/chats/operations';
  12. import { actionRightIsOpen,actionScrollChat } from '../../../../redux/control/action'
  13. import { TChats } from '../../../../typescript/redux/chats/types';
  14. import { prodBaseURL,refreshAppTime } from '../../../../helpers';
  15. const useStyles = makeStyles({
  16. list: {
  17. width: '100%',
  18. maxHeight: '93vh',
  19. overflowY: 'scroll',
  20. '&::-webkit-scrollbar': {
  21. width: '0.4em'
  22. },
  23. '&::-webkit-scrollbar-track': {
  24. boxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  25. webkitBoxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  26. backgroundColor: '#eceeec',
  27. },
  28. '&::-webkit-scrollbar-thumb': {
  29. backgroundColor: '#ccc8c8',
  30. },
  31. "&::-webkit-scrollbar-thumb:focus": {
  32. backgroundColor: "#959595",
  33. },
  34. "&::-webkit-scrollbar-thumb:active": {
  35. backgroundColor: "#959595",
  36. },
  37. },
  38. })
  39. interface IChatsList {
  40. sort: boolean,
  41. }
  42. const ChatsList = ({sort}:IChatsList) => {
  43. const classes = useStyles()
  44. const dispatch = useDispatch()
  45. const chatsRef = useRef<any>(null)
  46. const { total, chats } = useSelector(getStateMemo)
  47. const chat = useSelector(getChatMemo)
  48. const handleListItemClick = (companionId: string) => {
  49. dispatch(actionRightIsOpen(''))
  50. dispatch(asyncGetChatById(companionId))
  51. if(chat.companionId !== companionId) dispatch(actionScrollChat(true))
  52. }
  53. const handleNewMsgs = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>, companionId: string) => {
  54. e.stopPropagation()
  55. dispatch(actionScrollChat(true))
  56. dispatch(actionRightIsOpen(''))
  57. dispatch(asyncGetChatById(companionId))
  58. }
  59. const handleNotification = useCallback((companionId: string) => {
  60. dispatch(asyncGetChatById(companionId))
  61. dispatch(actionScrollChat(true))
  62. }, [dispatch])
  63. const sortedChats: TChats = sortByRecent(chats, sort)
  64. useEffect(() => {
  65. const handleReset = () => dispatch(asyncGetChats())
  66. handleReset()
  67. const idInterval = setInterval(handleReset, refreshAppTime);
  68. return () => clearInterval(idInterval);
  69. }, [dispatch]);
  70. useEffect(() => {
  71. if (chatsRef.current) {
  72. chatsRef.current.forEach(({total,seen}: any,i:number) => {
  73. const oldDifferent = total - seen
  74. const chat = sortedChats[i]
  75. if(chat === undefined) return
  76. const newDifferent = chat.total - chat.seen
  77. if (newDifferent > oldDifferent && !chat.mute) {
  78. playNotificationWithoutPermission(`${prodBaseURL}/telegramReceive.mp3`)
  79. notification(chat.name,() => handleNotification(chat.companionId))
  80. }
  81. })
  82. }
  83. chatsRef.current = sortedChats
  84. }, [chat,sortedChats,handleNotification,dispatch])
  85. return total !== '0' ? (
  86. <List className={classes.list} component="nav"
  87. aria-label="main mailbox folders">
  88. {sortedChats.map((el) => <ChatItem key={el.number} chat={el}
  89. handleListItemClick={handleListItemClick} handleNewMsgs={handleNewMsgs} />)}
  90. </List>
  91. ):<AlertInfo name='You do not have Chats yet!' />;
  92. }
  93. export default ChatsList