Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 添加支持作者评论字数上限为500 #338

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/components/viewer/comment/CommentArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
useComments,
} from '../../../apis/comment'
import {
MAX_COMMENT_LENGTH,
AUTHOR_MAX_COMMENT_LENGTH,
CommentInfo,
CommentRating,
MainCommentInfo,
Expand Down Expand Up @@ -62,11 +64,10 @@ export const CommentArea = withSuspensable(function ViewerComments({

const auth = useAtomValue(authAtom)
const operation = useOperation({ id: operationId }).data
// FIXME: 用户名可以重名,这里会让重名用户都显示置顶按钮,需要等后端支持 operation.uploaderId 后再修复
const operationOwned =
!!operation?.uploader &&
!!auth.username &&
operation.uploader === auth.username

const operationOwned = operation && auth.userId && operation.uploaderId === auth.userId

const maxLength = operationOwned ? AUTHOR_MAX_COMMENT_LENGTH : MAX_COMMENT_LENGTH

const [replyTo, setReplyTo] = useState<CommentInfo>()

Expand Down Expand Up @@ -94,7 +95,7 @@ export const CommentArea = withSuspensable(function ViewerComments({
return (
<CommentAreaContext.Provider value={contextValue}>
<div>
<CommentForm primary className="mb-6" />
<CommentForm primary className="mb-6" maxLength={maxLength} />
{comments?.map((comment) => (
<MainComment
key={comment.commentId}
Expand All @@ -110,8 +111,8 @@ export const CommentArea = withSuspensable(function ViewerComments({
sub.fromCommentId === comment.commentId
? undefined
: find(comment.subCommentsInfos, {
commentId: sub.fromCommentId,
})
commentId: sub.fromCommentId,
})
}
/>
))}
Expand Down Expand Up @@ -261,6 +262,7 @@ const CommentActions = ({
const [{ userId }] = useAtom(authAtom)
const { operationOwned, replyTo, setReplyTo, reload } =
useContext(CommentAreaContext)
const maxLength = operationOwned ? AUTHOR_MAX_COMMENT_LENGTH : MAX_COMMENT_LENGTH

const [deleteDialogOpen, setDeleteDialogOpen] = useState(false)
const [pending, setPending] = useState(false)
Expand Down Expand Up @@ -331,7 +333,7 @@ const CommentActions = ({
</p>
</Alert>
</div>
{replyTo === comment && <CommentForm inputAutoFocus className="mt-4" />}
{replyTo === comment && <CommentForm inputAutoFocus className="mt-4" maxLength={maxLength} />}
</div>
)
}
Expand Down
6 changes: 4 additions & 2 deletions src/components/viewer/comment/CommentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ export interface CommentFormProps {
primary?: boolean
placeholder?: string
inputAutoFocus?: boolean
maxLength?: number
}

export const CommentForm = ({
className,
primary,
placeholder = '发一条友善的评论吧',
inputAutoFocus,
maxLength = MAX_COMMENT_LENGTH,
}: CommentFormProps) => {
const { operationId, replyTo, reload } = useContext(CommentAreaContext)

Expand Down Expand Up @@ -82,7 +84,7 @@ export const CommentForm = ({
rows={2}
growVertically
large
maxLength={MAX_COMMENT_LENGTH}
maxLength={maxLength}
placeholder={placeholder}
value={message}
onChange={(e) => setMessage(e.target.value)}
Expand Down Expand Up @@ -111,7 +113,7 @@ export const CommentForm = ({
</Checkbox>

<div className="ml-auto text-slate-500 text-sm">
{message.length}/{MAX_COMMENT_LENGTH}
{message.length}/{maxLength}
</div>
</div>

Expand Down
1 change: 1 addition & 0 deletions src/models/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const enum CommentRating {
}

export const MAX_COMMENT_LENGTH = 150
export const AUTHOR_MAX_COMMENT_LENGTH = 500

export function isMainComment(
comment: CommentInfo,
Expand Down