fix: scroll bottom when switch thread (#4152)
This commit is contained in:
parent
cd5e1ff4ed
commit
8657e032a7
@ -2,10 +2,29 @@ import { PropsWithChildren, useCallback, useEffect, useRef } from 'react'
|
|||||||
|
|
||||||
import { ScrollArea } from '@janhq/joi'
|
import { ScrollArea } from '@janhq/joi'
|
||||||
|
|
||||||
|
import { useAtomValue } from 'jotai'
|
||||||
|
|
||||||
|
import { activeThreadAtom } from '@/helpers/atoms/Thread.atom'
|
||||||
|
|
||||||
const ListContainer = ({ children }: PropsWithChildren) => {
|
const ListContainer = ({ children }: PropsWithChildren) => {
|
||||||
const listRef = useRef<HTMLDivElement>(null)
|
const listRef = useRef<HTMLDivElement>(null)
|
||||||
const prevScrollTop = useRef(0)
|
const prevScrollTop = useRef(0)
|
||||||
const isUserManuallyScrollingUp = useRef(false)
|
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<HTMLElement>) => {
|
const handleScroll = useCallback((event: React.UIEvent<HTMLElement>) => {
|
||||||
const currentScrollTop = event.currentTarget.scrollTop
|
const currentScrollTop = event.currentTarget.scrollTop
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user