Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions frontend/src/components/LeftSidebar/ThreadHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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;
Expand Down
Loading