ChatListItem.jsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { Avatar} from "@mui/material";
  2. import { memo, useEffect } from "react";
  3. import { connect } from "react-redux";
  4. import { actionSetInputMessageValue } from "../../actions/actionsForChats";
  5. import { backendURL } from "../../helpers/gql";
  6. import { TimeLastMessage } from "../Time/Time";
  7. import { ChatListItemLink, ChatListItemContent, ChatListItemTitle, ChatListItemWrapper, ChatListLastMessage, ChatListItemHeader, ChatListItemFooter, MessageDraftChatListItem } from "./ChatListItem.style";
  8. import OwnerStar from "./icons8_star.png";
  9. import OnwerStarActive from "./icons8_star_white.png";
  10. const ChatListItem = ({chat, handleSetId, isActive, currUserId, setDraftValue, clearInput}) => {
  11. const inputValue = chat?.draft?.mainInputValue?.value || '';
  12. const draftValue = chat?.draft?.draftValue?.value;
  13. let timer;
  14. function showDraftMessage(){
  15. timer = setTimeout(() => {
  16. setDraftValue(chat._id, inputValue, 'draftValue')
  17. }, [10000])
  18. }
  19. useEffect(() => {
  20. showDraftMessage()
  21. return (() => {
  22. clearTimeout(timer);
  23. })
  24. },[inputValue])
  25. return (
  26. <ChatListItemWrapper isActive={isActive} onClick={() => {handleSetId(chat._id); clearInput()}}>
  27. <ChatListItemLink to={`/main/${chat._id}`}>
  28. <Avatar
  29. alt={chat?.title || 'chat'}
  30. src={`${backendURL}/${chat.avatar?.url || ''}`}
  31. sx={{ width: 45, height: 45, mr: '20px'}}
  32. />
  33. <ChatListItemContent>
  34. <ChatListItemHeader>
  35. <ChatListItemTitle isActive={isActive}>{chat.title}
  36. {currUserId === chat?.owner?._id && <img src={isActive ? OnwerStarActive : OwnerStar} alt="ownerStar"/>}
  37. </ChatListItemTitle>
  38. <TimeLastMessage isActive={isActive} timestamp={ + chat.lastMessage?.createdAt}/>
  39. </ChatListItemHeader>
  40. <ChatListItemFooter>
  41. {!draftValue ? <div>
  42. {chat.lastMessage ?
  43. <ChatListLastMessage isActive={isActive}>
  44. <span>{(currUserId == chat.lastMessage?.owner._id) ? 'You' : chat.lastMessage?.owner.nick}:</span>
  45. {chat.lastMessage?.text?.replace(/ /g, "\u00a0") || ''}
  46. </ChatListLastMessage>
  47. : <div></div>}
  48. </div> : <MessageDraftChatListItem isActive={isActive}><span>Draft:</span>{draftValue?.replace(/ /g, "\u00a0") || ''}</MessageDraftChatListItem>}
  49. </ChatListItemFooter>
  50. </ChatListItemContent>
  51. </ChatListItemLink>
  52. </ChatListItemWrapper>
  53. )
  54. }
  55. export default connect(state => ({currUserId: state.auth?.payload?.sub?.id}), { setDraftValue: actionSetInputMessageValue})(memo(ChatListItem));