Skip to content

Commit c88f834

Browse files
authored
Merge pull request #138 from likelion-ssu/fix/comment-scroll
Fix: 댓글 섹션 스크롤 기본 위치를 맨 아래로 설정 및 댓글 중복 입력 방지
2 parents aedb5d6 + 1efe588 commit c88f834

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const Comment = ({ articleId, studyroomId }: Props) => {
5353
const [selectedFile, setSelectedFile] = useState<File | null>(null);
5454
const [isLoading, setIsLoading] = useState(true);
5555
const [lastCommentId, setLastCommentId] = useState<string | null>(null);
56+
const [isSubmitting, setIsSubmitting] = useState(false);
5657

5758
const { name, year, uid } = useUserStore();
5859
const showToast = useToastStore(state => state.showToast);
@@ -80,31 +81,33 @@ const Comment = ({ articleId, studyroomId }: Props) => {
8081
setComments(sortedComments);
8182
setIsLoading(false);
8283

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-
}
84+
// 초기 로딩이 완료되거나 새로운 댓글이 추가된 경우 스크롤
85+
if (!isLoading || (lastCommentId && sortedComments.length > 0)) {
86+
setTimeout(() => {
87+
if (commentsAreaRef.current) {
88+
commentsAreaRef.current.scrollTo({
89+
top: commentsAreaRef.current.scrollHeight,
90+
behavior: "smooth"
91+
});
92+
}
93+
}, 100);
9694
}
9795
});
9896

9997
return () => unsub();
100-
}, [articleId, studyroomId, lastCommentId]);
98+
}, [articleId, studyroomId, lastCommentId, isLoading]);
10199

102100
const handleSubmit = async () => {
103101
if (!commentText.trim() && !selectedFile) {
104102
return;
105103
}
106104

105+
if (isSubmitting) {
106+
return;
107+
}
108+
107109
try {
110+
setIsSubmitting(true);
108111
let imageUrl = null;
109112

110113
if (selectedFile) {
@@ -115,6 +118,7 @@ const Comment = ({ articleId, studyroomId }: Props) => {
115118
} catch (uploadError) {
116119
console.error("파일 업로드 중 오류 발생:", uploadError);
117120
showToast("fail");
121+
setIsSubmitting(false);
118122
return;
119123
}
120124
}
@@ -141,6 +145,8 @@ const Comment = ({ articleId, studyroomId }: Props) => {
141145
} catch (error) {
142146
console.error("댓글 작성 중 오류 발생:", error);
143147
showToast("fail");
148+
} finally {
149+
setIsSubmitting(false);
144150
}
145151
};
146152

@@ -288,6 +294,7 @@ const Comment = ({ articleId, studyroomId }: Props) => {
288294
value={commentText}
289295
onChange={e => setCommentText(e.target.value)}
290296
style={{ border: "none", width: "100%", paddingRight: "4rem", outline: "none" }}
297+
disabled={isSubmitting}
291298
/>
292299
<div className={styles.buttonGroup}>
293300
<label
@@ -300,6 +307,7 @@ const Comment = ({ articleId, studyroomId }: Props) => {
300307
type="file"
301308
onChange={handleFileChange}
302309
style={{ display: "none" }}
310+
disabled={isSubmitting}
303311
/>
304312
<ICFile />
305313
</label>
@@ -308,7 +316,7 @@ const Comment = ({ articleId, studyroomId }: Props) => {
308316
<button
309317
className={`${styles.submitBtn} ${isSubmitEnabled ? styles.active : ""}`}
310318
onClick={handleSubmit}
311-
disabled={!isSubmitEnabled}
319+
disabled={!isSubmitEnabled || isSubmitting}
312320
type="button"
313321
>
314322
<ICUpload />

0 commit comments

Comments
 (0)