@sybilion/uilib 1.4.36 → 1.4.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/README.md +2 -0
  2. package/dist/agent-glossary/workspace.generated.ts +1 -0
  3. package/dist/agent-glossary/workspace.index.md +1 -0
  4. package/dist/agent-glossary/workspace.md +1 -0
  5. package/dist/esm/src/components/ui/AppHeader/AppHeader.styl.js +1 -1
  6. package/dist/esm/src/components/ui/Banner/Banner.styl.js +1 -1
  7. package/dist/esm/src/components/ui/Card/Card.styl.js +1 -1
  8. package/dist/esm/src/components/ui/Chart/Chart.js +1 -2
  9. package/dist/esm/src/components/ui/Chip/Chip.js +9 -2
  10. package/dist/esm/src/components/ui/Chip/Chip.styl.js +2 -2
  11. package/dist/esm/src/components/ui/Dialog/Dialog.styl.js +1 -1
  12. package/dist/esm/src/components/ui/Drawer/Drawer.styl.js +1 -1
  13. package/dist/esm/src/components/ui/DropdownMenu/DropdownMenu.styl.js +1 -1
  14. package/dist/esm/src/components/ui/Input/Input.styl.js +1 -1
  15. package/dist/esm/src/components/ui/Logo/Logo.styl.js +1 -1
  16. package/dist/esm/src/components/ui/MobileAdaptiveSelector/MobileAdaptiveSelector.styl.js +1 -1
  17. package/dist/esm/src/components/ui/Page/PageHeader/PageHeader.styl.js +1 -1
  18. package/dist/esm/src/components/ui/Page/PageTabs/PageTabs.styl.js +1 -1
  19. package/dist/esm/src/components/ui/Select/Select.js +314 -6
  20. package/dist/esm/src/components/ui/Select/Select.styl.js +2 -2
  21. package/dist/esm/src/components/ui/Sheet/Sheet.styl.js +1 -1
  22. package/dist/esm/src/components/ui/SupplyChainMap/SupplyChainMap.js +384 -0
  23. package/dist/esm/src/components/ui/SupplyChainMap/SupplyChainMap.styl.js +7 -0
  24. package/dist/esm/src/components/ui/SupplyChainMap/map-colors.js +44 -0
  25. package/dist/esm/src/components/ui/SupplyChainMap/map-disclosure.js +142 -0
  26. package/dist/esm/src/components/ui/SupplyChainMap/map-labels.js +55 -0
  27. package/dist/esm/src/components/ui/SupplyChainMap/map-projection.js +186 -0
  28. package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-clusters.js +96 -0
  29. package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-draw.js +66 -0
  30. package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-scene.js +59 -0
  31. package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-types.js +10 -0
  32. package/dist/esm/src/components/ui/SupplyChainMap/use-map-base-renderer.js +122 -0
  33. package/dist/esm/src/components/ui/Table/Table.styl.js +1 -1
  34. package/dist/esm/src/components/ui/Tabs/Tabs.styl.js +1 -1
  35. package/dist/esm/src/components/ui/Tooltip/Tooltip.styl.js +1 -1
  36. package/dist/esm/src/components/widgets/DriverCard/DriverPerformanceChart.js +1 -2
  37. package/dist/esm/src/components/widgets/SybilionAppHeader/SybilionAppHeader.styl.js +1 -1
  38. package/dist/esm/src/index.js +2 -0
  39. package/dist/esm/types/src/components/ui/Chip/Chip.d.ts +1 -1
  40. package/dist/esm/types/src/components/ui/Chip/Chip.types.d.ts +2 -0
  41. package/dist/esm/types/src/components/ui/Chip/index.d.ts +1 -1
  42. package/dist/esm/types/src/components/ui/Select/Select.d.ts +3 -3
  43. package/dist/esm/types/src/components/ui/Select/Select.types.d.ts +3 -1
  44. package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.contract.test.d.ts +1 -0
  45. package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.d.ts +2 -0
  46. package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.example.d.ts +1 -0
  47. package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.render.worker.d.ts +15 -0
  48. package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.types.d.ts +10 -0
  49. package/dist/esm/types/src/components/ui/SupplyChainMap/index.d.ts +3 -0
  50. package/dist/esm/types/src/components/ui/SupplyChainMap/map-colors.d.ts +43 -0
  51. package/dist/esm/types/src/components/ui/SupplyChainMap/map-disclosure.d.ts +35 -0
  52. package/dist/esm/types/src/components/ui/SupplyChainMap/map-disclosure.test.d.ts +1 -0
  53. package/dist/esm/types/src/components/ui/SupplyChainMap/map-labels.d.ts +14 -0
  54. package/dist/esm/types/src/components/ui/SupplyChainMap/map-labels.test.d.ts +1 -0
  55. package/dist/esm/types/src/components/ui/SupplyChainMap/map-projection.d.ts +83 -0
  56. package/dist/esm/types/src/components/ui/SupplyChainMap/map-projection.test.d.ts +1 -0
  57. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-clusters.d.ts +25 -0
  58. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-clusters.test.d.ts +1 -0
  59. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-draw.d.ts +30 -0
  60. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-scene.d.ts +28 -0
  61. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-scene.test.d.ts +1 -0
  62. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-types.d.ts +31 -0
  63. package/dist/esm/types/src/components/ui/SupplyChainMap/use-map-base-renderer.d.ts +14 -0
  64. package/dist/esm/types/src/docs/pages/BannerPage.d.ts +1 -0
  65. package/dist/esm/types/src/docs/pages/SupplyChainMapPage.d.ts +1 -0
  66. package/dist/esm/types/src/index.d.ts +1 -0
  67. package/dist/esm/types/src/lib/logger.d.ts +1 -0
  68. package/package.json +7 -1
  69. package/src/components/ui/AppHeader/AppHeader.styl +2 -2
  70. package/src/components/ui/Banner/Banner.styl +1 -1
  71. package/src/components/ui/Card/Card.styl +3 -1
  72. package/src/components/ui/Chip/AGENT.md +1 -1
  73. package/src/components/ui/Chip/Chip.styl +66 -6
  74. package/src/components/ui/Chip/Chip.styl.d.ts +4 -0
  75. package/src/components/ui/Chip/Chip.tsx +15 -1
  76. package/src/components/ui/Chip/Chip.types.ts +3 -0
  77. package/src/components/ui/Chip/index.ts +1 -1
  78. package/src/components/ui/Dialog/Dialog.styl +2 -2
  79. package/src/components/ui/Drawer/Drawer.styl +2 -2
  80. package/src/components/ui/DropdownMenu/DropdownMenu.styl +2 -2
  81. package/src/components/ui/Input/Input.styl +9 -13
  82. package/src/components/ui/Logo/Logo.styl +1 -1
  83. package/src/components/ui/MobileAdaptiveSelector/MobileAdaptiveSelector.styl +1 -1
  84. package/src/components/ui/Page/PageHeader/PageHeader.styl +1 -1
  85. package/src/components/ui/Page/PageTabs/PageTabs.styl +3 -0
  86. package/src/components/ui/Select/AGENT.md +3 -1
  87. package/src/components/ui/Select/Select.styl +38 -6
  88. package/src/components/ui/Select/Select.styl.d.ts +28 -25
  89. package/src/components/ui/Select/Select.tsx +512 -5
  90. package/src/components/ui/Select/Select.types.ts +3 -1
  91. package/src/components/ui/Sheet/Sheet.styl +2 -2
  92. package/src/components/ui/SupplyChainMap/AGENT.md +45 -0
  93. package/src/components/ui/SupplyChainMap/SupplyChainMap.contract.test.ts +45 -0
  94. package/src/components/ui/SupplyChainMap/SupplyChainMap.example.tsx +176 -0
  95. package/src/components/ui/SupplyChainMap/SupplyChainMap.render.worker.ts +63 -0
  96. package/src/components/ui/SupplyChainMap/SupplyChainMap.styl +221 -0
  97. package/src/components/ui/SupplyChainMap/SupplyChainMap.styl.d.ts +25 -0
  98. package/src/components/ui/SupplyChainMap/SupplyChainMap.tsx +720 -0
  99. package/src/components/ui/SupplyChainMap/SupplyChainMap.types.ts +15 -0
  100. package/src/components/ui/SupplyChainMap/index.ts +11 -0
  101. package/src/components/ui/SupplyChainMap/map-colors.ts +66 -0
  102. package/src/components/ui/SupplyChainMap/map-disclosure.test.ts +237 -0
  103. package/src/components/ui/SupplyChainMap/map-disclosure.ts +226 -0
  104. package/src/components/ui/SupplyChainMap/map-labels.test.ts +102 -0
  105. package/src/components/ui/SupplyChainMap/map-labels.ts +85 -0
  106. package/src/components/ui/SupplyChainMap/map-projection.test.ts +172 -0
  107. package/src/components/ui/SupplyChainMap/map-projection.ts +281 -0
  108. package/src/components/ui/SupplyChainMap/supply-chain-map-clusters.test.ts +182 -0
  109. package/src/components/ui/SupplyChainMap/supply-chain-map-clusters.ts +127 -0
  110. package/src/components/ui/SupplyChainMap/supply-chain-map-draw.ts +114 -0
  111. package/src/components/ui/SupplyChainMap/supply-chain-map-scene.test.ts +196 -0
  112. package/src/components/ui/SupplyChainMap/supply-chain-map-scene.ts +118 -0
  113. package/src/components/ui/SupplyChainMap/supply-chain-map-types.ts +45 -0
  114. package/src/components/ui/SupplyChainMap/use-map-base-renderer.ts +145 -0
  115. package/src/components/ui/Table/Table.styl +4 -3
  116. package/src/components/ui/Tabs/Tabs.styl +4 -0
  117. package/src/components/ui/Tooltip/Tooltip.styl +7 -2
  118. package/src/components/widgets/SybilionAppHeader/SybilionAppHeader.styl +4 -4
  119. package/src/declarations.d.ts +4 -0
  120. package/src/docs/pages/BannerPage.tsx +83 -0
  121. package/src/docs/pages/ChipPage.tsx +20 -1
  122. package/src/docs/pages/FeedbackOverviewPage.tsx +7 -1
  123. package/src/docs/pages/SelectPage.tsx +30 -1
  124. package/src/docs/pages/SupplyChainMapPage.tsx +21 -0
  125. package/src/docs/registry.ts +12 -0
  126. package/src/index.ts +1 -0
