index.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { makeStyles } from '@material-ui/core'
  2. import { useSelector } from 'react-redux'
  3. import React, { useState } from 'react'
  4. import List from '@mui/material/List';
  5. import ListItem from '@mui/material/ListItem';
  6. import ListItemText from '@mui/material/ListItemText';
  7. import ListItemAvatar from '@mui/material/ListItemAvatar';
  8. import Avatar from '@mui/material/Avatar';
  9. import Typography from '@mui/material/Typography';
  10. import Divider from '@mui/material/Divider';
  11. import Search from './Search'
  12. import AlertInfo from "../../../../reusableComponents/AlertInfo";
  13. import { getMessages } from '../../../../../redux/messages/selector'
  14. import { getChat } from '../../../../../redux/chat/selector'
  15. import { timeStampEU, timeStampFilter, firstLetter, slicedWord, handleSort } from '../../../../../helpers'
  16. import { TMessages } from '../../../../../typescript/redux/messages/types';
  17. const useStyles = makeStyles({
  18. container: {
  19. height: '100%',
  20. backgroundColor: '#ffffff'
  21. },
  22. list: {
  23. maxHeight: '93vh',
  24. overflowY: 'scroll',
  25. '&::-webkit-scrollbar': {
  26. width: '0.4em'
  27. },
  28. '&::-webkit-scrollbar-track': {
  29. boxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  30. webkitBoxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  31. backgroundColor: '#eceeec',
  32. },
  33. '&::-webkit-scrollbar-thumb': {
  34. backgroundColor: '#ccc8c8',
  35. },
  36. "&::-webkit-scrollbar-thumb:focus": {
  37. backgroundColor: "#959595",
  38. },
  39. "&::-webkit-scrollbar-thumb:active": {
  40. backgroundColor: "#959595",
  41. },
  42. },
  43. listItem: {
  44. '&:hover': {
  45. backgroundColor: '#f0f0f0',
  46. }
  47. },
  48. })
  49. const SearchList= () => {
  50. const classes = useStyles()
  51. const { sort } = useSelector(getChat)
  52. const messages = useSelector(getMessages)
  53. const [value, setValue] = useState<string>('')
  54. const [date, setDate] = useState<any>('');
  55. const handleSearch = (e: React.ChangeEvent<HTMLInputElement>): void => setValue(e.target.value)
  56. const filteredMessages = (arr:TMessages) => arr.filter((el) => {
  57. if (!date) {
  58. return el.message.toLowerCase().includes(value.toLowerCase())
  59. } else if (el.message.toLowerCase().includes(value.toLowerCase())
  60. && timeStampFilter(date) === timeStampFilter(el.createdAt)) {
  61. return el
  62. }
  63. })
  64. const arr:TMessages = filteredMessages(handleSort('createdAt',messages,sort))
  65. return (
  66. <div className={classes.container}>
  67. <Search handleSearch={handleSearch} value={value}
  68. setDate={setDate} date={date} />
  69. <div className={messages.length > 0 ?classes.list:undefined}>
  70. {messages.length > 0 ? arr.length > 0 ?
  71. <List sx={{ width: '100%' }}>
  72. {arr.map(({ name, lastName, avatarUrl, color, message, createdAt }) =>
  73. <div key={createdAt}>
  74. <ListItem alignItems="flex-start" className={classes.listItem}>
  75. <ListItemAvatar>
  76. <Avatar alt={name} src={avatarUrl?`http://localhost:3000/${avatarUrl}`:undefined}
  77. sx={{ background: color, width: 44, height: 44, marginRight:2 }}>
  78. {!avatarUrl&&`${firstLetter(name)}${firstLetter(lastName)}`}
  79. </Avatar>
  80. </ListItemAvatar>
  81. <ListItemText
  82. primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  83. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  84. secondary={<>
  85. <Typography
  86. sx={{ display: 'block',wordBreak:'break-word' }}
  87. component="span"
  88. variant="body2"
  89. color="text.primary"
  90. >
  91. {message}
  92. </Typography>
  93. {timeStampEU(createdAt)}
  94. </>}
  95. />
  96. </ListItem>
  97. <Divider variant="inset"/>
  98. </div>)}
  99. </List> :
  100. <AlertInfo name={`Can not find message by request: ${value}`}/>:
  101. <AlertInfo name='You do not have messages yet!' />}
  102. </div>
  103. </div>
  104. )
  105. }
  106. export default SearchList