🚀 Scroll to bottom

This commit is contained in:
Asim Shrestha
2023-04-07 19:14:09 -07:00
parent 5ce19e9efa
commit e4588d9606
2 changed files with 23 additions and 9 deletions

View File

@@ -1,13 +1,23 @@
import cx from "classnames"; import cx from "classnames";
import type { ReactNode } from "react"; import type { ReactNode } from "react";
import React from "react"; import React, { useEffect, useRef } from "react";
interface ChatWindowProps { interface ChatWindowProps {
children?: ReactNode; children?: ReactNode;
className: string; className: string;
messages: Message[];
} }
const ChatWindow = ({ children, className }: ChatWindowProps) => { const ChatWindow = ({ messages, children, className }: ChatWindowProps) => {
const scrollRef = useRef<HTMLDivElement>(null);
useEffect(() => {
// Scroll to bottom on re-renders
if (scrollRef && scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
});
return ( return (
<div <div
className={ className={
@@ -16,7 +26,13 @@ const ChatWindow = ({ children, className }: ChatWindowProps) => {
} }
> >
<MacWindowHeader /> <MacWindowHeader />
<div className="mb-3 mr-3 overflow-y-auto sm:h-[10em] 2xl:h-[20em]"> <div
className="mb-3 mr-3 overflow-y-auto sm:h-[10em] 2xl:h-[20em]"
ref={scrollRef}
>
{messages.map((message, index) => (
<ChatMessage key={`${index}-${message.type}`} message={message} />
))}
{children} {children}
</div> </div>
</div> </div>

View File

@@ -62,14 +62,12 @@ const Home: NextPage = () => {
</div> </div>
</div> </div>
<ChatWindow className="m-10"> <ChatWindow className="m-10" messages={messages}>
{messages.map((message, index) => ( {loading ? (
<ChatMessage <ChatMessage
key={`${index}-${message.type}`} message={{ type: "action", value: "🧠 Thinking..." }}
message={message}
/> />
))} ) : null}
{loading ? <div>LOADING...</div> : null}
</ChatWindow> </ChatWindow>
<Input <Input