index.tsx 4.4 KB

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