'use client'; import useSWR from "swr"; import { BlogCommentTool } from "./BlogCommentTool"; import { BlogComment } from "@/lib/types/blogComment"; import { useState } from "react"; import { BlogAPI } from "@/lib/api/client"; import { useUserStore } from "@/store/useUserStore"; export function BlogComments({ blogId }: { blogId: string }) { const { data, mutate } = useSWR( `/api/blog/${blogId}/comments`, () => BlogAPI.getComments(blogId), ) const { user } = useUserStore(); const insertComment = async (newOne: BlogComment) => { await mutate( (comments) => { if (!comments) return [newOne]; return [newOne, ...comments] }, { revalidate: false } ) } const [replayTarget, setReplayTarget] = useState(null); return ( data &&

评论 {data.length}

setReplayTarget(null)} />
{ user ? (当前账户:{user.nickname}) : (当前未登录,留言名称为匿名,登录可前往控制台) }
{ data.filter(d => !d.parentId) .map((d) => (

{d.user ? d.user.nickname : '匿名'}

{d.content}

{new Date(d.createdAt).toLocaleString()}

{d.address}

setReplayTarget(d)}>回复

{ data.filter(c => c.parentId === d.id).length > 0 && (
{ data.filter(c => c.parentId === d.id).map(c => (

{c.user ? c.user.nickname : '匿名'}

{c.content}

{new Date().toLocaleString()}

{c.address}

)) }
) }
)) }
) }