fix: Auto-scroll retry with multiple delays (100, 300, 600, 1000ms)

FlatList needs time to render - single setTimeout(150) was unreliable.
Now tries 4 times on initial load, 2 times for new messages.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
duffyduck 2026-04-11 12:07:54 +02:00
parent 64cd5f7d52
commit ea4f639fcb
1 changed files with 10 additions and 6 deletions

View File

@ -373,13 +373,17 @@ const ChatScreen: React.FC = () => {
const shouldAutoScroll = useRef(true); const shouldAutoScroll = useRef(true);
const lastMessageCount = useRef(0); const lastMessageCount = useRef(0);
// Bei neuen Nachrichten: sofort nach unten // Bei neuen Nachrichten oder App-Start: nach unten springen
useEffect(() => { useEffect(() => {
if (messages.length > lastMessageCount.current && shouldAutoScroll.current) { if (messages.length > 0 && shouldAutoScroll.current) {
// Kurzer Delay damit FlatList rendern kann const isInitial = lastMessageCount.current === 0;
setTimeout(() => { // Mehrfach versuchen (FlatList braucht Zeit zum Rendern)
flatListRef.current?.scrollToEnd({ animated: messages.length > lastMessageCount.current + 1 ? false : true }); const delays = isInitial ? [100, 300, 600, 1000] : [100, 300];
}, 150); for (const delay of delays) {
setTimeout(() => {
flatListRef.current?.scrollToEnd({ animated: !isInitial });
}, delay);
}
} }
lastMessageCount.current = messages.length; lastMessageCount.current = messages.length;
}, [messages]); }, [messages]);