fix: Chat scrollt bei User-Wechsel und erstem Laden sofort ans Ende

This commit is contained in:
2026-06-13 19:27:59 +02:00
parent cbb8d59639
commit d4e9a0aff5

View File

@@ -208,12 +208,20 @@ export default function Nachrichten({ toast, mobile, nav }) {
// ── Scroll to bottom ───────────────────────────────────────────────────────── // ── Scroll to bottom ─────────────────────────────────────────────────────────
const prevLengthRef = useRef(0); const prevLengthRef = useRef(0);
const prevUserRef = useRef(null);
useEffect(() => { useEffect(() => {
const isFirstLoad = prevLengthRef.current === 0 && messages.length > 0;
prevLengthRef.current = messages.length;
if (!bottomRef.current) return; if (!bottomRef.current) return;
bottomRef.current.scrollIntoView({ behavior: isFirstLoad ? 'instant' : 'smooth' }); const userChanged = prevUserRef.current !== activeUser?.id;
}, [messages.length]); const isFirstLoad = prevLengthRef.current === 0 && messages.length > 0;
prevLengthRef.current = messages.length;
prevUserRef.current = activeUser?.id;
// Bei User-Wechsel oder erstem Laden: sofort ans Ende springen
if (userChanged || isFirstLoad) {
bottomRef.current.scrollIntoView({ behavior: 'instant' });
} else if (messages.length > 0) {
bottomRef.current.scrollIntoView({ behavior: 'smooth' });
}
}, [messages.length, activeUser?.id]);
// ── Handlers ───────────────────────────────────────────────────────────────── // ── Handlers ─────────────────────────────────────────────────────────────────
const loadUsers = async () => { const loadUsers = async () => {