import { useEffect, useState, useCallback } from 'react' import { useParams, useNavigate } from 'react-router-dom' import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts' import { Card, List, NavBar } from 'antd-mobile' import { api } from '../../api/client.js' import TrendBadge, { calcTrend, lastPoint } from '../../components/TrendBadge.jsx' import useRefreshOnVisible from '../../hooks/useRefreshOnVisible.js' // 经济数据 // 列表态 (默认): 列出所有指标, 每行显示最新值 + 趋势徽标, 点击进入详情 // 详情态: 顶部 NavBar (返回) + 折线图 + 数据表 // 频率中文标签 (对应 Indicator.frequency) const FREQ_LABEL = { year: '年', quarter: '季', month: '月', day: '日' } // 统一数据行的时间字段: 兼容 {month}/{year}/{period} 三种存储形状 function periodOf(r) { return (r && (r.period || r.month || r.year)) || '' } export default function DataView() { const [indicators, setIndicators] = useState([]) const [err, setErr] = useState('') const [loading, setLoading] = useState(true) // 路由驱动: /macro/data 列表, /macro/data/:name 详情, 浏览器/手机返回键天然可用 const { name } = useParams() const navigate = useNavigate() const load = useCallback(() => { setLoading(true) api('/indicators') .then((data) => { setErr(''); setIndicators(data.indicators || []) }) .catch((e) => setErr(e.message)) .finally(() => setLoading(false)) }, []) useEffect(() => { load() }, [load]) // 后台回前台时重新拉取 useRefreshOnVisible(load) if (loading) return
| 月份 | 数值 |
|---|---|
| {periodOf(r)} | {r.value} |