@@ -0,0 +1,720 @@
1
+ import cn from 'classnames';
2
+ import {
3
+ type CSSProperties,
4
+ type PointerEvent as ReactPointerEvent,
5
+ useCallback,
6
+ useEffect,
7
+ useMemo,
8
+ useRef,
9
+ useState,
10
+ } from 'react';
11
+
12
+ import { useTheme } from '#uilib/contexts/theme-context';
13
+
14
+ import S from './SupplyChainMap.styl';
15
+ import type { SupplyChainMapProps } from './SupplyChainMap.types';
16
+ import { MAP_COLORS, MAP_COLORS_DARK, mapBasemapPalette } from './map-colors';
17
+ import {
18
+ discloseMapScene,
19
+ resolveMapDisclosureAnchors,
20
+ } from './map-disclosure';
21
+ import { placeMapLabels } from './map-labels';
22
+ import {
23
+ HOME_CAMERA,
24
+ MIN_ZOOM,
25
+ type MapCamera,
26
+ ZOOM_STEP,
27
+ cameraCenteredOn,
28
+ cameraFitPoints,
29
+ clampCamera,
30
+ mapProjection,
31
+ panCamera,
32
+ projectPoint,
33
+ syntheticAltitude,
34
+ zoomCamera,
35
+ } from './map-projection';
36
+ import {
37
+ CLUSTER_RADIUS_PX,
38
+ clusterMapNodes,
39
+ forceOpenExpired,
40
+ remapArcsToClusters,
41
+ } from './supply-chain-map-clusters';
42
+ import type { MapDrawArc, MapDrawCommand } from './supply-chain-map-draw';
43
+ import {
44
+ type MapSceneArc,
45
+ type MapSceneNode,
46
+ buildConfirmedMapScene,
47
+ resolveMapSelection,
48
+ } from './supply-chain-map-scene';
49
+ import { activeMapCascade } from './supply-chain-map-types';
50
+ import { useMapBaseRenderer } from './use-map-base-renderer';
51
+
52
+ type SceneArc = MapSceneArc & {
53
+ fromId: string;
54
+ toId: string;
55
+ onSpine?: boolean;
56
+ };
57
+
58
+ const DRAG_THRESHOLD_PX = 4;
59
+ const LANE_HIT_PX = 7;
60
+ const FLY_MS = 700;
61
+
62
+ function labelTier(altitude: number): 'far' | 'mid' | 'near' {
63
+ if (altitude > 1.6) return 'far';
64
+ if (altitude > 0.9) return 'mid';
65
+ return 'near';
66
+ }
67
+
68
+ function easeInOutCubic(t: number): number {
69
+ return t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
70
+ }
71
+
72
+ function segmentDistance(
73
+ px: number,
74
+ py: number,
75
+ ax: number,
76
+ ay: number,
77
+ bx: number,
78
+ by: number,
79
+ ): number {
80
+ const dx = bx - ax;
81
+ const dy = by - ay;
82
+ const lenSq = dx * dx + dy * dy;
83
+ const t =
84
+ lenSq === 0
85
+ ? 0
86
+ : Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / lenSq));
87
+ return Math.hypot(px - (ax + t * dx), py - (ay + t * dy));
88
+ }
89
+
90
+ function inspectionLocation(node: MapSceneNode): string | null {
91
+ return (
92
+ [node.place, node.country, node.precision].filter(Boolean).join(' · ') ||
93
+ null
94
+ );
95
+ }
96
+
97
+ function orgNodeId(
98
+ entities: SupplyChainMapProps['entities'],
99
+ ): string | undefined {
100
+ return entities.find(
101
+ entity => entity.type === 'company' && entity.status === 'confirmed',
102
+ )?.id;
103
+ }
104
+
105
+ export function SupplyChainMap({
106
+ entities,
107
+ edges,
108
+ onEdgeSelect,
109
+ onNodeClick,
110
+ cascade,
111
+ fill,
112
+ className,
113
+ }: SupplyChainMapProps) {
114
+ const { isDarkMode } = useTheme();
115
+ const marks = isDarkMode ? MAP_COLORS_DARK : MAP_COLORS;
116
+ const wrapRef = useRef<HTMLDivElement | null>(null);
117
+ const canvasRef = useRef<HTMLCanvasElement | null>(null);
118
+ const [size, setSize] = useState({ w: 0, h: 0 });
119
+ const [camera, setCamera] = useState<MapCamera | null>(null);
120
+ const [showAll, setShowAll] = useState(false);
121
+ const [selected, setSelected] = useState<MapSceneNode | null>(null);
122
+ const [expandedIds, setExpandedIds] = useState<ReadonlySet<string>>(
123
+ () => new Set(),
124
+ );
125
+ const forceOpenKRef = useRef<number | null>(null);
126
+
127
+ const scene = useMemo(
128
+ () => buildConfirmedMapScene(entities, edges),
129
+ [entities, edges],
130
+ );
131
+ const liveCascade = useMemo(() => activeMapCascade(cascade), [cascade]);
132
+ const orgId = useMemo(() => orgNodeId(entities), [entities]);
133
+
134
+ const sceneNodes = useMemo(
135
+ () =>
136
+ scene.nodes.map(node => ({
137
+ ...node,
138
+ impacted: liveCascade?.onPathIds.has(node.id) ?? false,
139
+ })),
140
+ [scene.nodes, liveCascade],
141
+ );
142
+ const sceneArcs = useMemo<SceneArc[]>(
143
+ () =>
144
+ scene.arcs.map(arc => ({
145
+ ...arc,
146
+ fromId: arc.fromEntity,
147
+ toId: arc.toEntity,
148
+ onSpine: liveCascade?.pathIds.includes(arc.fromEntity) ?? false,
149
+ })),
150
+ [scene.arcs, liveCascade],
151
+ );
152
+
153
+ const byId = useMemo(
154
+ () => new Map(sceneNodes.map(node => [node.id, node])),
155
+ [sceneNodes],
156
+ );
157
+ const inspected = resolveMapSelection(selected, sceneNodes);
158
+ const disclosureAnchors = useMemo(
159
+ () =>
160
+ resolveMapDisclosureAnchors(byId, {
161
+ selectedId: inspected?.id ?? null,
162
+ }),
163
+ [byId, inspected],
164
+ );
165
+
166
+ const altitude = syntheticAltitude(camera?.k ?? 1);
167
+ const tier = labelTier(altitude);
168
+ const disclosedScene = useMemo(
169
+ () =>
170
+ discloseMapScene(sceneNodes, sceneArcs, {
171
+ tier,
172
+ orgId,
173
+ ...disclosureAnchors,
174
+ expanded: showAll,
175
+ }),
176
+ [sceneNodes, sceneArcs, tier, orgId, disclosureAnchors, showAll],
177
+ );
178
+
179
+ useEffect(() => {
180
+ const el = wrapRef.current;
181
+ if (!el) return;
182
+ const apply = (w: number, h: number) => {
183
+ if (w > 0 && h > 0) setSize({ w: Math.round(w), h: Math.round(h) });
184
+ };
185
+ apply(el.clientWidth, el.clientHeight);
186
+ const ro = new ResizeObserver(entries => {
187
+ const r = entries[0]?.contentRect;
188
+ if (r) apply(r.width, r.height);
189
+ });
190
+ ro.observe(el);
191
+ return () => ro.disconnect();
192
+ }, [scene.nodes.length > 0]);
193
+
194
+ useEffect(() => {
195
+ if (size.w === 0 || size.h === 0) return;
196
+ setCamera(cam => (cam ? clampCamera(cam, size.w, size.h) : cam));
197
+ }, [size.w, size.h]);
198
+
199
+ useEffect(() => {
200
+ if (selected && !inspected) setSelected(null);
201
+ }, [selected, inspected]);
202
+
203
+ useEffect(() => {
204
+ const openedAt = forceOpenKRef.current;
205
+ const k = camera?.k;
206
+ if (openedAt == null || k == null || expandedIds.size === 0) return;
207
+ if (!forceOpenExpired(openedAt, k)) return;
208
+ forceOpenKRef.current = null;
209
+ setExpandedIds(new Set());
210
+ }, [camera?.k, expandedIds.size]);
211
+
212
+ const cameraRef = useRef<MapCamera | null>(null);
213
+ cameraRef.current = camera;
214
+ const flyRaf = useRef(0);
215
+ const stopFlying = useCallback(() => {
216
+ if (flyRaf.current) cancelAnimationFrame(flyRaf.current);
217
+ flyRaf.current = 0;
218
+ }, []);
219
+ useEffect(() => stopFlying, [stopFlying]);
220
+
221
+ const flyTo = useCallback(
222
+ (target: MapCamera) => {
223
+ stopFlying();
224
+ const from = cameraRef.current ?? HOME_CAMERA;
225
+ const start = performance.now();
226
+ const step = (now: number) => {
227
+ const t = Math.min(1, (now - start) / FLY_MS);
228
+ const e = easeInOutCubic(t);
229
+ setCamera(
230
+ clampCamera(
231
+ {
232
+ k: from.k + (target.k - from.k) * e,
233
+ x: from.x + (target.x - from.x) * e,
234
+ y: from.y + (target.y - from.y) * e,
235
+ },
236
+ size.w,
237
+ size.h,
238
+ ),
239
+ );
240
+ if (t < 1) flyRaf.current = requestAnimationFrame(step);
241
+ };
242
+ flyRaf.current = requestAnimationFrame(step);
243
+ },
244
+ [size.w, size.h, stopFlying],
245
+ );
246
+
247
+ const openedRef = useRef(false);
248
+ useEffect(() => {
249
+ if (openedRef.current || size.w === 0 || size.h === 0) return;
250
+ openedRef.current = true;
251
+ setCamera(
252
+ cameraFitPoints(
253
+ scene.nodes.map(n => ({ lat: n.lat, lng: n.lng })),
254
+ size.w,
255
+ size.h,
256
+ ),
257
+ );
258
+ }, [size.w, size.h, scene.nodes]);
259
+
260
+ const resetView = useCallback(() => {
261
+ forceOpenKRef.current = null;
262
+ setExpandedIds(new Set());
263
+ flyTo(
264
+ cameraFitPoints(
265
+ scene.nodes.map(n => ({ lat: n.lat, lng: n.lng })),
266
+ size.w,
267
+ size.h,
268
+ ),
269
+ );
270
+ }, [flyTo, scene.nodes, size.w, size.h]);
271
+
272
+ const zoomBy = useCallback(
273
+ (dir: 1 | -1) => {
274
+ const cam = cameraRef.current ?? HOME_CAMERA;
275
+ flyTo(
276
+ zoomCamera(
277
+ cam,
278
+ dir > 0 ? ZOOM_STEP : 1 / ZOOM_STEP,
279
+ { x: size.w / 2, y: size.h / 2 },
280
+ size.w,
281
+ size.h,
282
+ ),
283
+ );
284
+ },
285
+ [flyTo, size.w, size.h],
286
+ );
287
+
288
+ const dragRef = useRef<{ x: number; y: number; moved: boolean } | null>(null);
289
+ const hitLanesRef = useRef<{ id: string; points: [number, number][] }[]>([]);
290
+
291
+ const onPointerDown = useCallback((ev: ReactPointerEvent<HTMLDivElement>) => {
292
+ if (ev.button !== 0) return;
293
+ dragRef.current = { x: ev.clientX, y: ev.clientY, moved: false };
294
+ ev.currentTarget.setPointerCapture(ev.pointerId);
295
+ }, []);
296
+
297
+ const onPointerMove = useCallback(
298
+ (ev: ReactPointerEvent<HTMLDivElement>) => {
299
+ const drag = dragRef.current;
300
+ if (!drag) return;
301
+ const dx = ev.clientX - drag.x;
302
+ const dy = ev.clientY - drag.y;
303
+ if (!drag.moved && Math.hypot(dx, dy) < DRAG_THRESHOLD_PX) return;
304
+ drag.moved = true;
305
+ drag.x = ev.clientX;
306
+ drag.y = ev.clientY;
307
+ stopFlying();
308
+ setCamera(cam => (cam ? panCamera(cam, dx, dy, size.w, size.h) : cam));
309
+ },
310
+ [size.w, size.h, stopFlying],
311
+ );
312
+
313
+ const closeInspector = useCallback(() => setSelected(null), []);
314
+
315
+ const onPointerUp = useCallback(
316
+ (ev: ReactPointerEvent<HTMLDivElement>) => {
317
+ const drag = dragRef.current;
318
+ dragRef.current = null;
319
+ if (!drag || drag.moved) return;
320
+ const rect = wrapRef.current?.getBoundingClientRect();
321
+ if (!rect) return;
322
+ const x = ev.clientX - rect.left;
323
+ const y = ev.clientY - rect.top;
324
+ let bestId: string | null = null;
325
+ let bestD = LANE_HIT_PX;
326
+ for (const lane of hitLanesRef.current) {
327
+ const pts = lane.points;
328
+ for (let i = 1; i < pts.length; i++) {
329
+ const d = segmentDistance(
330
+ x,
331
+ y,
332
+ pts[i - 1][0],
333
+ pts[i - 1][1],
334
+ pts[i][0],
335
+ pts[i][1],
336
+ );
337
+ if (d < bestD) {
338
+ bestD = d;
339
+ bestId = lane.id;
340
+ }
341
+ }
342
+ }
343
+ if (bestId) {
344
+ setSelected(null);
345
+ onEdgeSelect?.(bestId);
346
+ return;
347
+ }
348
+ closeInspector();
349
+ },
350
+ [closeInspector, onEdgeSelect],
351
+ );
352
+
353
+ useEffect(() => {
354
+ const el = wrapRef.current;
355
+ if (!el) return;
356
+ const onWheel = (ev: WheelEvent) => {
357
+ ev.preventDefault();
358
+ const rect = el.getBoundingClientRect();
359
+ stopFlying();
360
+ const at = { x: ev.clientX - rect.left, y: ev.clientY - rect.top };
361
+ setCamera(cam =>
362
+ cam
363
+ ? zoomCamera(cam, Math.exp(-ev.deltaY * 0.0022), at, size.w, size.h)
364
+ : cam,
365
+ );
366
+ };
367
+ el.addEventListener('wheel', onWheel, { passive: false });
368
+ return () => el.removeEventListener('wheel', onWheel);
369
+ }, [size.w, size.h, stopFlying]);
370
+
371
+ const onKeyDown = useCallback(
372
+ (ev: React.KeyboardEvent<HTMLDivElement>) => {
373
+ if (ev.target !== ev.currentTarget) return;
374
+ const pan = 60;
375
+ if (ev.key === '+' || ev.key === '=') zoomBy(1);
376
+ else if (ev.key === '-' || ev.key === '_') zoomBy(-1);
377
+ else if (ev.key === '0') resetView();
378
+ else if (ev.key === 'ArrowLeft')
379
+ setCamera(c => (c ? panCamera(c, pan, 0, size.w, size.h) : c));
380
+ else if (ev.key === 'ArrowRight')
381
+ setCamera(c => (c ? panCamera(c, -pan, 0, size.w, size.h) : c));
382
+ else if (ev.key === 'ArrowUp')
383
+ setCamera(c => (c ? panCamera(c, 0, pan, size.w, size.h) : c));
384
+ else if (ev.key === 'ArrowDown')
385
+ setCamera(c => (c ? panCamera(c, 0, -pan, size.w, size.h) : c));
386
+ else if (ev.key === 'Escape') closeInspector();
387
+ else return;
388
+ ev.preventDefault();
389
+ },
390
+ [zoomBy, resetView, size.w, size.h, closeInspector],
391
+ );
392
+
393
+ const proj = useMemo(
394
+ () =>
395
+ camera && size.w > 0
396
+ ? mapProjection(size.w, size.h, camera).clipAngle(null)
397
+ : null,
398
+ [camera, size],
399
+ );
400
+ const worldView = (camera?.k ?? MIN_ZOOM) <= MIN_ZOOM + 1e-6;
401
+ const project = useCallback(
402
+ (lat: number, lng: number): [number, number] | null =>
403
+ proj ? projectPoint(proj, lat, lng) : null,
404
+ [proj],
405
+ );
406
+ const onScreen = useCallback(
407
+ (p: [number, number] | null, margin = 40): p is [number, number] =>
408
+ p !== null &&
409
+ p[0] > -margin &&
410
+ p[0] < size.w + margin &&
411
+ p[1] > -margin &&
412
+ p[1] < size.h + margin,
413
+ [size],
414
+ );
415
+
416
+ const clusters = useMemo(
417
+ () =>
418
+ proj
419
+ ? clusterMapNodes(sceneNodes, project, CLUSTER_RADIUS_PX, expandedIds)
420
+ : [],
421
+ [proj, sceneNodes, project, expandedIds],
422
+ );
423
+ const clusteredArcs = useMemo(
424
+ () => remapArcsToClusters(disclosedScene.arcs, clusters),
425
+ [disclosedScene.arcs, clusters],
426
+ );
427
+
428
+ const drawArcs = useMemo<MapDrawArc[]>(
429
+ () =>
430
+ clusteredArcs.map(a => {
431
+ const dim = Boolean(
432
+ liveCascade &&
433
+ !liveCascade.laneIds.has(a.id) &&
434
+ !liveCascade.onPathIds.has(a.fromEntity) &&
435
+ !liveCascade.onPathIds.has(a.toEntity),
436
+ );
437
+ return {
438
+ id: a.id,
439
+ from: a.from,
440
+ to: a.to,
441
+ color:
442
+ a.onSpine || liveCascade?.laneIds.has(a.id)
443
+ ? marks.spine
444
+ : marks.arcDefault,
445
+ alpha: dim ? 0.22 : 1,
446
+ };
447
+ }),
448
+ [clusteredArcs, liveCascade, marks],
449
+ );
450
+
451
+ const drawCommand = useMemo<MapDrawCommand | null>(() => {
452
+ if (!camera || size.w === 0 || size.h === 0) return null;
453
+ return {
454
+ width: size.w,
455
+ height: size.h,
456
+ dpr: typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1,
457
+ camera,
458
+ arcs: drawArcs,
459
+ palette: mapBasemapPalette(isDarkMode),
460
+ };
461
+ }, [camera, size, drawArcs, isDarkMode]);
462
+
463
+ const { mode: rendererMode, canvasKey } = useMapBaseRenderer(
464
+ canvasRef,
465
+ drawCommand,
466
+ lanes => {
467
+ hitLanesRef.current = lanes;
468
+ },
469
+ scene.nodes.length > 0,
470
+ );
471
+
472
+ const labels = useMemo(
473
+ () =>
474
+ clusters
475
+ .filter(c => c.nodeId)
476
+ .map(c => {
477
+ const n = byId.get(c.nodeId!);
478
+ return {
479
+ id: c.id,
480
+ lat: c.lat,
481
+ lng: c.lng,
482
+ text: n?.name ?? c.id,
483
+ primary: c.id === orgId || c.id === inspected?.id,
484
+ };
485
+ }),
486
+ [clusters, byId, orgId, inspected],
487
+ );
488
+ const placedLabels = useMemo(
489
+ () => (proj ? placeMapLabels(labels, project, size.w, size.h) : []),
490
+ [proj, labels, project, size],
491
+ );
492
+ const placedLabelById = useMemo(
493
+ () => new Map(placedLabels.map(l => [l.id, l])),
494
+ [placedLabels],
495
+ );
496
+
497
+ const flyToPlace = useCallback(
498
+ (lat: number, lng: number) => {
499
+ const k = cameraRef.current?.k ?? HOME_CAMERA.k;
500
+ flyTo(cameraCenteredOn(lat, lng, k, size.w, size.h));
501
+ },
502
+ [flyTo, size.w, size.h],
503
+ );
504
+
505
+ const activateCluster = useCallback(
506
+ (cluster: (typeof clusters)[number]) => {
507
+ if (cluster.nodeId) {
508
+ const node = byId.get(cluster.nodeId);
509
+ if (!node) return;
510
+ setSelected(node);
511
+ onNodeClick?.(node.id);
512
+ flyToPlace(node.lat, node.lng);
513
+ return;
514
+ }
515
+ setSelected(null);
516
+ if (forceOpenKRef.current == null) {
517
+ forceOpenKRef.current = cameraRef.current?.k ?? HOME_CAMERA.k;
518
+ }
519
+ setExpandedIds(prev => {
520
+ const next = new Set(prev);
521
+ for (const id of cluster.memberIds) next.add(id);
522
+ return next;
523
+ });
524
+ },
525
+ [byId, flyToPlace, onNodeClick],
526
+ );
527
+
528
+ const at = (p: [number, number]): CSSProperties => ({
529
+ left: `${p[0]}px`,
530
+ top: `${p[1]}px`,
531
+ });
532
+
533
+ const ready = camera !== null && size.w > 0;
534
+ const shownRouteCount = disclosedScene.arcs.length;
535
+ const totalRouteCount = scene.arcs.length;
536
+
537
+ if (scene.nodes.length === 0) {
538
+ return (
539
+ <div className={cn(S.root, fill && S.fill, className)}>
540
+ <div className={S.empty} role="status">
541
+ No confirmed locations yet — confirm a placed node to show it on the
542
+ map.
543
+ </div>
544
+ </div>
545
+ );
546
+ }
547
+
548
+ return (
549
+ <div className={cn(S.root, fill && S.fill, className)}>
550
+ <div
551
+ ref={wrapRef}
552
+ className={S.canvas}
553
+ role="application"
554
+ aria-label={`Supply chain map, ${scene.nodes.length} located ${
555
+ scene.nodes.length === 1 ? 'site' : 'sites'
556
+ }, ${shownRouteCount} of ${totalRouteCount} routes. Drag to pan, arrow keys pan, plus and minus zoom, 0 resets the view.`}
557
+ data-map-renderer={rendererMode}
558
+ data-camera-k={camera?.k ?? ''}
559
+ data-map-force-open={expandedIds.size}
560
+ data-map-places={scene.nodes.length}
561
+ data-map-glyphs={
562
+ clusters.filter(c => {
563
+ const p = project(c.lat, c.lng);
564
+ return p != null && (worldView || onScreen(p));
565
+ }).length
566
+ }
567
+ data-map-clustered={clusters.reduce((sum, c) => sum + c.count, 0)}
568
+ tabIndex={0}
569
+ onPointerDown={onPointerDown}
570
+ onPointerMove={onPointerMove}
571
+ onPointerUp={onPointerUp}
572
+ onPointerCancel={() => {
573
+ dragRef.current = null;
574
+ }}
575
+ onKeyDown={onKeyDown}
576
+ >
577
+ <canvas
578
+ key={canvasKey}
579
+ ref={canvasRef}
580
+ className={S.baseCanvas}
581
+ aria-hidden
582
+ />
583
+ {ready ? (
584
+ <div className={S.overlay}>
585
+ {clusters.map(cluster => {
586
+ const p = project(cluster.lat, cluster.lng);
587
+ if (!p || (!worldView && !onScreen(p))) return null;
588
+ const node = cluster.nodeId
589
+ ? byId.get(cluster.nodeId)
590
+ : undefined;
591
+ const dim = Boolean(
592
+ liveCascade &&
593
+ !cluster.memberIds.some(id => liveCascade.onPathIds.has(id)),
594
+ );
595
+ const selectedNode = Boolean(
596
+ inspected && cluster.memberIds.includes(inspected.id),
597
+ );
598
+ const isOrg = Boolean(orgId && cluster.memberIds.includes(orgId));
599
+ const label = cluster.nodeId
600
+ ? placedLabelById.get(cluster.id)
601
+ : undefined;
602
+ const names = cluster.memberIds
603
+ .map(id => byId.get(id)?.name)
604
+ .filter((name): name is string => Boolean(name));
605
+ const title =
606
+ node?.name ??
607
+ (names.length > 3
608
+ ? `${names.slice(0, 3).join(', ')} +${names.length - 3}`
609
+ : names.join(', '));
610
+ return (
611
+ <div
612
+ key={cluster.id}
613
+ className={cn(
614
+ S.marker,
615
+ cluster.count > 1 && S.cluster,
616
+ selectedNode && S.selected,
617
+ dim && S.dimmed,
618
+ )}
619
+ style={at(p)}
620
+ >
621
+ <button
622
+ type="button"
623
+ className={cn(S.node, cluster.count > 1 && S.cluster)}
624
+ style={{
625
+ background: isOrg ? marks.org : marks.chain,
626
+ }}
627
+ title={title}
628
+ aria-label={
629
+ node
630
+ ? `Inspect ${node.name}`
631
+ : `Show ${cluster.count} nearby sites`
632
+ }
633
+ onPointerDown={e => e.stopPropagation()}
634
+ onPointerUp={e => e.stopPropagation()}
635
+ onClick={e => {
636
+ e.stopPropagation();
637
+ activateCluster(cluster);
638
+ }}
639
+ >
640
+ {cluster.count > 1 ? cluster.count : null}
641
+ </button>
642
+ {label ? (
643
+ <button
644
+ type="button"
645
+ className={cn(S.label, label.primary && S.primary)}
646
+ title={label.text}
647
+ aria-label={`Inspect ${label.text}`}
648
+ onPointerDown={e => e.stopPropagation()}
649
+ onPointerUp={e => e.stopPropagation()}
650
+ onClick={e => {
651
+ e.stopPropagation();
652
+ activateCluster(cluster);
653
+ }}
654
+ >
655
+ {label.text}
656
+ </button>
657
+ ) : null}
658
+ </div>
659
+ );
660
+ })}
661
+ </div>
662
+ ) : null}
663
+
664
+ <div className={S.disclosure} role="region" aria-label="Map disclosure">
665
+ <span role="status" aria-live="polite">
666
+ <strong>{showAll ? 'All routes' : 'Overview'}</strong>
667
+ {' · '}
668
+ <span>
669
+ {shownRouteCount} of {totalRouteCount} routes
670
+ </span>
671
+ {disclosedScene.canRevealByZoom ? (
672
+ <span> · Zoom in to reveal more</span>
673
+ ) : null}
674
+ </span>
675
+ <button
676
+ type="button"
677
+ className={S.action}
678
+ aria-pressed={showAll}
679
+ onClick={() => setShowAll(expanded => !expanded)}
680
+ >
681
+ {showAll ? 'Return to overview' : 'Show all'}
682
+ </button>
683
+ <div className={S.zoom}>
684
+ <button
685
+ type="button"
686
+ className={S.action}
687
+ onClick={() => zoomBy(1)}
688
+ >
689
+ +
690
+ </button>
691
+ <button
692
+ type="button"
693
+ className={S.action}
694
+ onClick={() => zoomBy(-1)}
695
+ >
696
+ −
697
+ </button>
698
+ <button type="button" className={S.action} onClick={resetView}>
699
+ 0
700
+ </button>
701
+ </div>
702
+ </div>
703
+ </div>
704
+
705
+ {inspected ? (
706
+ <div className={S.inspection} aria-live="polite">
707
+ <div className={S.kicker}>{inspected.type}</div>
708
+ <strong>{inspected.name}</strong>
709
+ {inspectionLocation(inspected) ? (
710
+ <p>{inspectionLocation(inspected)}</p>
711
+ ) : null}
712
+ </div>
713
+ ) : (
714
+ <p className={S.hint}>
715
+ Select a confirmed location to inspect its stored details.
716
+ </p>
717
+ )}
718
+ </div>
719
+ );
720
+ }