Skip to content

Commit

Permalink
implement autoscroll (#112)
Browse files Browse the repository at this point in the history
* implement autoscroll
Refactored imports in "page.tsx" to improve readability and maintainability. Updated the type of the "input" ref to be more specific. Added a new state and ref to track the scroll position of the message container. Set up an event listener to update the state when the scroll position changes. Made adjustments to the scroll position in certain event handlers.

* update scroll

---------

Co-authored-by: Vineeth Voruganti <[email protected]>
  • Loading branch information
2 people authored and jacobvm04 committed Dec 3, 2023
1 parent 67e3b2b commit 04fc6e4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
5 changes: 4 additions & 1 deletion www/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
@tailwind components;
@tailwind utilities;

html {
scroll-behavior: smooth;
}

.typing_dot {
animation: typing 1s infinite;
Expand All @@ -28,4 +31,4 @@

.typing_dot:nth-child(3) {
animation-delay: 0.4s;
}
}
56 changes: 53 additions & 3 deletions www/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import {
} from "react-icons/fa";
// import { IoIosArrowDown } from "react-icons/io";
// import { GrClose } from "react-icons/gr";
import { useRef, useEffect, useState, useCallback } from "react";
import {
useRef,
useEffect,
useState,
useCallback,
use,
ElementRef,
} from "react";

import { v4 as uuidv4 } from "uuid";
import Typing from "@/components/typing";
Expand Down Expand Up @@ -56,9 +63,12 @@ export default function Home() {
conversation_id: "",
name: "",
});
const input = useRef<HTMLInputElement>(null);
const input = useRef<ElementRef<"input">>(null);
const supabase = createClientComponentClient();

const isAtBottom = useRef(true);
const messageContainerRef = useRef<ElementRef<"section">>(null);

const newChat = useCallback(async () => {
return await fetch(`${URL}/api/conversations/insert?user_id=${userId}`)
.then((res) => res.json())
Expand Down Expand Up @@ -144,8 +154,33 @@ export default function Home() {
setMessages([defaultMessage, ...messages]);
});
}

// scroll to bottom
const messageContainer = messageContainerRef.current;
if (messageContainer) {
messageContainer.scrollTop = messageContainer.scrollHeight;
}
}, [currentConversation, userId]);

useEffect(() => {
const messageContainer = messageContainerRef.current;
if (!messageContainer) return;

const func = () => {
const val =
Math.round(
messageContainer.scrollHeight - messageContainer.scrollTop
) === messageContainer.clientHeight;
isAtBottom.current = val;
};

messageContainer.addEventListener("scroll", func);

return () => {
messageContainer.removeEventListener("scroll", func);
};
}, []);

// async function newChat() {
// return await fetch(`${URL}/api/conversations/insert?user_id=${userId}`)
// .then((res) => res.json())
Expand Down Expand Up @@ -207,6 +242,11 @@ export default function Home() {

const reader = data.body?.pipeThrough(new TextDecoderStream()).getReader()!;

const messageContainer = messageContainerRef.current;
if (messageContainer) {
messageContainer.scrollTop = messageContainer.scrollHeight;
}

// clear the last message
setMessages((prev) => {
prev[prev.length - 1].text = "";
Expand Down Expand Up @@ -240,6 +280,13 @@ export default function Home() {
prev[prev.length - 1].text += value;
return [...prev];
});

if (isAtBottom.current) {
const messageContainer = messageContainerRef.current;
if (messageContainer) {
messageContainer.scrollTop = messageContainer.scrollHeight;
}
}
}
}
}
Expand Down Expand Up @@ -285,7 +332,10 @@ export default function Home() {
</p>
</section>
)}
<section className="flex flex-col flex-1 overflow-y-auto lg:px-5">
<section
className="flex flex-col flex-1 overflow-y-auto lg:px-5"
ref={messageContainerRef}
>
{messages.map((message, i) => (
<Message isUser={message.isUser} key={i}>
<MarkdownWrapper text={message.text} />
Expand Down

0 comments on commit 04fc6e4

Please sign in to comment.