fix: ensure comment thread always scrolls to bottom on first render

This commit is contained in:
lyzno1 2025-10-12 13:27:42 +08:00
parent 0ac32188c5
commit 44fe71e4db
No known key found for this signature in database
1 changed files with 7 additions and 4 deletions

View File

@ -256,8 +256,8 @@ export const CommentThread: FC<CommentThreadProps> = memo(({
const replies = comment.replies || []
const messageListRef = useRef<HTMLDivElement>(null)
const previousReplyCountRef = useRef(replies.length)
const previousCommentIdRef = useRef(comment.id)
const previousReplyCountRef = useRef<number | undefined>(undefined)
const previousCommentIdRef = useRef<string | undefined>(undefined)
// Close dropdown when scrolling
useEffect(() => {
@ -279,10 +279,13 @@ export const CommentThread: FC<CommentThreadProps> = memo(({
if (!container)
return
const isFirstRender = previousCommentIdRef.current === undefined
const isNewComment = comment.id !== previousCommentIdRef.current
const hasNewReply = replies.length > previousReplyCountRef.current
const hasNewReply = previousReplyCountRef.current !== undefined
&& replies.length > previousReplyCountRef.current
if (isNewComment || hasNewReply) {
// Scroll on first render, new comment, or new reply
if (isFirstRender || isNewComment || hasNewReply) {
container.scrollTo({
top: container.scrollHeight,
behavior: 'smooth',