diff --git a/src/components/Chat.tsx b/src/components/Chat.tsx index f7d3fb4..583230e 100644 --- a/src/components/Chat.tsx +++ b/src/components/Chat.tsx @@ -75,7 +75,7 @@ export function Chat({ messages, isGenerating, isLoadingHistory, status, session const isNearBottomRef = useRef(true); const userSentRef = useRef(false); const [showScrollBtn, setShowScrollBtn] = useState(false); - const [hasNewMessages, setHasNewMessages] = useState(false); + const [newMessageCount, setNewMessageCount] = useState(0); const prevMessageCountRef = useRef(messages.length); const checkIfNearBottom = useCallback(() => { @@ -85,7 +85,7 @@ export function Chat({ messages, isGenerating, isLoadingHistory, status, session isNearBottomRef.current = distanceFromBottom <= SCROLL_THRESHOLD; setShowScrollBtn(distanceFromBottom > SCROLL_THRESHOLD * 2); if (distanceFromBottom <= SCROLL_THRESHOLD) { - setHasNewMessages(false); + setNewMessageCount(0); } }, []); @@ -108,7 +108,7 @@ export function Chat({ messages, isGenerating, isLoadingHistory, status, session if (sessionKey !== prevSessionKeyRef.current) { prevSessionKeyRef.current = sessionKey; prevMessageCountRef.current = messages.length; - setHasNewMessages(false); + setNewMessageCount(0); isNearBottomRef.current = true; // Scroll to bottom on session switch requestAnimationFrame(() => scrollToBottom('instant')); @@ -119,7 +119,8 @@ export function Chat({ messages, isGenerating, isLoadingHistory, status, session const wasLoadingHistoryRef = useRef(isLoadingHistory); useEffect(() => { const newCount = messages.length; - const hadNew = newCount > prevMessageCountRef.current; + const delta = newCount - prevMessageCountRef.current; + const hadNew = delta > 0; // Detect history load completion (don't treat as "new messages") const justFinishedLoading = wasLoadingHistoryRef.current && !isLoadingHistory; wasLoadingHistoryRef.current = isLoadingHistory; @@ -129,7 +130,7 @@ export function Chat({ messages, isGenerating, isLoadingHistory, status, session // History just loaded — scroll to bottom, don't show indicator scrollToBottom('instant'); isNearBottomRef.current = true; - setHasNewMessages(false); + setNewMessageCount(0); return; } @@ -138,15 +139,15 @@ export function Chat({ messages, isGenerating, isLoadingHistory, status, session userSentRef.current = false; scrollToBottom('smooth'); isNearBottomRef.current = true; - setHasNewMessages(false); + setNewMessageCount(0); return; } if (isNearBottomRef.current) { scrollToBottom('smooth'); - setHasNewMessages(false); + setNewMessageCount(0); } else if (hadNew) { // New message arrived while scrolled up - setHasNewMessages(true); + setNewMessageCount(c => c + delta); } }, [messages, isGenerating, isLoadingHistory, scrollToBottom]); @@ -288,7 +289,7 @@ export function Chat({ messages, isGenerating, isLoadingHistory, status, session
{/* Floating action buttons — sticky to bottom of scroll area */} - {(hasToolCalls || showScrollBtn || hasNewMessages) && ( + {(hasToolCalls || showScrollBtn || newMessageCount > 0) && (