"use client"; import { cn } from "@/lib/utils"; import { useTambo } from "@tambo-ai/react"; import * as React from "react"; import { useEffect, useRef } from "react"; /** * Props for the ScrollableMessageContainer component */ export type ScrollableMessageContainerProps = React.HTMLAttributes; /** * A scrollable container for message content with auto-scroll functionality. * Used across message thread components for consistent scrolling behavior. * * @example * ```tsx * * * * * * ``` */ export const ScrollableMessageContainer = React.forwardRef< HTMLDivElement, ScrollableMessageContainerProps >(({ className, children, ...props }, ref) => { const scrollContainerRef = useRef(null); const { thread } = useTambo(); // Handle forwarded ref React.useImperativeHandle(ref, () => scrollContainerRef.current!, []); // Auto-scroll to bottom when messages change useEffect(() => { if (scrollContainerRef.current && thread?.messages?.length) { const timeoutId = setTimeout(() => { if (scrollContainerRef.current) { scrollContainerRef.current.scrollTo({ top: scrollContainerRef.current.scrollHeight, behavior: "smooth", }); } }, 250); return () => clearTimeout(timeoutId); } }, [thread?.messages]); return (
{children}
); }); ScrollableMessageContainer.displayName = "ScrollableMessageContainer";