index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { useState,useEffect } from 'react';
  2. import { useSelector } from 'react-redux';
  3. import { makeStyles } from '@material-ui/core'
  4. import AudioList from './AudioList';
  5. import MediaList from './MediaList';
  6. import FilesList from './FilesList';
  7. import TextList from './TextList';
  8. import VideoList from './VideoList'
  9. import { getMessagesMemo } from '../../../../../redux/messages/selector'
  10. import { handleSort } from '../../../../../helpers';
  11. import { getChat } from '../../../../../redux/chat/selector'
  12. import { TMessages } from '../../../../../typescript/redux/messages/types'
  13. const useStyles = makeStyles({
  14. container: {
  15. borderBottom: 'solid 2px #dddddd',
  16. display: 'flex',
  17. justifyContent: 'space-around',
  18. alignContent: "center",
  19. alignItems:"center",
  20. flexWrap: 'nowrap',
  21. height:'7vh',
  22. color:'rgba(0, 0, 0, 0.6)'
  23. },
  24. item: {
  25. height:'100%',
  26. display: 'flex',
  27. flexDirection: 'column',
  28. justifyContent: 'center',
  29. alignContent: "center",
  30. alignItems: "center",
  31. cursor:'pointer',
  32. },
  33. icon: {
  34. fontSize: '1rem',
  35. lineHeight: 0,
  36. marginBottom: 0,
  37. fontWeight:600
  38. },
  39. underline: {
  40. fontSize: '2.5rem',
  41. lineHeight: 0,
  42. },
  43. })
  44. const ProfileLists = ({setDisabled,chatDivRef}:{setDisabled: React.Dispatch<boolean>,chatDivRef: any | null}) => {
  45. const classes = useStyles()
  46. const { sort } = useSelector(getChat)
  47. const messagesMemo = useSelector(getMessagesMemo)
  48. const [isActive, setIsActive] = useState<number>(0)
  49. const handleIsActive = (newValue: number): void => setIsActive(newValue)
  50. const handleScrollToTheMessage = (_id: string) => {
  51. const childNodes = chatDivRef.current.childNodes[0].childNodes
  52. let toScrollNode = [...childNodes].find((el: any) => el.id === _id)
  53. if (toScrollNode) {
  54. toScrollNode = [...toScrollNode.childNodes].slice(-1)[0]
  55. toScrollNode.style.boxShadow = '0px 0px 6px 0px #555555'
  56. toScrollNode.scrollIntoView({ behavior: 'smooth' })
  57. setTimeout(() => {
  58. toScrollNode.style.boxShadow = 'unset'
  59. }, 2000)
  60. }
  61. }
  62. const filterBy = ['text', 'image', 'text', 'audio', 'video']
  63. const sorted: TMessages = handleSort('createdAt', messagesMemo, sort)
  64. const filteredAndSorted = sorted.filter((el) => {
  65. if (isActive !== 0) {
  66. if(el.type === filterBy[isActive]) return el
  67. } else {
  68. if(el.type !== filterBy[isActive]) return el
  69. }
  70. })
  71. useEffect(() => {
  72. setDisabled(filteredAndSorted.length > 0?false:true)
  73. }, [filteredAndSorted, setDisabled])
  74. return (
  75. <>
  76. <div className={classes.container}>
  77. <div className={classes.item} style={{color:isActive === 0?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  78. onClick={() => handleIsActive(0)}>
  79. <span className={classes.icon}>Files</span>
  80. <span className={classes.underline}>___</span>
  81. </div>
  82. <div className={classes.item} style={{color:isActive === 1?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  83. onClick={() => handleIsActive(1)}>
  84. <span className={classes.icon}>Media</span>
  85. <span className={classes.underline}>___</span>
  86. </div>
  87. <div className={classes.item} style={{color:isActive === 2?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  88. onClick={() => handleIsActive(2)}>
  89. <span className={classes.icon}>Text</span>
  90. <span className={classes.underline}>___</span>
  91. </div>
  92. <div className={classes.item} style={{color:isActive === 3?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  93. onClick={() => handleIsActive(3)}>
  94. <span className={classes.icon}>Audio</span>
  95. <span className={classes.underline}>___</span>
  96. </div>
  97. <div className={classes.item} style={{color:isActive === 4?'#1976d2':"rgba(0, 0, 0, 0.6)"}}
  98. onClick={() => handleIsActive(4)}>
  99. <span className={classes.icon}>Video</span>
  100. <span className={classes.underline}>___</span>
  101. </div>
  102. </div>
  103. {isActive === 0 && <FilesList filteredAndSorted={filteredAndSorted} handleScrollToTheMessage={handleScrollToTheMessage}/>}
  104. {isActive === 1 && <MediaList filteredAndSorted={filteredAndSorted} handleScrollToTheMessage={handleScrollToTheMessage}/>}
  105. {isActive === 2 && <TextList filteredAndSorted={filteredAndSorted} handleScrollToTheMessage={handleScrollToTheMessage}/>}
  106. {isActive === 3 && <AudioList filteredAndSorted={filteredAndSorted} handleScrollToTheMessage={handleScrollToTheMessage}/>}
  107. {isActive === 4 && <VideoList filteredAndSorted={filteredAndSorted} handleScrollToTheMessage={handleScrollToTheMessage}/>}
  108. </>
  109. )
  110. }
  111. export default ProfileLists