Skip to content

Commit

Permalink
feat: add scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioErdeljac committed Apr 26, 2023
1 parent 36f6e44 commit bebdc98
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/conversations/[conversationId]/components/Body.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -12,16 +12,19 @@ interface BodyProps {
}

const Body: React.FC<BodyProps> = ({ initialMessages = [] }) => {
const bottomRef = useRef<HTMLDivElement>(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)
Expand All @@ -38,8 +41,9 @@ const Body: React.FC<BodyProps> = ({ initialMessages = [] }) => {
{messages.map((message) => (
<MessageBox key={message.id} data={message} />
))}
<div className="pt-24" ref={bottomRef} />
</div>
);
);
}

export default Body;

0 comments on commit bebdc98

Please sign in to comment.