You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatCard.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React, { useMemo } from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. ChatInfo,
  5. ChatCardContainer,
  6. Col,
  7. UserImage,
  8. UserImgWrapper,
  9. UserName,
  10. LastMessage,
  11. Line,
  12. } from "./ChatCard.styled";
  13. import { useHistory } from "react-router-dom";
  14. import LittleOfferDetails from "./LittleOfferDetails/LittleOfferDetails";
  15. import MobileOfferDetails from "./MobileOfferDetails/MobileOfferDetails";
  16. import OfferLocation from "./OfferLocation/OfferLocation";
  17. import ChatCommands from "./ChatCommands/ChatCommands";
  18. import useIsMobile from "../../../hooks/useIsMobile";
  19. const ChatCard = (props) => {
  20. const history = useHistory();
  21. const { isMobile } = useIsMobile();
  22. const chat = useMemo(() => {
  23. return props.chat;
  24. }, [props.chat]);
  25. const lastMessage = useMemo(() => {
  26. if (chat?.chat?.messages && chat?.chat?.messages?.length > 0) {
  27. return chat.chat.messages[chat.chat.messages.length - 1]?.text;
  28. }
  29. return "";
  30. }, [chat]);
  31. const routeToItem = () => {
  32. history.push(`/messages/${chat?.chat?._id}`);
  33. };
  34. return (
  35. <ChatCardContainer
  36. onClick={isMobile ? () => routeToItem(chat?.chat?._id) : () => {}}
  37. >
  38. <Col>
  39. <UserImgWrapper>
  40. <UserImage src={chat?.interlocutorData?.image} />
  41. </UserImgWrapper>
  42. <ChatInfo>
  43. <UserName>{chat?.interlocutorData?.name}</UserName>
  44. {/* Only shows on Mobile */}
  45. <MobileOfferDetails chat={chat} />
  46. {/* ^^^^^ */}
  47. <LastMessage>{lastMessage}</LastMessage>
  48. <OfferLocation chat={chat} />
  49. </ChatInfo>
  50. </Col>
  51. <Line />
  52. {/* Only shows on Desktop */}
  53. <LittleOfferDetails chat={chat} />
  54. {/* ^^^^^^^ */}
  55. <ChatCommands routeToItem={() => routeToItem(chat?.chat?._id)} />
  56. </ChatCardContainer>
  57. );
  58. };
  59. ChatCard.propTypes = {
  60. children: PropTypes.node,
  61. title: PropTypes.string,
  62. description: PropTypes.string,
  63. category: PropTypes.string,
  64. author: PropTypes.string,
  65. location: PropTypes.string,
  66. image: PropTypes.node,
  67. quantity: PropTypes.number,
  68. package: PropTypes.string,
  69. numberOfViews: PropTypes.number,
  70. halfwidth: PropTypes.bool,
  71. sponsored: PropTypes.bool,
  72. offer: PropTypes.any,
  73. pinned: PropTypes.bool,
  74. vertical: PropTypes.bool,
  75. chat: PropTypes.any,
  76. };
  77. ChatCard.defaultProps = {
  78. halfwidth: false,
  79. sponsored: false,
  80. };
  81. export default ChatCard;