diff --git a/web/containers/ListContainer/index.tsx b/web/containers/ListContainer/index.tsx index c55287bc6e..44e5b25276 100644 --- a/web/containers/ListContainer/index.tsx +++ b/web/containers/ListContainer/index.tsx @@ -2,10 +2,29 @@ import { PropsWithChildren, useCallback, useEffect, useRef } from 'react' import { ScrollArea } from '@janhq/joi' +import { useAtomValue } from 'jotai' + +import { activeThreadAtom } from '@/helpers/atoms/Thread.atom' + const ListContainer = ({ children }: PropsWithChildren) => { const listRef = useRef(null) const prevScrollTop = useRef(0) const isUserManuallyScrollingUp = useRef(false) + const activeThread = useAtomValue(activeThreadAtom) + const prevActiveThread = useRef(activeThread) + + // Handle active thread changes + useEffect(() => { + if (prevActiveThread.current?.id !== activeThread?.id) { + isUserManuallyScrollingUp.current = false + const scrollHeight = listRef.current?.scrollHeight ?? 0 + listRef.current?.scrollTo({ + top: scrollHeight, + behavior: 'instant', + }) + prevActiveThread.current = activeThread // Update the previous active thread reference + } + }, [activeThread]) const handleScroll = useCallback((event: React.UIEvent) => { const currentScrollTop = event.currentTarget.scrollTop