import { useEffect, useState } from 'react' import { useParams, useNavigate } from 'react-router-dom' import { NavBar } from 'antd-mobile' import { api } from '../../api/client.js' // 新闻详情: /news/:id // 正文来自 News.content (MCP 返回的完整正文), 旧数据可能无 content, 兜底显示摘要 function fmtDate(s) { if (!s) return '' const d = new Date(s) if (isNaN(d.getTime())) return s const pad = (n) => String(n).padStart(2, '0') return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) } export default function NewsDetail() { const { id } = useParams() const navigate = useNavigate() const [item, setItem] = useState(null) const [err, setErr] = useState('') const [loading, setLoading] = useState(true) useEffect(() => { let cancelled = false setLoading(true) api('/news/' + encodeURIComponent(id)) .then((data) => { if (!cancelled) setItem(data.item) }) .catch((e) => !cancelled && setErr(e.message)) .finally(() => !cancelled && setLoading(false)) return () => { cancelled = true } }, [id]) return (
navigate(-1)}>新闻详情 {loading &&
加载中...
} {err &&
{err}
} {!loading && !err && !item &&
新闻不存在
} {!loading && !err && item && (

{item.title}

{item.source || '未知来源'} · {fmtDate(item.publishedAt)} · #{item.impactRank} {/^https?:\/\//i.test(item.url || '') && ( 原文链接 )}
{item.content ? item.content : item.summary} {!item.content &&
(本条暂无全文,仅摘要)
}
)}
) }