ui: mobile chatbot improvements (#1155)

This commit is contained in:
josh 2025-09-07 00:04:51 +01:00 committed by GitHub
parent fd8ed4d863
commit 31de38ab18
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 547 additions and 449 deletions

View file

@ -21,13 +21,6 @@ export function useMessages({
const [hasSentMessage, setHasSentMessage] = useState(false);
useEffect(() => {
if (chatId) {
scrollToBottom('instant');
setHasSentMessage(false);
}
}, [chatId, scrollToBottom]);
useEffect(() => {
if (status === 'submitted') {
setHasSentMessage(true);

View file

@ -1,27 +1,47 @@
import useSWR from 'swr';
import { useRef, useEffect, useCallback } from 'react';
import { useRef, useEffect, useCallback, useState } from 'react';
type ScrollFlag = ScrollBehavior | false;
export function useScrollToBottom() {
const containerRef = useRef<HTMLDivElement>(null);
const endRef = useRef<HTMLDivElement>(null);
const { data: isAtBottom = false, mutate: setIsAtBottom } = useSWR(
'messages:is-at-bottom',
null,
{ fallbackData: false },
);
const [isAtBottom, setIsAtBottom] = useState(true);
const { data: scrollBehavior = false, mutate: setScrollBehavior } =
useSWR<ScrollFlag>('messages:should-scroll', null, { fallbackData: false });
const handleScroll = useCallback(() => {
if (!containerRef.current) return;
const { scrollTop, scrollHeight, clientHeight } = containerRef.current;
// Check if we are within 100px of the bottom (like v0 does)
setIsAtBottom(scrollTop + clientHeight >= scrollHeight - 100);
}, []);
useEffect(() => {
if (scrollBehavior) {
endRef.current?.scrollIntoView({ behavior: scrollBehavior });
const container = containerRef.current;
if (!container) return;
container.addEventListener('scroll', handleScroll);
handleScroll(); // Check initial state
return () => {
container.removeEventListener('scroll', handleScroll);
};
}, [handleScroll]);
useEffect(() => {
if (scrollBehavior && containerRef.current) {
const container = containerRef.current;
const scrollOptions: ScrollToOptions = {
top: container.scrollHeight,
behavior: scrollBehavior
};
container.scrollTo(scrollOptions);
setScrollBehavior(false);
}
}, [setScrollBehavior, scrollBehavior]);
}, [scrollBehavior, setScrollBehavior]);
const scrollToBottom = useCallback(
(scrollBehavior: ScrollBehavior = 'smooth') => {