Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement autoscroll #112

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
VVoruganti marked this conversation as resolved.
Show resolved Hide resolved
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
Loading