From b953391737b239868965ac2c6c10a47ee457ad79 Mon Sep 17 00:00:00 2001 From: weishu Date: Thu, 25 Dec 2025 14:56:06 +0800 Subject: [PATCH] fix: improve message counter initialization with bootstrap check Add hasBootstrappedRef to track component initialization and prevent counting messages during the initial render. This fixes the new message counter behavior when messages load on component mount and ensures accurate message tracking when autoScroll is disabled. --- web/src/components/AssistantChat/HappyThread.tsx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/web/src/components/AssistantChat/HappyThread.tsx b/web/src/components/AssistantChat/HappyThread.tsx index e267d48d..ceda7685 100644 --- a/web/src/components/AssistantChat/HappyThread.tsx +++ b/web/src/components/AssistantChat/HappyThread.tsx @@ -82,6 +82,7 @@ export function HappyThread(props: { const prevRenderedCountRef = useRef(props.renderedMessagesCount) const autoScrollEnabledRef = useRef(autoScrollEnabled) const newMessageCountRef = useRef(newMessageCount) + const hasBootstrappedRef = useRef(false) // Keep refs in sync with state useEffect(() => { @@ -121,10 +122,22 @@ export function HappyThread(props: { const currentCount = props.renderedMessagesCount const wasLoadingMore = wasLoadingMoreRef.current wasLoadingMoreRef.current = props.isLoadingMoreMessages + + if (props.isLoadingMessages) { + prevRenderedCountRef.current = currentCount + return + } + + if (!hasBootstrappedRef.current) { + hasBootstrappedRef.current = true + prevRenderedCountRef.current = currentCount + return + } + prevRenderedCountRef.current = currentCount // Skip during loading states - if (props.isLoadingMoreMessages || props.isLoadingMessages) { + if (props.isLoadingMoreMessages) { return } @@ -154,6 +167,7 @@ export function HappyThread(props: { setAutoScrollEnabled(true) setNewMessageCount(0) prevRenderedCountRef.current = 0 + hasBootstrappedRef.current = false }, [props.sessionId]) const handleLoadMore = useCallback(() => {