diff --git a/app/conversations/[conversationId]/components/Body.tsx b/app/conversations/[conversationId]/components/Body.tsx index 5560230..68d8832 100644 --- a/app/conversations/[conversationId]/components/Body.tsx +++ b/app/conversations/[conversationId]/components/Body.tsx @@ -1,6 +1,6 @@ 'use client'; -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import MessageBox from "./MessageBox"; import { Message, User } from "@prisma/client"; import { pusherClient } from "@/app/libs/pusher"; @@ -12,16 +12,19 @@ interface BodyProps { } const Body: React.FC = ({ initialMessages = [] }) => { + const bottomRef = useRef(null); const [messages, setMessages] = useState(initialMessages); const params = useParams(); const { conversationId } = params; useEffect(() => { pusherClient.subscribe(conversationId) + bottomRef?.current?.scrollIntoView(); const messageHandler = (message: Message & { sender: User }) => { axios.post(`/api/conversations/${conversationId}/seen`); setMessages((current) => [...current, message]); + bottomRef?.current?.scrollIntoView(); } pusherClient.bind('conversation-message', messageHandler) @@ -38,8 +41,9 @@ const Body: React.FC = ({ initialMessages = [] }) => { {messages.map((message) => ( ))} +
- ); + ); } export default Body; \ No newline at end of file