"use client" import { clickToUserprofile, useToLogin } from "@/hooks/use-route"; import { User } from "@/models/user"; import { useTranslations } from "next-intl"; import { Suspense, use, useEffect, useState } from "react"; import NeedLogin from "../common/need-login"; import { toast } from "sonner"; import { getGravatarByUser } from "../common/gravatar"; import { CircleUser, Reply, Trash } from "lucide-react"; import { Textarea } from "../ui/textarea"; import { Comment } from "@/models/comment"; import { createComment, deleteComment, listComments } from "@/api/comment"; import { TargetType } from "@/models/types"; import { OrderBy } from "@/models/common"; import { Separator } from "../ui/separator"; import { getLoginUser } from "@/api/user"; import { Skeleton } from "../ui/skeleton"; import { toggleLike } from "@/api/like"; import { useDoubleConfirm } from "@/hooks/use-double-confirm"; import "./style.css"; const DEFAULT_PAGE_SIZE = 20; export function CommentSection( { targetType, targetId }: { targetType: TargetType, targetId: number } ) { const t = useTranslations('Comment') const [currentUser, setCurrentUser] = useState(null); const [comments, setComments] = useState([]); const [refreshCommentsKey, setRefreshCommentsKey] = useState(0); // 获取当前登录用户 useEffect(() => { getLoginUser() .then(response => { setCurrentUser(response.data); }) }, []) // 加载0/顶层评论 useEffect(() => { listComments({ targetType, targetId, depth: 0, orderBy: OrderBy.CreatedAt, desc: true, page: 1, size: DEFAULT_PAGE_SIZE, commentId: 0 }).then(response => { setComments(response.data); }); }, [refreshCommentsKey]) const onCommentSubmitted = (commentContent: string) => { createComment({ targetType, targetId, content: commentContent, replyId: null, isPrivate: false }).then(() => { toast.success(t("comment_success")); setRefreshCommentsKey(k => k + 1); }) } const onCommentDelete = (commentId: number) => { deleteComment(commentId).then(() => { toast.success(t("delete_success")); setRefreshCommentsKey(k => k + 1); }).catch(error => { toast.error(t("delete_failed") + ": " + error.message); }); } return (
{t("comment")}
}> {comments.map((comment, idx) => (
))}
) } export function CommentInput( { user, onCommentSubmitted, }: { user: User | null, onCommentSubmitted: (commentContent: string) => void } ) { const t = useTranslations('Comment') const handleToLogin = useToLogin() const [commentContent, setCommentContent] = useState(""); const handleCommentSubmit = async () => { if (!user) { toast.error({t("login_required")}); return; } if (!commentContent.trim()) { toast.error(t("content_required")); return; } onCommentSubmitted(commentContent); setCommentContent(""); }; return (
{user && getGravatarByUser(user)} {!user && }