import remarkGfm from "remark-gfm" import ReactMarkdown from "react-markdown" import {…
- User
import remarkGfm from "remark-gfm" import ReactMarkdown from "react-markdown" import { useParams } from "react-router-dom" import { Config } from "../../../utils/config" import styles from "./dashboard-message.module.css" import botLogo from "../../../assets/images/bot-logo.svg" import { MessageType } from "../../../types/message.type" import { useAccountUser } from "../../../hooks/user.hook" import { Icon } from "../../../ui-component/icon/icon.component" import { Prism as SyntaxHighlighter } from "react-syntax-highlighter" import { prism } from "react-syntax-highlighter/dist/esm/styles/prism" import { Loader } from "../../../ui-component/loader/loader.component" import useWindowDimensions from "../../../hooks/window-dimensions.hook" import { SidebarContext } from "../../side-bar-provider/side-bar.context" import { useRef, useState, useEffect, LegacyRef, useContext } from "react" import { DashboardInput } from "../dashboard-input/dashboard-input.component" import { TokenUtils } from "../../../utils/token.utils"
export function DashboardMessageRoom() { // --------------------------------------------------------------------------- // variables // ---------------------------------------------------------------------------
const { roomId } = useParams() const fetchedRef = useRef(false)
const [text, setText] = useState("") const [skip, setSkip] = useState(0) const accountUser = useAccountUser() const { width: screenWidth } = useWindowDimensions() const [loading, setLoading] = useState(false) const [isDisabled, setDisabled] = useState(false) const messagesEndRef = useRef<HTMLDivElement | null>(null) const [messages, setMessages] = useState<MessageType[]>([]) const { isSideBarOpen, setIsSideBarOpen } = useContext(SidebarContext) const [typingMessage, setTypingMessage] = useState<string | null>(null)
// --------------------------------------------------------------------------- // effects // ---------------------------------------------------------------------------
useEffect(() => { if (screenWidth <= 900) { if (isSideBarOpen) setIsSideBarOpen(false) } else { if (!isSideBarOpen) setIsSideBarOpen(true) } }, [screenWidth])
useEffect(() => { if (messagesEndRef.current) { messagesEndRef.current.scrollIntoView({ behavior: "instant" }) } }, [messages, typingMessage])
// Fetch messages for the specific room when the component mounts or roomId changes useEffect(() => { setSkip(0) fetchedRef.current = false load()
sendMessage(text)}, [roomId])
// --------------------------------------------------------------------------- // functions // ---------------------------------------------------------------------------
async function load() { if (!accountUser || !roomId) return null
try { setLoading(true) fetchedRef.current = true const responseMessage = await fetch(`${Config.url}/api/message/loading`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: "Bearer " + TokenUtils.get(), }, body: JSON.stringify({ skip: skip, roomId: roomId, senderId: accountUser.id, }), credentials: "include", }) const loadMessages: MessageType[] = await responseMessage.json() setMessages(loadMessages) setLoading(false) } catch (error) { console.log("Something went wrong", error) } - Assistant
The
messagesEndRefis a React ref that's used to manage scrolling behavior in theDashboardMessageRoomcomponent, ensuring that the view scrolls to the latest message when messages are updated. Here's a detailed explanation of its usage:-
Declaration:
const messagesEndRef = useRef<HTMLDivElement | null>(null)- This line creates a ref using
useRefand initializes it withnull. Its type is set toHTMLDivElement | null, indicating it will reference a DOM element or be .
- This line creates a ref using
-