HeaderBar.tsx
5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { type ReactNode } from 'react';
import { Button, Tooltip } from 'antd';
import { BrandMark } from '@hm/ui';
import { useHeatmapStore } from '@/store/useHeatmapStore';
import { useFullscreen } from '@/layout/useFullscreen';
import { ImportControl } from '@/layout/ImportControl';
import { fmtCompact, fmtDateTime, fmtInt, spanDays } from '@/utils/format';
import {
AppstoreOutlined,
CalendarOutlined,
DatabaseOutlined,
FullscreenExitOutlined,
FullscreenOutlined,
} from '@ant-design/icons';
function Pill({
icon,
label,
value,
tip,
}: {
icon: ReactNode;
label: string;
value: string;
/** 悬浮说明:这个数字是从哪儿来的、精确到个位是多少 */
tip?: ReactNode;
}) {
const body = (
<div className="hm-map__stat-chip" style={{ borderRadius: 8 }}>
<span style={{ color: 'var(--hm-primary)', display: 'inline-flex' }}>{icon}</span>
{label}
<b>{value}</b>
</div>
);
// 压缩过的数字(4.1 万)必然对不上精确值,所以精确值与来源都写在悬浮里:
// 拿顶栏的 4.3 万去对导入表格的行数,本来就对不上(表里有被拒收的行),不说清就成了「数据丢了」
return tip ? <Tooltip title={tip}>{body}</Tooltip> : body;
}
/** 导入时间那串 ISO 字符折成北京时间;缺字段或写坏时宁可不说,也不给一个「NaN」 */
function fmtImportedAt(iso: string): string | null {
const ms = Date.parse(iso);
return Number.isFinite(ms) ? fmtDateTime(Math.floor(ms / 1000)) : null;
}
/** 顶栏:品牌 + 项目语境 + 数据状态概览。纯展示,不持有状态 */
export function HeaderBar() {
const meta = useHeatmapStore((s) => s.meta);
const status = useHeatmapStore((s) => s.status);
const dataOrigin = useHeatmapStore((s) => s.dataOrigin);
const importInfo = useHeatmapStore((s) => s.importInfo);
const coverage = meta ? spanDays(meta.timeRange.min, meta.timeRange.max) : 0;
const { active: isFs, toggle: toggleFs } = useFullscreen();
/**
* 数字是从哪份数据来的,写在悬浮里。
*
* 顶栏只有两格宽度,放不下「导入 43,182 条 · 来自 xxx.xlsx」这一长句,
* 于是同一个位置在「导入」和「样本」之间换过来源时,看起来就像同一个数在跳。
* 这里不改变显示的数字,只把来源、精确条数与口径差别说清 —— 悬浮一次就能自己核对。
*/
const sourceTip = (() => {
if (!meta) return null;
const exact = `有效 ${fmtInt(meta.count)} 条`;
const note = '顶栏按「万」压缩,与表格行数、页面别处的命中数不必逐位相同';
if (dataOrigin === 'imported') {
const when = importInfo ? fmtImportedAt(importInfo.importedAt) : null;
return [
`导入数据${importInfo ? `:${importInfo.fileName}` : ''}`,
exact,
importInfo && importInfo.rejected > 0 ? `另拒收 ${fmtInt(importInfo.rejected)} 行` : '',
when ? `导入于 ${when}` : '',
note,
]
.filter(Boolean)
.join(' · ');
}
// 未导入:项目不内置数据,非导入态即空态(请走顶栏导入)
if (!meta || status === 'empty') {
return ['尚未导入数据', '请通过顶栏「导入数据」选择本地 xlsx / csv 导入', note]
.filter(Boolean)
.join(' · ');
}
return ['本地 data:build 产物(public/data)', exact, note].filter(Boolean).join(' · ');
})();
const coverageTip = meta
? [
`报案时间跨度 ${fmtDateTime(meta.timeRange.min)} — ${fmtDateTime(meta.timeRange.max)}`,
'按北京时间自然日计天数',
].join(' · ')
: null;
const statusText =
status === 'ready'
? '数据已就绪'
: status === 'loading'
? '数据加载中'
: status === 'empty'
? '无数据'
: status === 'error'
? '加载失败'
: '等待初始化';
return (
<header className="hm-header">
<div className="hm-header__left">
<BrandMark />
<div className="hm-header__divider" />
<div className="hm-header__context">
<div className="hm-header__context-title">深圳车险出险位置-9月风险热力图</div>
<div className="hm-header__context-sub">智能网联监管平台 · 事故出险点位分析</div>
</div>
</div>
<div className="hm-header__right">
<Pill
icon={<DatabaseOutlined />}
label={dataOrigin === 'imported' ? '导入' : '样本'}
value={meta ? fmtCompact(meta.count) : '—'}
tip={sourceTip}
/>
<Pill
icon={<CalendarOutlined />}
label="覆盖"
value={coverage ? `${coverage}天` : '—'}
tip={coverageTip}
/>
<Pill icon={<AppstoreOutlined />} label="状态" value={statusText} />
<ImportControl />
<Tooltip title={isFs ? '退出全屏 (F / Esc)' : '一键全屏 (F)'}>
<Button
type="text"
className="hm-header__icon-btn"
icon={isFs ? <FullscreenExitOutlined /> : <FullscreenOutlined />}
onClick={toggleFs}
/>
</Tooltip>
</div>
</header>
);
}