fix: Chat scrollt bei User-Wechsel und erstem Laden sofort ans Ende
This commit is contained in:
@@ -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(() => {
|
||||||
|
if (!bottomRef.current) return;
|
||||||
|
const userChanged = prevUserRef.current !== activeUser?.id;
|
||||||
const isFirstLoad = prevLengthRef.current === 0 && messages.length > 0;
|
const isFirstLoad = prevLengthRef.current === 0 && messages.length > 0;
|
||||||
prevLengthRef.current = messages.length;
|
prevLengthRef.current = messages.length;
|
||||||
if (!bottomRef.current) return;
|
prevUserRef.current = activeUser?.id;
|
||||||
bottomRef.current.scrollIntoView({ behavior: isFirstLoad ? 'instant' : 'smooth' });
|
// Bei User-Wechsel oder erstem Laden: sofort ans Ende springen
|
||||||
}, [messages.length]);
|
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 () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user