From 071354be876c5f81987ab5b62c36c39d3ce8152a Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Mon, 19 Jan 2026 00:14:41 +0530 Subject: [PATCH] feat: Implement auto-scrolling to the bottom of chat messages and a scroll-to-bottom button that appears when scrolled up. --- src/app/(agent)/agent/message/page.tsx | 89 ++++++++++++++++++-------- 1 file changed, 61 insertions(+), 28 deletions(-) diff --git a/src/app/(agent)/agent/message/page.tsx b/src/app/(agent)/agent/message/page.tsx index 38f5674..fd35d55 100644 --- a/src/app/(agent)/agent/message/page.tsx +++ b/src/app/(agent)/agent/message/page.tsx @@ -1,6 +1,6 @@ 'use client'; -import { useState } from 'react'; +import { useState, useRef, useEffect } from 'react'; import Image from 'next/image'; import { ChatHeader, MessageInput } from './component'; @@ -209,12 +209,36 @@ const messages = [ export default function MessagePage() { const [selectedConversation, setSelectedConversation] = useState('1'); const [searchQuery, setSearchQuery] = useState(''); + const [showScrollButton, setShowScrollButton] = useState(false); + const messagesContainerRef = useRef(null); const handleSendMessage = (message: string) => { console.log('Sending message:', message); // In production, this would send the message to the API }; + const handleScroll = () => { + if (messagesContainerRef.current) { + const { scrollTop, scrollHeight, clientHeight } = messagesContainerRef.current; + // Show button if not at bottom (with 100px threshold) + setShowScrollButton(scrollHeight - scrollTop - clientHeight > 100); + } + }; + + const scrollToBottom = () => { + if (messagesContainerRef.current) { + messagesContainerRef.current.scrollTo({ + top: messagesContainerRef.current.scrollHeight, + behavior: 'smooth' + }); + } + }; + + useEffect(() => { + // Scroll to bottom on initial load + scrollToBottom(); + }, [selectedConversation]); + const filteredConversations = conversations.filter((conv) => conv.name.toLowerCase().includes(searchQuery.toLowerCase()) ); @@ -329,39 +353,48 @@ export default function MessagePage() { {/* Chat header */} - {/* Messages area */} -
-
- {messages.map((message) => ( -
+ {/* Messages area wrapper */} +
+
+
+ {messages.map((message) => (
-

- {message.text} -

-

- {message.timestamp} -

+

+ {message.text} +

+

+ {message.timestamp} +

+
-
- ))} + ))} +
- {/* Scroll to bottom button */} -
- -
+ )}
{/* Message input */}