From a20b60db75aeecde526840c284bd698c70f01e50 Mon Sep 17 00:00:00 2001 From: hzt <3061613175@qq.com> Date: Fri, 20 Feb 2026 12:20:52 +0800 Subject: [PATCH] fix: reorder chat history sidebar after new message in existing chat When sending a new message in an existing/resumed chat, the thread's position in the sidebar history list was not updated. This happened because the first_interaction event only fires once per session, so subsequent messages never triggered a thread list refresh. Track message count changes and update the active thread's createdAt timestamp when a new user message is detected, which triggers groupByDate to re-sort and move the thread to the top of the list. Fixes #2763 --- .../components/LeftSidebar/ThreadHistory.tsx | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/frontend/src/components/LeftSidebar/ThreadHistory.tsx b/frontend/src/components/LeftSidebar/ThreadHistory.tsx index e7c2bf4866..b1c6ded7ff 100644 --- a/frontend/src/components/LeftSidebar/ThreadHistory.tsx +++ b/frontend/src/components/LeftSidebar/ThreadHistory.tsx @@ -30,6 +30,7 @@ export function ThreadHistory() { const [isLoadingMore, setIsLoadingMore] = useState(false); const [isFetching, setIsFetching] = useState(false); const [shouldLoadMore, setShouldLoadMore] = useState(false); + const prevMessageCountRef = useRef(0); // Restore scroll position useEffect(() => { @@ -60,6 +61,35 @@ export function ThreadHistory() { handleFirstInteraction(); }, [firstInteraction]); + // Reorder thread to top when a new message is sent in the current thread + useEffect(() => { + const currentCount = messages.length; + const prevCount = prevMessageCountRef.current; + prevMessageCountRef.current = currentCount; + + if ( + threadId && + currentCount > prevCount && + prevCount > 0 && + threadHistory?.threads + ) { + const lastMessage = messages[currentCount - 1]; + if (lastMessage?.type === 'user_message') { + setThreadHistory((prev) => { + if (!prev?.threads) return prev; + const threadIndex = prev.threads.findIndex((t) => t.id === threadId); + if (threadIndex <= 0) return prev; // Already at top or not found + const updatedThreads = [...prev.threads]; + updatedThreads[threadIndex] = { + ...updatedThreads[threadIndex], + createdAt: new Date().toISOString() + }; + return { ...prev, threads: updatedThreads }; + }); + } + } + }, [messages.length, threadId]); + const handleScroll = () => { if (!scrollRef.current) return; const { scrollHeight, clientHeight, scrollTop } = scrollRef.current;