Skip to content

Commit

Permalink
feat: support more user-friendly scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherlocksuper committed Nov 14, 2024
1 parent 819d249 commit e6b9c07
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -960,9 +960,29 @@ function _Chat() {
(scrollRef.current.scrollTop + scrollRef.current.clientHeight),
) <= 1
: false;
const isAttachWithTop = useCallback(() => {
// if user is typing, should scroll
if (!scrollRef.current) return false;
// get lastMessageId
const lastMessageId = session.messages[session.messages.length - 1]?.id;
if (!lastMessageId) return false;
const lastMessage = document.getElementById(lastMessageId);
if (!lastMessage) return false;

const topDistance =
lastMessage.getBoundingClientRect().top -
scrollRef.current.getBoundingClientRect().top;
// leave some space for user question
return topDistance < 100;
}, [session.messages, scrollRef]);

const isTyping = userInput !== "";

// if user is typing, should auto scroll to bottom
// if user is not typing, should auto scroll to bottom only if already at bottom
const { setAutoScroll, scrollDomToBottom } = useScrollToBottom(
scrollRef,
isScrolledToBottom,
(isScrolledToBottom || isAttachWithTop()) && !isTyping,
);
const [hitBottom, setHitBottom] = useState(true);
const isMobileScreen = useMobileScreen();
Expand Down Expand Up @@ -1717,6 +1737,7 @@ function _Chat() {
? styles["chat-message-user"]
: styles["chat-message"]
}
id={message.id}
>
<div className={styles["chat-message-container"]}>
<div className={styles["chat-message-header"]}>
Expand Down

0 comments on commit e6b9c07

Please sign in to comment.