diff --git a/src/ProChat/components/ScrollAnchor/index.tsx b/src/ProChat/components/ScrollAnchor/index.tsx index b744a6ed..299a872c 100644 --- a/src/ProChat/components/ScrollAnchor/index.tsx +++ b/src/ProChat/components/ScrollAnchor/index.tsx @@ -1,4 +1,4 @@ -import { memo, useEffect } from 'react'; +import { memo, useEffect, useState } from 'react'; import { useInView } from 'react-intersection-observer'; import { useStore } from '@/ProChat/store'; @@ -10,15 +10,31 @@ const ChatScrollAnchor = memo(() => { const trackVisibility = useStore((s) => !!s.chatLoadingId); const str = useStore(chatSelectors.currentChats); + const [isWindowAvailable, setIsWindowAvailable] = useState(false); + + useEffect(() => { + // 检查window对象是否已经可用 + if (typeof window !== 'undefined') { + setIsWindowAvailable(true); + } + }, []); + const { ref, entry, inView } = useInView({ delay: 100, rootMargin: '0px 0px -150px 0px', trackVisibility, }); - // 如果是移动端,可能200太多了,认为超过 1/3 即可,PC默认200 - const ScrollOffset = window.innerHeight / 3 > 200 ? 200 : window.innerHeight / 4; - const isAtBottom = useAtBottom(ScrollOffset); + const [scrollOffset, setScrollOffset] = useState(0); + + useEffect(() => { + if (isWindowAvailable) { + // 如果是移动端,可能200太多了,认为超过 1/3 即可,PC默认200 + setScrollOffset(window.innerHeight / 3 > 200 ? 200 : window.innerHeight / 4); + } + }, [isWindowAvailable]); + + const isAtBottom = useAtBottom(scrollOffset); useEffect(() => { if (isAtBottom && trackVisibility && !inView) {