import { useEffect, useState } from 'react'; import { Link, useParams } from 'react-router-dom'; import { api } from '../api/client'; import { useAuth } from '../lib/auth'; import { Button } from '../components/ui'; import { IconBack } from '../components/icons'; import type { Thread } from '../types'; export function ThreadPage() { const { id } = useParams(); const { user } = useAuth(); const [thread, setThread] = useState(null); const [body, setBody] = useState(''); const [error, setError] = useState(''); useEffect(() => { if (id) api.thread(Number(id)).then(setThread).catch((e) => setError(String(e))); }, [id]); const reply = async (e: React.FormEvent) => { e.preventDefault(); if (!id) return; setError(''); try { const updated = await api.addReply(Number(id), body); setThread(updated); setBody(''); } catch (err) { setError(err instanceof Error ? err.message : '回复失败'); } }; if (error) return

{error}

; if (!thread) return

加载中…

; const fmt = (s: string) => new Date(s + 'Z').toLocaleString('zh-CN'); return (
返回

{thread.title}

{thread.author_name} {fmt(thread.created_at)}

{thread.body}

{thread.reply_count} 条回复

{thread.replies?.map((r) => (
{r.author_name} {fmt(r.created_at)}

{r.body}

))}
{user ? (