Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ChatWindow.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React, { useState, useContext, useEffect, useRef } from "react";
  2. import { Button, Form, FormControl, InputGroup } from "react-bootstrap";
  3. import { UserContext } from "../contexts/userContext";
  4. import { useSelector, useDispatch } from "react-redux";
  5. import { chatActions } from "../store/chat-slice";
  6. const ChatWindow = ({ room }) => {
  7. const messagesEndRef = useRef(null)
  8. const scrollToBottom = () => {
  9. messagesEndRef.current?.scrollIntoView({ behavior: "smooth" })
  10. }
  11. const [message, setMessage] = useState("");
  12. const { user } = useContext(UserContext);
  13. const connection = useSelector((state) => state.chat.connection);
  14. const connections = useSelector((state) => state.chat.connections);
  15. const activeRoom = useSelector((state) => state.chat.activeRoom);
  16. const messages = useSelector((state) => state.chat.messages);
  17. const dispatch = useDispatch();
  18. useEffect(() => {
  19. dispatch(chatActions.setMessages(room.messages));
  20. }, [dispatch, room.messages]);
  21. useEffect(()=>{
  22. scrollToBottom();
  23. },[])
  24. const onSendMessageToGroupHandler = async () => {
  25. const userToFetch = connections.filter(
  26. (conn) => conn.userId === user.id && conn.roomId === activeRoom.id
  27. );
  28. await connection.invoke("SendMessageToGroup", {
  29. userId: user.id,
  30. message,
  31. connId: userToFetch[0].connId,
  32. });
  33. setMessage('')
  34. };
  35. return (
  36. <div className="px-3 bg-light-transparent rounded h-100 d-flex flex-column">
  37. <div style={{ height: "80px" }}>
  38. <h2 className="py-3 m-0">{room.name}</h2>
  39. </div>
  40. <div className="messages p-3 border d-flex flex-column-reverse">
  41. {/* mapirane poruke */}
  42. {messages.map((n, index) => (
  43. <div
  44. key={index}
  45. className={
  46. n.senderId === user.id
  47. ? "d-flex flex-column align-items-end"
  48. : "d-flex flex-column align-items-start"
  49. }
  50. >
  51. <p
  52. className={`p-2 px-4 mb-0 rounded message ${
  53. n.senderId !== user.id ? "bg-primary text-light" : "bg-light text-dark"
  54. }`}
  55. >
  56. {/* {n.message} */}
  57. {n.content}
  58. </p>
  59. <p className="text-muted small m-0 p-0 mb-4">{n.username}</p>
  60. </div>
  61. )).reverse()}
  62. <div ref={messagesEndRef} />
  63. </div>
  64. <Form style={{ height: "80px" }} className="d-flex align-items-center">
  65. <InputGroup className="mb-3">
  66. <FormControl
  67. placeholder="Enter your messagge..."
  68. aria-label="Enter your messagge..."
  69. aria-describedby="basic-addon2"
  70. onChange={(e) => setMessage(e.target.value)}
  71. />
  72. <Button
  73. className="px-5"
  74. variant="outline-secondary"
  75. id="button-addon2"
  76. onClick={onSendMessageToGroupHandler}
  77. >
  78. Send
  79. </Button>
  80. </InputGroup>
  81. </Form>
  82. </div>
  83. );
  84. };
  85. export default ChatWindow;