refactor: 移除组件中的用户属性,改为使用上下文中的用户信息
All checks were successful
Push to Helm Chart Repository / build (push) Successful in 9s

This commit is contained in:
2025-09-18 23:33:08 +08:00
parent a2ce70278e
commit 1c20598126
4 changed files with 9 additions and 16 deletions

View File

@ -10,11 +10,11 @@ import { Label } from "@/components/ui/label";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { getGravatarUrl } from "@/utils/common/gravatar"; import { getGravatarUrl } from "@/utils/common/gravatar";
import { getFirstCharFromUser } from "@/utils/common/username"; import { getFirstCharFromUser } from "@/utils/common/username";
import { useAuth } from "@/contexts/auth-context";
export function CommentInput( export function CommentInput(
{ {
user,
onCommentSubmitted, onCommentSubmitted,
initContent = "", initContent = "",
initIsPrivate = false, initIsPrivate = false,
@ -22,7 +22,6 @@ export function CommentInput(
isUpdate = false, isUpdate = false,
initShowClientInfo = true initShowClientInfo = true
}: { }: {
user: User | null,
onCommentSubmitted: ({ commentContent, isPrivate, showClientInfo }: { commentContent: string, isPrivate: boolean, showClientInfo: boolean }) => void, onCommentSubmitted: ({ commentContent, isPrivate, showClientInfo }: { commentContent: string, isPrivate: boolean, showClientInfo: boolean }) => void,
initContent?: string, initContent?: string,
initIsPrivate?: boolean, initIsPrivate?: boolean,
@ -31,6 +30,7 @@ export function CommentInput(
initShowClientInfo?: boolean initShowClientInfo?: boolean
} }
) { ) {
const {user} = useAuth();
const t = useTranslations('Comment') const t = useTranslations('Comment')
const commonT = useTranslations('Common') const commonT = useTranslations('Common')
const clickToLogin = useToLogin() const clickToLogin = useToLogin()

View File

@ -15,11 +15,11 @@ import { formatDateTime } from "@/utils/common/datetime";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { getGravatarUrl } from "@/utils/common/gravatar"; import { getGravatarUrl } from "@/utils/common/gravatar";
import { getFirstCharFromUser } from "@/utils/common/username"; import { getFirstCharFromUser } from "@/utils/common/username";
import { useAuth } from "@/contexts/auth-context";
export function CommentItem( export function CommentItem(
{ {
loginUser,
comment, comment,
parentComment, parentComment,
onCommentDelete, onCommentDelete,
@ -27,7 +27,6 @@ export function CommentItem(
setActiveInputId, setActiveInputId,
onReplySubmitted // 评论区计数更新用 onReplySubmitted // 评论区计数更新用
}: { }: {
loginUser: User | null,
comment: Comment, comment: Comment,
parentComment: Comment | null, parentComment: Comment | null,
onCommentDelete: ({ commentId }: { commentId: number }) => void, onCommentDelete: ({ commentId }: { commentId: number }) => void,
@ -36,6 +35,7 @@ export function CommentItem(
onReplySubmitted: ({ commentContent, isPrivate }: { commentContent: string, isPrivate: boolean }) => void, onReplySubmitted: ({ commentContent, isPrivate }: { commentContent: string, isPrivate: boolean }) => void,
} }
) { ) {
const {user} = useAuth();
const locale = useLocale(); const locale = useLocale();
const t = useTranslations("Comment"); const t = useTranslations("Comment");
const commonT = useTranslations("Common"); const commonT = useTranslations("Common");
@ -57,7 +57,7 @@ export function CommentItem(
return; return;
} }
setCanClickLike(false); setCanClickLike(false);
if (!loginUser) { if (!user) {
toast.error(t("login_required"), { toast.error(t("login_required"), {
action: { action: {
label: commonT("login"), label: commonT("login"),
@ -236,7 +236,7 @@ export function CommentItem(
</button> </button>
{/* 编辑和删除按钮 仅自己的评论可见 */} {/* 编辑和删除按钮 仅自己的评论可见 */}
{loginUser?.id === commentState.user.id && ( {user?.id === commentState.user.id && (
<> <>
<button <button
title={t("edit")} title={t("edit")}
@ -276,13 +276,11 @@ export function CommentItem(
</div> </div>
{/* 这俩输入框一次只能显示一个 */} {/* 这俩输入框一次只能显示一个 */}
{activeInput && activeInput.type === 'reply' && activeInput.id === commentState.id && <CommentInput {activeInput && activeInput.type === 'reply' && activeInput.id === commentState.id && <CommentInput
user={loginUser}
onCommentSubmitted={onReply} onCommentSubmitted={onReply}
initIsPrivate={commentState.isPrivate} initIsPrivate={commentState.isPrivate}
placeholder={`${t("reply")} ${commentState.user.nickname || commentState.user.username} :`} placeholder={`${t("reply")} ${commentState.user.nickname || commentState.user.username} :`}
/>} />}
{activeInput && activeInput.type === 'edit' && activeInput.id === commentState.id && <CommentInput {activeInput && activeInput.type === 'edit' && activeInput.id === commentState.id && <CommentInput
user={loginUser}
initContent={commentState.content} initContent={commentState.content}
initIsPrivate={commentState.isPrivate} initIsPrivate={commentState.isPrivate}
onCommentSubmitted={onCommentEdit} onCommentSubmitted={onCommentEdit}
@ -297,7 +295,6 @@ export function CommentItem(
{replies.map((reply) => ( {replies.map((reply) => (
<CommentItem <CommentItem
key={reply.id} key={reply.id}
loginUser={loginUser}
comment={reply} comment={reply}
parentComment={commentState} parentComment={commentState}
onCommentDelete={onReplyDelete} onCommentDelete={onReplyDelete}

View File

@ -109,7 +109,6 @@ export function CommentSection(
<Separator className="my-16" /> <Separator className="my-16" />
<div className="font-bold text-2xl">{t("comment")} ({totalCommentCount})</div> <div className="font-bold text-2xl">{t("comment")} ({totalCommentCount})</div>
<CommentInput <CommentInput
user={user}
onCommentSubmitted={onCommentSubmitted} onCommentSubmitted={onCommentSubmitted}
/> />
<div className="mt-4"> <div className="mt-4">
@ -118,7 +117,6 @@ export function CommentSection(
<div key={comment.id} className="" style={{ animationDelay: `${idx * 60}ms` }}> <div key={comment.id} className="" style={{ animationDelay: `${idx * 60}ms` }}>
<Separator className="my-2" /> <Separator className="my-2" />
<CommentItem <CommentItem
loginUser={user}
comment={comment} comment={comment}
parentComment={null} parentComment={null}
onCommentDelete={onCommentDelete} onCommentDelete={onCommentDelete}

View File

@ -31,13 +31,11 @@ import {
import { User } from "@/models/user" import { User } from "@/models/user"
import { getGravatarFromUser } from "@/utils/common/gravatar" import { getGravatarFromUser } from "@/utils/common/gravatar"
import { getFallbackAvatarFromUsername } from "@/utils/common/username" import { getFallbackAvatarFromUsername } from "@/utils/common/username"
import { useAuth } from "@/contexts/auth-context"
export function NavUser({ export function NavUser({}: {}) {
user,
}: {
user?: User
}) {
const { isMobile } = useSidebar() const { isMobile } = useSidebar()
const {user} = useAuth();
if (!user) return null if (!user) return null
return ( return (
<SidebarMenu> <SidebarMenu>