"use client"; import { FormEvent, useEffect, useState } from "react"; import { Link, useLocale } from "@/i18n/navigation"; import { useParams } from "next/navigation"; import Footer from "@/app/components/Footer"; import { fetchAuthMe } from "@/app/lib/api-client"; import { fetchDiscussion, fetchDiscussionReplies, postDiscussionReply, toggleDiscussionLike, toggleDiscussionPin, type DiscussionReply, } from "@/app/lib/social"; export default function DiscussThreadPage() { const params = useParams(); const discussionId = String(params?.id || ""); const locale = useLocale(); const [topic, setTopic] = useState | null>(null); const [replies, setReplies] = useState([]); const [draft, setDraft] = useState(""); const [loading, setLoading] = useState(true); const [posting, setPosting] = useState(false); const [isLoggedIn, setIsLoggedIn] = useState(false); const [currentUserId, setCurrentUserId] = useState(""); const [liking, setLiking] = useState(false); const [pinning, setPinning] = useState(false); const [error, setError] = useState(""); useEffect(() => { fetchAuthMe().then((data) => { const userId = String(data?.user?.id || ""); setIsLoggedIn(Boolean(userId)); setCurrentUserId(userId); }); }, []); useEffect(() => { if (!discussionId) return; let cancelled = false; Promise.all([fetchDiscussion(discussionId), fetchDiscussionReplies(discussionId)]) .then(([topicData, replyItems]) => { if (cancelled) return; setTopic(topicData.item); setReplies(replyItems); }) .catch(() => { if (!cancelled) setError(locale === "zh" ? "加载失败" : "Failed to load"); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [discussionId, locale]); const handleLike = async () => { if (!isLoggedIn || !discussionId) return; setLiking(true); setError(""); try { const result = await toggleDiscussionLike(discussionId); setTopic((current) => current ? { ...current, likes: result.likes, likedByMe: result.liked, } : current ); } catch (err) { setError(err instanceof Error ? err.message : locale === "zh" ? "点赞失败" : "Like failed"); } finally { setLiking(false); } }; const handlePin = async () => { if (!isLoggedIn || !discussionId) return; setPinning(true); setError(""); try { const result = await toggleDiscussionPin(discussionId); setTopic((current) => current ? { ...current, pinned: result.pinned, } : current ); } catch (err) { setError(err instanceof Error ? err.message : locale === "zh" ? "置顶失败" : "Pin failed"); } finally { setPinning(false); } }; const handleSubmit = async (event: FormEvent) => { event.preventDefault(); const body = draft.trim(); if (!body || !discussionId) return; if (!isLoggedIn) { setError(locale === "zh" ? "请先登录后再回复" : "Please log in to reply"); return; } setPosting(true); setError(""); try { const reply = await postDiscussionReply(discussionId, body); setReplies((current) => [...current, reply]); setDraft(""); setTopic((current) => current ? { ...current, replies: Number(current.replies || 0) + 1 } : current ); } catch (err) { setError(err instanceof Error ? err.message : locale === "zh" ? "回复失败" : "Reply failed"); } finally { setPosting(false); } }; if (loading) { return (
); } if (!topic) { return (

{error || (locale === "zh" ? "话题不存在" : "Topic not found")}

{locale === "zh" ? "返回讨论区" : "Back to discussions"}
); } const tags = Array.isArray(topic.tags) ? topic.tags.map(String) : []; const likedByMe = Boolean(topic.likedByMe); const likes = Number(topic.likes || 0); const pinned = Boolean(topic.pinned); const isAuthor = Boolean(currentUserId && String(topic.authorUserId || "") === currentUserId); return (
← {locale === "zh" ? "返回讨论区" : "Back"}
{tags.map((tag) => ( {tag} ))}

{String(topic.title || "")}

{pinned ? ( 📌 {locale === "zh" ? "置顶" : "Pinned"} ) : null}

{String(topic.author || "社区成员")} {topic.city ? ` · ${String(topic.city)}` : ""} {` · 👁 ${Number(topic.views || 0)} · 💬 ${Number(topic.replies || 0)}`}

{isAuthor ? ( ) : null}
{topic.excerpt ? (

{String(topic.excerpt)}

) : null}

{locale === "zh" ? "回复" : "Replies"} ({replies.length})

{replies.length ? ( replies.map((reply) => (
{reply.author || (locale === "zh" ? "成员" : "Member")} {reply.created ? new Date(reply.created).toLocaleString() : ""}

{reply.body}

)) ) : (

{locale === "zh" ? "还没有回复,来抢沙发吧" : "No replies yet"}

)}