Skip to content

Commit 52cc74d

Browse files
committed
fix: incorrectly post a sub comment when posting a main comment
1 parent 997f8fd commit 52cc74d

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/components/viewer/comment/CommentArea.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ export const CommentArea = withSuspensable(function ViewerComments({
6161

6262
// clear replyTo if it's not in comments
6363
useEffect(() => {
64-
if (
65-
replyTo &&
66-
!traverseComments(comments, (c) => c.commentId === replyTo.commentId)
67-
) {
64+
if (replyTo && !traverseComments(comments, (c) => c === replyTo)) {
6865
setReplyTo(undefined)
6966
}
7067
}, [replyTo, comments])
@@ -82,7 +79,7 @@ export const CommentArea = withSuspensable(function ViewerComments({
8279
return (
8380
<CommentAreaContext.Provider value={contextValue}>
8481
<div>
85-
<CommentForm className="mb-6" />
82+
<CommentForm primary className="mb-6" />
8683
{comments.map((comment) => (
8784
<MainComment
8885
key={comment.commentId}
@@ -291,7 +288,6 @@ const CommentActions = ({
291288
</div>
292289
{replyTo === comment && (
293290
<CommentForm
294-
embedded
295291
inputAutoFocus
296292
className="mt-4"
297293
placeholder={`回复 @${comment.uploader}:`}

src/components/viewer/comment/CommentForm.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import { CommentAreaContext } from './CommentArea'
1212

1313
export interface CommentFormProps {
1414
className?: string
15-
embedded?: boolean
15+
primary?: boolean
1616
placeholder?: string
1717
inputAutoFocus?: boolean
1818
}
1919

2020
export const CommentForm = ({
2121
className,
22-
embedded,
22+
primary,
2323
placeholder = '发表一条评论吧',
2424
inputAutoFocus,
2525
}: CommentFormProps) => {
@@ -46,7 +46,17 @@ export const CommentForm = ({
4646
try {
4747
await wrapErrorMessage(
4848
(e) => '发表评论失败:' + formatError(e),
49-
requestAddComment(message, operationId, replyTo?.commentId),
49+
(async () => {
50+
if (primary) {
51+
// this comment is a main comment and does not reply to others
52+
await requestAddComment(message, operationId)
53+
} else {
54+
if (!replyTo) {
55+
throw new Error('要回复的评论不存在')
56+
}
57+
await requestAddComment(message, operationId, replyTo?.commentId)
58+
}
59+
})(),
5060
)
5161

5262
setMessage('')
@@ -78,7 +88,7 @@ export const CommentForm = ({
7888
loading={isSubmitting}
7989
onClick={handleSubmit}
8090
>
81-
{embedded ? '回复' : '发表评论'}
91+
{primary ? '发表评论' : '回复'}
8292
</Button>
8393

8494
<div className="ml-auto text-slate-500 text-sm">

0 commit comments

Comments
 (0)