|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * as React from "react"; |
| 4 | + |
| 5 | +interface SiteCommentsProps { |
| 6 | + docId: string; |
| 7 | + user?: { name?: string | null; image?: string | null } | null; |
| 8 | +} |
| 9 | + |
| 10 | +type Comment = { |
| 11 | + id: number; |
| 12 | + doc_id: string; |
| 13 | + content: string; |
| 14 | + user_id: string; |
| 15 | + user_name: string | null; |
| 16 | + user_image: string | null; |
| 17 | + parent_id: number | null; |
| 18 | + created_at: string; |
| 19 | +}; |
| 20 | + |
| 21 | +export default function SiteComments({ docId, user }: SiteCommentsProps) { |
| 22 | + const [comments, setComments] = React.useState<Comment[]>([]); |
| 23 | + const [loading, setLoading] = React.useState(true); |
| 24 | + const [content, setContent] = React.useState(""); |
| 25 | + const [submitting, setSubmitting] = React.useState(false); |
| 26 | + const [error, setError] = React.useState<string | null>(null); |
| 27 | + |
| 28 | + const fetchComments = React.useCallback(async () => { |
| 29 | + setLoading(true); |
| 30 | + setError(null); |
| 31 | + try { |
| 32 | + const res = await fetch( |
| 33 | + `/api/comments?docId=${encodeURIComponent(docId)}`, |
| 34 | + { |
| 35 | + cache: "no-store", |
| 36 | + }, |
| 37 | + ); |
| 38 | + const data = await res.json(); |
| 39 | + if (!data.ok) throw new Error(data.error || "加载失败"); |
| 40 | + setComments(data.data as Comment[]); |
| 41 | + } catch (e: any) { |
| 42 | + setError(e?.message || "加载失败"); |
| 43 | + } finally { |
| 44 | + setLoading(false); |
| 45 | + } |
| 46 | + }, [docId]); |
| 47 | + |
| 48 | + React.useEffect(() => { |
| 49 | + fetchComments(); |
| 50 | + }, [fetchComments]); |
| 51 | + |
| 52 | + async function onSubmit(e: React.FormEvent) { |
| 53 | + e.preventDefault(); |
| 54 | + const text = content.trim(); |
| 55 | + if (!text) return; |
| 56 | + setSubmitting(true); |
| 57 | + setError(null); |
| 58 | + try { |
| 59 | + const res = await fetch(`/api/comments`, { |
| 60 | + method: "POST", |
| 61 | + headers: { "Content-Type": "application/json" }, |
| 62 | + body: JSON.stringify({ docId, content: text }), |
| 63 | + }); |
| 64 | + const data = await res.json(); |
| 65 | + if (!res.ok || !data.ok) throw new Error(data.error || "提交失败"); |
| 66 | + setContent(""); |
| 67 | + // 直接插入到顶部 |
| 68 | + setComments((prev) => [data.data as Comment, ...prev]); |
| 69 | + } catch (e: any) { |
| 70 | + setError(e?.message || "提交失败"); |
| 71 | + } finally { |
| 72 | + setSubmitting(false); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="space-y-4"> |
| 78 | + <form onSubmit={onSubmit} className="space-y-3"> |
| 79 | + <div className="flex items-center gap-3"> |
| 80 | + {user?.image ? ( |
| 81 | + // eslint-disable-next-line @next/next/no-img-element |
| 82 | + <img |
| 83 | + src={user.image} |
| 84 | + alt="avatar" |
| 85 | + className="h-8 w-8 rounded-full" |
| 86 | + /> |
| 87 | + ) : ( |
| 88 | + <div className="h-8 w-8 rounded-full bg-muted" /> |
| 89 | + )} |
| 90 | + <span className="text-sm text-muted-foreground"> |
| 91 | + {user?.name || "已登录用户"} |
| 92 | + </span> |
| 93 | + </div> |
| 94 | + <textarea |
| 95 | + className="w-full min-h-[100px] rounded-md border bg-transparent p-3" |
| 96 | + placeholder="发表你的看法……" |
| 97 | + value={content} |
| 98 | + onChange={(e) => setContent(e.target.value)} |
| 99 | + maxLength={4000} |
| 100 | + /> |
| 101 | + <div className="flex items-center justify-between"> |
| 102 | + <span className="text-xs text-muted-foreground"> |
| 103 | + {content.trim().length}/4000 |
| 104 | + </span> |
| 105 | + <button |
| 106 | + type="submit" |
| 107 | + className="inline-flex items-center rounded-md border px-3 py-2 text-sm hover:bg-accent disabled:opacity-60" |
| 108 | + disabled={!content.trim() || submitting} |
| 109 | + > |
| 110 | + {submitting ? "提交中…" : "发表评论"} |
| 111 | + </button> |
| 112 | + </div> |
| 113 | + {error && <div className="text-sm text-red-500">{error}</div>} |
| 114 | + </form> |
| 115 | + |
| 116 | + <div className="divide-y divide-border rounded-md border"> |
| 117 | + {loading ? ( |
| 118 | + <div className="p-4 text-sm text-muted-foreground">加载中…</div> |
| 119 | + ) : comments.length === 0 ? ( |
| 120 | + <div className="p-4 text-sm text-muted-foreground"> |
| 121 | + 还没有评论,来当第一个吧。 |
| 122 | + </div> |
| 123 | + ) : ( |
| 124 | + comments.map((c) => ( |
| 125 | + <div key={c.id} className="p-4"> |
| 126 | + <div className="mb-2 flex items-center gap-2"> |
| 127 | + {c.user_image ? ( |
| 128 | + // eslint-disable-next-line @next/next/no-img-element |
| 129 | + <img |
| 130 | + src={c.user_image} |
| 131 | + alt="avatar" |
| 132 | + className="h-6 w-6 rounded-full" |
| 133 | + /> |
| 134 | + ) : ( |
| 135 | + <div className="h-6 w-6 rounded-full bg-muted" /> |
| 136 | + )} |
| 137 | + <span className="text-sm font-medium"> |
| 138 | + {c.user_name || "匿名"} |
| 139 | + </span> |
| 140 | + <span className="text-xs text-muted-foreground"> |
| 141 | + {new Date(c.created_at).toLocaleString()} |
| 142 | + </span> |
| 143 | + </div> |
| 144 | + <div className="whitespace-pre-wrap text-sm leading-relaxed"> |
| 145 | + {c.content} |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + )) |
| 149 | + )} |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + ); |
| 153 | +} |
0 commit comments