import useSWR from "swr"; import { BlogCommentTool } from "./BlogCommentTool"; import { BlogApi } from "@/lib/api"; import { BlogComment } from "@/lib/types/blogComment"; import { useState } from "react"; export function BlogComments({ blogId }: { blogId: string }) { const { data, isLoading, error, mutate } = useSWR( `/api/blog/${blogId}/comments`, () => BlogApi.getComments(blogId), ) 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)} />
{ data.filter(d => !d.parentId) .map((d, dIndex) => (

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

{d.content}

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

未知

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()}

未知

)) }
) }
)) }
) }