Skip to content

Commit 69aedb2

Browse files
authored
Merge pull request #129 from likelion-ssu/fix/comment-scroll
Fix: 댓글 연속 작성 시 스크롤 동작 개선
2 parents d8390e7 + 46191b6 commit 69aedb2

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

src/app/_component/domain/readArticle/Comment.tsx

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ const Comment = ({ articleId, studyroomId }: Props) => {
5252
const [comments, setComments] = useState<CommentData[]>([]);
5353
const [selectedFile, setSelectedFile] = useState<File | null>(null);
5454
const [isLoading, setIsLoading] = useState(true);
55+
const [lastCommentId, setLastCommentId] = useState<string | null>(null);
5556

5657
const { name, year, uid } = useUserStore();
5758
const showToast = useToastStore(state => state.showToast);
5859

5960
const fileInputRef = useRef<HTMLInputElement>(null);
6061
const commentsAreaRef = useRef<HTMLDivElement>(null);
61-
const prevCommentsLengthRef = useRef(0);
6262

6363
useEffect(() => {
6464
const commentsRef = collection(
@@ -79,22 +79,25 @@ const Comment = ({ articleId, studyroomId }: Props) => {
7979
const sortedComments = sortArrByTime(result, true);
8080
setComments(sortedComments);
8181
setIsLoading(false);
82+
83+
// 마지막 댓글 ID가 있고, 새로운 댓글이 추가된 경우에만 스크롤
84+
if (lastCommentId && sortedComments.length > 0) {
85+
const lastComment = sortedComments[sortedComments.length - 1];
86+
if (lastComment.commentId === lastCommentId) {
87+
setTimeout(() => {
88+
if (commentsAreaRef.current) {
89+
commentsAreaRef.current.scrollTo({
90+
top: commentsAreaRef.current.scrollHeight,
91+
behavior: "smooth"
92+
});
93+
}
94+
}, 100);
95+
}
96+
}
8297
});
8398

8499
return () => unsub();
85-
}, [articleId, studyroomId]);
86-
87-
// 댓글 추가하면 해당 댓글로 스크롤
88-
useEffect(() => {
89-
if (comments.length > prevCommentsLengthRef.current && commentsAreaRef.current) {
90-
const scrollOptions = {
91-
top: commentsAreaRef.current.scrollHeight,
92-
behavior: "smooth" as const
93-
};
94-
commentsAreaRef.current.scrollTo(scrollOptions);
95-
}
96-
prevCommentsLengthRef.current = comments.length;
97-
}, [comments]);
100+
}, [articleId, studyroomId, lastCommentId]);
98101

99102
const handleSubmit = async () => {
100103
if (!commentText.trim() && !selectedFile) {
@@ -131,13 +134,10 @@ const Comment = ({ articleId, studyroomId }: Props) => {
131134
imageUrl: imageUrl
132135
};
133136

134-
await addDoc(commentsRef, commentData);
137+
const docRef = await addDoc(commentsRef, commentData);
135138
setCommentText("");
136139
setSelectedFile(null);
137-
138-
if (commentsAreaRef.current) {
139-
commentsAreaRef.current.scrollTop = commentsAreaRef.current.scrollHeight;
140-
}
140+
setLastCommentId(docRef.id);
141141
} catch (error) {
142142
console.error("댓글 작성 중 오류 발생:", error);
143143
showToast("fail");
@@ -216,7 +216,7 @@ const Comment = ({ articleId, studyroomId }: Props) => {
216216
</div>
217217
) : comments.length > 0 ? (
218218
<div className={styles.commentsArea} ref={commentsAreaRef}>
219-
{comments.map(comment => (
219+
{comments.map((comment, index) => (
220220
<div className={styles.commentWrapper} key={comment.commentId}>
221221
<Image
222222
className={styles.profileImgContainer}

0 commit comments

Comments
 (0)