PointListDrawer.tsx
11.9 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { useMemo } from 'react';
import { Button, Empty, Table, Tooltip, type TableColumnsType } from 'antd';
import { GlassDrawer } from '@/features/heatmap/components/GlassDrawer';
import { EnvironmentOutlined, QuestionCircleOutlined } from '@ant-design/icons';
import { CollapseButton, DrawerTitle } from '@hm/ui';
import { useHeatmapStore } from '@/store/useHeatmapStore';
import { useDrawerCompact } from '@/layout/drawerWidth';
import type { PointRosterRow } from '@/features/heatmap/aggregate/types';
import { fmtCompact, fmtInt } from '@/utils/format';
/**
* 一格的身份:格号。
*
* 用格号而不是坐标:同一个格子在不同缩放级别下算出来的中心点会差一点点,
* 而「刚才点的是哪一行」必须是个稳定的键(见 store 的 CasePin.key)。
*/
const gridKeyOf = (r: PointRosterRow): string => `grid:${r.gx}:${r.gy}`;
/**
* 点位清单抽屉:屏幕上那批热力方格,一行一格。
*
* 从「热力点位数」那个读数点进来。这里列的不是一条条案件,而是地图把屏幕划成的那些格子:
* 一行的案件数就是屏幕上那个方格里写的数字。之所以不给案件清单 —— 点位数说的是格子数,
* 端上一份案件清单就成了「点进来 240 处、列出来 8 万起」,两个数不是一回事,读起来像数错了。
*
* 数据是点开那一刻的聚合快照(见 aggregate/viewSnapshot.ts),不向 Worker 另开查询:
* 现算的第二遍用的还是同一批格子,而格子边长本身随缩放级别变,
* 两次查询之间缩放一次,清单就会列出屏幕上根本没有的格子。
*
* 这里没有筛选条:这四个筛钮(区 / 街道 / 时间 / 关键词)都是「按记录归集」的维度,
* 而一张方格可以同时跨着好几个区、几段时间,筛子没有能落的地方。
* 要看某一格里的案件,点那一行 —— 走的正是案件清单那条路(按这一格的经纬度范围取)。
*/
export function PointListDrawer() {
const roster = useHeatmapStore((s) => s.roster);
const closeRoster = useHeatmapStore((s) => s.closeRoster);
const openRoster = useHeatmapStore((s) => s.openRoster);
const focusCasePoint = useHeatmapStore((s) => s.focusCasePoint);
const view = roster?.kind === 'points' ? roster : null;
const points = view?.points;
const rows = points?.rows ?? [];
// 条形只在这一批格子之间归一(最多的一格 = 满格):和面板两张榜同一口径
const maxCount = rows.length ? rows[0].count : 0;
const listedCases = rows.reduce((sum, r) => sum + r.count, 0);
/**
* 抽屉顶上那句「怎么用这份清单」原是常驻横幅(见下面被删掉的那块):读过一次就够的解释,
* 却把表体往下压了一行。收进右上角那颗「?」(hover/focus 才展开),不占阅读流。
* 内容是动态的(缩放级别、格宽、地面米数、屏外预备格),所以这里先拼好再喂给 Tooltip。
*/
const listHelp = points
? `每一行是地图上画着的一个方格,「案件数」就是那个方格里写的数字。方格按屏幕大小划分:当前是第 ${points.zoom} 级缩放、每格 ${points.gridPx} 像素(地面上约 ${fmtInt(points.cellMeters)} 米),所以这份清单跟着缩放级别走。点一行可以看那个方格里的案件;行末那颗定位标记把地图挪到这一格的中心,清单不关。${points.outside > 0 ? `另有 ${fmtInt(points.outside)} 个方格画在屏幕之外的预备区里,不列在这里。` : ''}`
: '点位清单:屏幕上每个热力方格一行,点一行可以看那一格里的案件。';
/**
* 表头 memo 一次:条形那一列要按「这批格子里最多的一格」归一,所以它只跟 maxCount 有关。
* 放在 render 里就是每次重渲染换一个新引用,Table 收到新引用会把整页重画一遍。
*
* 抽屉宽度跟着视口变(见 src/layout/drawerWidth.ts),窄到 392 那一档时五列放不下,
* 先收的是「与最多的一格相比」那条条形 —— 名次已经说了先后,案件数说了多少,
* 条形在这里只是把这两个数再画一遍,是五列里唯一不带着新信息的。
* 悬浮说明与点进去的案件清单都不受影响。
*/
const compact = useDrawerCompact();
/**
* 被钉到地图上的那一格。
*
* 一行是一个方格,它的身份就是格号(见 gridKeyOf)——
* 坐标在不同缩放级别下会算得差一点点,拿来当键就会「点了第一行却标到第二行」。
*/
const locatedKey = useHeatmapStore((s) => s.casePin?.key ?? null);
const rowClass = useMemo(
() => (r: PointRosterRow) =>
['hm-row--link', locatedKey && gridKeyOf(r) === locatedKey ? 'hm-row--located' : '']
.filter(Boolean)
.join(' '),
[locatedKey],
);
const columns = useMemo<TableColumnsType<PointRosterRow>>(
() => [
{
title: '名次',
dataIndex: 'seq',
// 46:12px 两个字 28 + 左右各 8 内边距;40 时表头竖排成「名/次」
width: 46,
align: 'right',
render: (v: number) => (
<span style={{ color: 'var(--hm-text-3)', fontVariantNumeric: 'tabular-nums' }}>{v}</span>
),
},
{
title: '案件数',
dataIndex: 'count',
width: compact ? 64 : 84,
align: 'right',
render: (v: number) => (
<span style={{ color: 'var(--hm-primary)', fontVariantNumeric: 'tabular-nums' }}>
{fmtCompact(v)}
</span>
),
},
...(!compact
? [
{
title: '与最多的一格相比',
dataIndex: 'count',
key: 'bar',
width: 110,
render: (v: number) => (
<div className="hm-board__bar">
<div
style={{
width: `${maxCount ? Math.max((v / maxCount) * 100, 1.5) : 0}%`,
// 整表一种墨色:长短是唯一的编码,颜色在这里没有第二层含义
background: 'var(--hm-primary)',
}}
/>
</div>
),
},
]
: []),
// 屏幕位置只在宽屏留:紧凑档(正文 ~344)加上定位列就挤不下了,
// 而它说的是「这一格在屏幕上第几列」,定位按钮把图挪过去之后这个数就不再需要
...(!compact
? [
{
title: '屏幕位置',
key: 'pos',
width: 96,
render: (_v: unknown, r: PointRosterRow) => (
<span style={{ color: 'var(--hm-text-2)', fontSize: 11 }}>
第 {r.col} 列 第 {r.row} 行
</span>
),
},
]
: []),
{
title: '中心坐标',
key: 'coord',
// 小数第五位在 11px 下约 6.2 像素一位,两个数一共 11 位数字加逗号空格 —— 给到 130 才不断行
width: compact ? 130 : undefined,
render: (_v: unknown, r: PointRosterRow) => (
<span
style={{
fontSize: 11,
color: 'var(--hm-text-2)',
fontVariantNumeric: 'tabular-nums',
whiteSpace: 'nowrap',
}}
title="方格中心:这一格里所有案件经纬度的平均位置"
>
{r.lng.toFixed(5)}, {r.lat.toFixed(5)}
</span>
),
},
/**
* 定位到这一格的中心。
*
* 与另外两张案件名单同一颗图标、同一列位置:一行是一个方格而不是一条案件,
* 所以地图上钉的是它的中心,牌子写「第几列第几行 · 多少起」——
* 这里没有一个门牌地址可写,写坐标又念不出来。
*
* 这一列的点击必须 stopPropagation:整行点击是「看这一格里的案件」(下面 onRow),
* 而定位只是把图挪过去,两件事不该一次点触发。
*/
{
title: '定位',
key: 'locate',
width: compact ? 44 : 48,
align: 'center',
render: (_v: unknown, r: PointRosterRow) => (
<Button
type="text"
size="small"
className="hm-locate"
icon={<EnvironmentOutlined />}
title="在地图上定位这一格的中心(清单保持打开)"
aria-label={`定位第 ${r.col} 列第 ${r.row} 行`}
onClick={(e) => {
e.stopPropagation();
focusCasePoint({
lng: r.lng,
lat: r.lat,
address: `热力方格 第 ${r.col} 列 第 ${r.row} 行 · ${fmtInt(r.count)} 起`,
key: gridKeyOf(r),
});
}}
/>
),
},
],
[maxCount, compact, focusCasePoint],
);
return (
<GlassDrawer
title={
<DrawerTitle
title={view?.source ?? '点位清单'}
note={points ? `共 ${fmtInt(rows.length)} 处` : undefined}
/>
}
extra={
<>
<Tooltip
title={listHelp}
placement="bottomRight"
trigger={['hover', 'focus']}
styles={{ root: { maxWidth: 340 }, container: { fontSize: 12, lineHeight: 1.7 } }}
>
<button type="button" className="hm-list__help" aria-label="清单用法说明">
<QuestionCircleOutlined />
</button>
</Tooltip>
<CollapseButton
onClick={closeRoster}
dir="right"
hint="收起这张清单,屏幕立刻整块留给地图;再点一次「热力点位数」那个读数就回来"
/>
</>
}
// 这份的每一行都指着地图上的一个方格(公共壳已统一不带遮罩):要的就是「看着图对清单」,
// 那层深色一压,行末定位标记点下去也看不分明。
open={view !== null}
onClose={closeRoster}
>
{rows.length === 0 ? (
<Empty description="屏幕上没有方格" />
) : (
<Table<PointRosterRow>
size="small"
rowKey={(r) => `${r.gx}:${r.gy}`}
// 整批格子已经在这份快照里,分页交给前端:翻一页不该再等一次取数
pagination={{
pageSize: 20,
size: 'small',
showSizeChanger: false,
showTotal: (t, range) =>
range[1] > range[0]
? `第 ${range[0]}–${range[1]} 处 / 共 ${fmtInt(t)} 处`
: `共 ${fmtInt(t)} 处`,
}}
// 点一行 = 看这一格里的那些案件。两张清单共用右缘那一个抽屉位,
// store 的 openRoster 会顺手把这张点位清单收回
onRow={(r) => ({
onClick: () =>
openRoster({
source: `热力方格 · 第 ${r.col} 列 第 ${r.row} 行`,
// 带上网格号与格边长,让 Worker 用分桶那一条式子判定成员 ——
// 换成 bbox 反算,边界上的点会多算/少算一格(见 RosterWhere.cell)
where: {
cell: {
gx: r.gx,
gy: r.gy,
dLng: points?.dLng ?? 0,
dLat: points?.dLat ?? 0,
},
},
}),
})}
rowClassName={rowClass}
dataSource={rows}
columns={columns}
/>
)}
{points && rows.length > 0 && (
<div className="hm-roster-foot">
<span>
列出的 {fmtInt(rows.length)} 处合计 {fmtInt(listedCases)} 起;名次只按案件数排,
一行就是一个方格,不是一个地点
</span>
</div>
)}
</GlassDrawer>
);
}