PrivateChat.jsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { Avatar, Box, Button} from '@mui/material';
  2. import { dateFormat } from '../utils/dateFormat';
  3. import { useSelector } from 'react-redux';
  4. import { useRef, useEffect, useState} from 'react';
  5. import { scrollToBottom } from '../utils/scrollToBottom';
  6. import { useDispatch } from 'react-redux';
  7. import { editMessage } from '../../../reducers/messageReducer';
  8. import { MessageEditorMenu } from '../MessageEditorMenu.jsx';
  9. import imgBtn from '../../../assets/img/gg.png';
  10. import useSound from 'use-sound';
  11. import { PrivatChatHeader } from './PrivatChatHeader';
  12. import { privateMessage } from '../../../reducers/userDataReducer';
  13. import notifSound from '../../../assets/get.mp3'
  14. import { UserInfoButton } from '../generalChat/UserInfoButton';
  15. //need to fix update wenn message sendet and icon for new private messages
  16. export const PrivateChat = () => {
  17. const dispatch = useDispatch();
  18. const socket = useSelector(state => state.getUserSocketReducer.socket)
  19. const SERVER_URL =process.env.REACT_APP_SERVER_URL
  20. const user = useSelector(state => state.getUserSocketReducer.socketUserData)
  21. const storeMessageId = useSelector(state => state.messageReducer.messageId)
  22. const selectedUser = useSelector(state => state.dataReducer.selectedUser)
  23. const newPrivateMessages = useSelector(state => state.getUserSocketReducer.newPrivateMessages)
  24. const isNewMessage = newPrivateMessages.length > 0
  25. const [startMessages, setStartMessages] = useState([])
  26. let endMessages = useRef(null);
  27. socket.on('send privat messages', (messages)=> {
  28. setStartMessages(messages)
  29. });
  30. // bug need to fix
  31. const [isEditing, setIsEditing] = useState(false)
  32. const [isEditiedMessage, setIsEditiedMessage] = useState(false) //need to type in the bottom of message after message was edited
  33. const regYoutube = /http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?/; //for youtube video
  34. const [play] = useSound(notifSound);
  35. useEffect(() => {
  36. if (!isEditing) {
  37. scrollToBottom((endMessages))
  38. }
  39. }, [startMessages, newPrivateMessages]);
  40. return (
  41. <>
  42. <PrivatChatHeader/>
  43. <Box className='messageBox'>
  44. {
  45. startMessages.map((item, i) =>
  46. <div key={i + 1} className={
  47. (item.fromUser === user._id)? 'message myMessage' :'message'}
  48. onClick = {(e) => {
  49. console.log(e.target)
  50. if(e.target.closest("div").className.includes('myMessage') && (item.userName === user.userName) && (item.text === e.target.textContent)){
  51. e.currentTarget.className += ' editMessage'
  52. dispatch(editMessage({socket, editMessage: e.target.textContent, messageId: item._id}))
  53. setIsEditing(true)
  54. }
  55. }}
  56. >
  57. {storeMessageId === item._id ? <MessageEditorMenu />: ""}
  58. <div
  59. key={i}
  60. className={
  61. (item.fromUser === user._id)? 'message myMessage' :'message'}>
  62. {
  63. item.text.match(regYoutube) ?
  64. <iframe
  65. width="280"
  66. height="160"
  67. style={{'maxWidth': "90%"}}
  68. src={`https://www.youtube.com/embed/`+ (item.text.match(regYoutube)[1])}
  69. title="YouTube video player"
  70. allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
  71. allowFullScreen>
  72. </iframe>
  73. :
  74. (item.file && item.fileType && item.fileType.split('/')[0] !== 'image') ?
  75. <div style={{'display': 'flex', 'alignItems': 'center'}} >
  76. <a href={SERVER_URL + '/' +item.file} download>
  77. <Button
  78. variant="contained"
  79. component="label"
  80. sx = {{
  81. minWidth: 'auto',
  82. minHeight: '25px',
  83. backgroundImage:'url(' + imgBtn + ')' ,
  84. backgroundPosition: 'center',
  85. backgroundRepeat: "no-repeat",
  86. backgroundSize: '15px 20px',
  87. backgroundColor: '#d3d3d3'
  88. }}
  89. >
  90. </Button>
  91. </a>
  92. <p style={{'marginLeft': '15px'}} >{item.text}</p>
  93. </div>
  94. :
  95. <p>{item.text}</p>
  96. }
  97. {
  98. (item.file && item.fileType && item.fileType.split('/')[0] == 'image' ) //need to fix for other type files
  99. ?
  100. <img width={'auto'} style={{'maxWidth': "90%"}} src={ SERVER_URL + '/' + item.file} alt={'error load image'}/>
  101. :
  102. ''
  103. }
  104. </div>
  105. <div className={
  106. (item.userName === user.userName)? 'myDate' :'date'}>
  107. {dateFormat(item).time}
  108. </div>
  109. {isEditiedMessage && <i>Edited</i>}
  110. {/* <div className={
  111. (item.fromUser === user._id)? 'myDate' :'date'}>
  112. {dateFormat(item).time}
  113. </div> */}
  114. </div>
  115. )}
  116. <div ref={endMessages}></div>
  117. </Box>
  118. </>
  119. )
  120. }