"use client"; // PEP-207: a conversation opens at the first unread message (or at its end); a new message // arriving while the reader is at the bottom keeps them at the bottom. import { useEffect, useRef } from "react"; export function ChatScroll({ lastId }: { lastId: number }) { const first = useRef(true); useEffect(() => { const end = document.querySelector("[data-chat-end]"); if (first.current) { first.current = false; const unread = document.querySelector("[data-first-unread]"); (unread ?? end)?.scrollIntoView({ block: unread ? "center" : "end" }); return; } const nearBottom = window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 320; if (nearBottom) end?.scrollIntoView({ block: "end", behavior: "smooth" }); }, [lastId]); return null; }