2026-05-13 16:39:07 +00:00
|
|
|
import { type ReactNode, useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
|
2026-04-18 18:51:53 +00:00
|
|
|
import { ArrowDown } from "lucide-react";
|
2026-04-19 06:39:06 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-04-18 18:51:53 +00:00
|
|
|
|
|
|
|
|
import { ThreadMessages } from "@/components/thread/ThreadMessages";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
import type { UIMessage } from "@/lib/types";
|
|
|
|
|
|
|
|
|
|
interface ThreadViewportProps {
|
|
|
|
|
messages: UIMessage[];
|
|
|
|
|
isStreaming: boolean;
|
|
|
|
|
composer: ReactNode;
|
|
|
|
|
emptyState?: ReactNode;
|
2026-05-13 16:39:07 +00:00
|
|
|
scrollToBottomSignal?: number;
|
|
|
|
|
conversationKey?: string | null;
|
2026-04-18 18:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const NEAR_BOTTOM_PX = 48;
|
|
|
|
|
|
|
|
|
|
export function ThreadViewport({
|
|
|
|
|
messages,
|
|
|
|
|
isStreaming,
|
|
|
|
|
composer,
|
|
|
|
|
emptyState,
|
2026-05-13 16:39:07 +00:00
|
|
|
scrollToBottomSignal = 0,
|
|
|
|
|
conversationKey = null,
|
2026-04-18 18:51:53 +00:00
|
|
|
}: ThreadViewportProps) {
|
2026-04-19 06:39:06 +00:00
|
|
|
const { t } = useTranslation();
|
2026-04-18 18:51:53 +00:00
|
|
|
const scrollRef = useRef<HTMLDivElement>(null);
|
2026-05-13 16:39:07 +00:00
|
|
|
const contentRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const bottomRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const lastConversationKeyRef = useRef<string | null>(conversationKey);
|
|
|
|
|
const pendingConversationScrollRef = useRef(true);
|
|
|
|
|
const scrollFrameIdsRef = useRef<number[]>([]);
|
|
|
|
|
const forceBottomUntilRef = useRef(0);
|
2026-04-18 18:51:53 +00:00
|
|
|
const [atBottom, setAtBottom] = useState(true);
|
|
|
|
|
const hasMessages = messages.length > 0;
|
|
|
|
|
|
2026-05-13 16:39:07 +00:00
|
|
|
const cancelScheduledBottomScroll = useCallback(() => {
|
|
|
|
|
for (const id of scrollFrameIdsRef.current) {
|
|
|
|
|
window.cancelAnimationFrame(id);
|
|
|
|
|
}
|
|
|
|
|
scrollFrameIdsRef.current = [];
|
2026-04-18 18:51:53 +00:00
|
|
|
}, []);
|
|
|
|
|
|
2026-05-13 16:39:07 +00:00
|
|
|
const scrollToBottomNow = useCallback((smooth = false) => {
|
|
|
|
|
const el = scrollRef.current;
|
|
|
|
|
const marker = bottomRef.current;
|
|
|
|
|
const behavior: ScrollBehavior = smooth ? "smooth" : "auto";
|
|
|
|
|
if (marker) {
|
|
|
|
|
marker.scrollIntoView({ block: "end", behavior });
|
|
|
|
|
} else if (el) {
|
|
|
|
|
el.scrollTo({ top: el.scrollHeight, behavior });
|
|
|
|
|
}
|
|
|
|
|
setAtBottom(true);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const scrollToBottom = useCallback((smooth = false, frames = 1) => {
|
|
|
|
|
cancelScheduledBottomScroll();
|
|
|
|
|
scrollToBottomNow(smooth);
|
|
|
|
|
for (let i = 1; i < frames; i += 1) {
|
|
|
|
|
const id = window.requestAnimationFrame(() => scrollToBottomNow(smooth));
|
|
|
|
|
scrollFrameIdsRef.current.push(id);
|
|
|
|
|
}
|
|
|
|
|
}, [cancelScheduledBottomScroll, scrollToBottomNow]);
|
|
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!atBottom) return;
|
|
|
|
|
scrollToBottom(!isStreaming);
|
|
|
|
|
}, [messages, isStreaming, atBottom, scrollToBottom]);
|
|
|
|
|
|
2026-05-13 16:39:07 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (scrollToBottomSignal <= 0) return;
|
|
|
|
|
forceBottomUntilRef.current = Date.now() + 2_000;
|
|
|
|
|
scrollToBottom(true, 8);
|
|
|
|
|
}, [scrollToBottomSignal, scrollToBottom]);
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
if (lastConversationKeyRef.current === conversationKey) return;
|
|
|
|
|
lastConversationKeyRef.current = conversationKey;
|
|
|
|
|
pendingConversationScrollRef.current = true;
|
|
|
|
|
forceBottomUntilRef.current = Date.now() + 2_000;
|
|
|
|
|
setAtBottom(true);
|
|
|
|
|
}, [conversationKey]);
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
if (!pendingConversationScrollRef.current) return;
|
|
|
|
|
if (!conversationKey) {
|
|
|
|
|
pendingConversationScrollRef.current = false;
|
|
|
|
|
scrollToBottom(false, 4);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
scrollToBottom(false, 8);
|
|
|
|
|
if (!hasMessages) return;
|
|
|
|
|
pendingConversationScrollRef.current = false;
|
|
|
|
|
}, [conversationKey, hasMessages, messages, scrollToBottom]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => cancelScheduledBottomScroll, [cancelScheduledBottomScroll]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const target = contentRef.current;
|
|
|
|
|
if (!target || typeof ResizeObserver === "undefined") return;
|
|
|
|
|
const observer = new ResizeObserver(() => {
|
|
|
|
|
if (!atBottom && Date.now() > forceBottomUntilRef.current) return;
|
|
|
|
|
scrollToBottom(false, 4);
|
|
|
|
|
});
|
|
|
|
|
observer.observe(target);
|
|
|
|
|
return () => observer.disconnect();
|
|
|
|
|
}, [atBottom, hasMessages, scrollToBottom]);
|
|
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
const el = scrollRef.current;
|
|
|
|
|
if (!el) return;
|
|
|
|
|
|
|
|
|
|
const onScroll = () => {
|
|
|
|
|
const distance = el.scrollHeight - el.scrollTop - el.clientHeight;
|
|
|
|
|
setAtBottom(distance < NEAR_BOTTOM_PX);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onScroll();
|
|
|
|
|
el.addEventListener("scroll", onScroll, { passive: true });
|
|
|
|
|
return () => el.removeEventListener("scroll", onScroll);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="relative flex min-h-0 flex-1 overflow-hidden">
|
|
|
|
|
<div
|
|
|
|
|
ref={scrollRef}
|
|
|
|
|
className={cn(
|
|
|
|
|
"absolute inset-0 overflow-y-auto scroll-smooth scrollbar-thin",
|
|
|
|
|
"[&::-webkit-scrollbar]:w-1.5",
|
|
|
|
|
"[&::-webkit-scrollbar-thumb]:rounded-full",
|
|
|
|
|
"[&::-webkit-scrollbar-thumb]:bg-muted-foreground/30",
|
|
|
|
|
"[&::-webkit-scrollbar-track]:bg-transparent",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{hasMessages ? (
|
2026-05-13 16:39:07 +00:00
|
|
|
<div ref={contentRef} className="mx-auto flex min-h-full w-full max-w-[64rem] flex-col">
|
2026-04-18 18:51:53 +00:00
|
|
|
<div className="flex-1 px-4 pb-20 pt-4">
|
2026-04-23 07:43:21 +00:00
|
|
|
<div className="mx-auto w-full max-w-[49.5rem]">
|
|
|
|
|
<ThreadMessages messages={messages} />
|
|
|
|
|
</div>
|
2026-04-18 18:51:53 +00:00
|
|
|
</div>
|
|
|
|
|
|
2026-04-23 07:43:21 +00:00
|
|
|
<div className="sticky bottom-0 z-10 mt-auto bg-background">
|
2026-04-18 18:51:53 +00:00
|
|
|
<div className="px-4 pb-3">
|
|
|
|
|
{composer}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
2026-05-13 16:39:07 +00:00
|
|
|
<div ref={contentRef} className="mx-auto flex min-h-full w-full max-w-[72rem] flex-col px-4">
|
2026-05-06 14:15:36 +00:00
|
|
|
<div className="flex w-full flex-1 items-center justify-center pb-[7vh] pt-8">
|
|
|
|
|
<div className="flex w-full max-w-[58rem] flex-col gap-6">
|
2026-04-18 18:51:53 +00:00
|
|
|
{emptyState}
|
|
|
|
|
<div className="w-full">{composer}</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-05-13 16:39:07 +00:00
|
|
|
<div ref={bottomRef} aria-hidden className="h-px" />
|
2026-04-18 18:51:53 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
aria-hidden
|
|
|
|
|
className="pointer-events-none absolute inset-x-0 top-0 h-6 bg-gradient-to-b from-background to-transparent"
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{!atBottom && (
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={() => scrollToBottom(true)}
|
|
|
|
|
className={cn(
|
|
|
|
|
"absolute bottom-28 left-1/2 h-8 w-8 -translate-x-1/2 rounded-full shadow-md",
|
|
|
|
|
"bg-background/90 backdrop-blur",
|
|
|
|
|
"animate-in fade-in-0 zoom-in-95",
|
|
|
|
|
)}
|
2026-04-19 06:39:06 +00:00
|
|
|
aria-label={t("thread.scrollToBottom")}
|
2026-04-18 18:51:53 +00:00
|
|
|
>
|
|
|
|
|
<ArrowDown className="h-4 w-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|