@sybilion/uilib 1.4.37 → 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.
- package/dist/agent-glossary/workspace.generated.ts +1 -0
- package/dist/agent-glossary/workspace.index.md +1 -0
- package/dist/agent-glossary/workspace.md +1 -0
- package/dist/esm/src/components/ui/Banner/Banner.styl.js +1 -1
- package/dist/esm/src/components/ui/Chart/Chart.js +1 -2
- package/dist/esm/src/components/ui/Dialog/Dialog.styl.js +1 -1
- package/dist/esm/src/components/ui/Drawer/Drawer.styl.js +1 -1
- package/dist/esm/src/components/ui/DropdownMenu/DropdownMenu.styl.js +1 -1
- package/dist/esm/src/components/ui/MobileAdaptiveSelector/MobileAdaptiveSelector.styl.js +1 -1
- package/dist/esm/src/components/ui/Page/PageHeader/PageHeader.styl.js +1 -1
- package/dist/esm/src/components/ui/Select/Select.js +313 -5
- package/dist/esm/src/components/ui/Select/Select.styl.js +2 -2
- package/dist/esm/src/components/ui/Sheet/Sheet.styl.js +1 -1
- package/dist/esm/src/components/ui/SupplyChainMap/SupplyChainMap.js +384 -0
- package/dist/esm/src/components/ui/SupplyChainMap/SupplyChainMap.styl.js +7 -0
- package/dist/esm/src/components/ui/SupplyChainMap/map-colors.js +44 -0
- package/dist/esm/src/components/ui/SupplyChainMap/map-disclosure.js +142 -0
- package/dist/esm/src/components/ui/SupplyChainMap/map-labels.js +55 -0
- package/dist/esm/src/components/ui/SupplyChainMap/map-projection.js +186 -0
- package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-clusters.js +96 -0
- package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-draw.js +66 -0
- package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-scene.js +59 -0
- package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-types.js +10 -0
- package/dist/esm/src/components/ui/SupplyChainMap/use-map-base-renderer.js +122 -0
- package/dist/esm/src/components/ui/Table/Table.styl.js +1 -1
- package/dist/esm/src/components/ui/Tabs/Tabs.styl.js +1 -1
- package/dist/esm/src/components/ui/Tooltip/Tooltip.styl.js +1 -1
- package/dist/esm/src/components/widgets/DriverCard/DriverPerformanceChart.js +1 -2
- package/dist/esm/src/index.js +2 -0
- package/dist/esm/types/src/components/ui/Select/Select.d.ts +3 -3
- package/dist/esm/types/src/components/ui/Select/Select.types.d.ts +3 -1
- package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.contract.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.d.ts +2 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.example.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.render.worker.d.ts +15 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.types.d.ts +10 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/index.d.ts +3 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-colors.d.ts +43 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-disclosure.d.ts +35 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-disclosure.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-labels.d.ts +14 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-labels.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-projection.d.ts +83 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/map-projection.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-clusters.d.ts +25 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-clusters.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-draw.d.ts +30 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-scene.d.ts +28 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-scene.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-types.d.ts +31 -0
- package/dist/esm/types/src/components/ui/SupplyChainMap/use-map-base-renderer.d.ts +14 -0
- package/dist/esm/types/src/docs/pages/BannerPage.d.ts +1 -0
- package/dist/esm/types/src/docs/pages/SupplyChainMapPage.d.ts +1 -0
- package/dist/esm/types/src/index.d.ts +1 -0
- package/dist/esm/types/src/lib/logger.d.ts +1 -0
- package/package.json +7 -1
- package/src/components/ui/Banner/Banner.styl +1 -1
- package/src/components/ui/Dialog/Dialog.styl +2 -2
- package/src/components/ui/Drawer/Drawer.styl +2 -2
- package/src/components/ui/DropdownMenu/DropdownMenu.styl +2 -2
- package/src/components/ui/MobileAdaptiveSelector/MobileAdaptiveSelector.styl +1 -1
- package/src/components/ui/Page/PageHeader/PageHeader.styl +1 -1
- package/src/components/ui/Select/AGENT.md +3 -1
- package/src/components/ui/Select/Select.styl +31 -0
- package/src/components/ui/Select/Select.styl.d.ts +28 -25
- package/src/components/ui/Select/Select.tsx +506 -4
- package/src/components/ui/Select/Select.types.ts +3 -1
- package/src/components/ui/Sheet/Sheet.styl +2 -2
- package/src/components/ui/SupplyChainMap/AGENT.md +45 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.contract.test.ts +45 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.example.tsx +176 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.render.worker.ts +63 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.styl +221 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.styl.d.ts +25 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.tsx +720 -0
- package/src/components/ui/SupplyChainMap/SupplyChainMap.types.ts +15 -0
- package/src/components/ui/SupplyChainMap/index.ts +11 -0
- package/src/components/ui/SupplyChainMap/map-colors.ts +66 -0
- package/src/components/ui/SupplyChainMap/map-disclosure.test.ts +237 -0
- package/src/components/ui/SupplyChainMap/map-disclosure.ts +226 -0
- package/src/components/ui/SupplyChainMap/map-labels.test.ts +102 -0
- package/src/components/ui/SupplyChainMap/map-labels.ts +85 -0
- package/src/components/ui/SupplyChainMap/map-projection.test.ts +172 -0
- package/src/components/ui/SupplyChainMap/map-projection.ts +281 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-clusters.test.ts +182 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-clusters.ts +127 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-draw.ts +114 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-scene.test.ts +196 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-scene.ts +118 -0
- package/src/components/ui/SupplyChainMap/supply-chain-map-types.ts +45 -0
- package/src/components/ui/SupplyChainMap/use-map-base-renderer.ts +145 -0
- package/src/components/ui/Table/Table.styl +3 -3
- package/src/components/ui/Tabs/Tabs.styl +1 -0
- package/src/components/ui/Tooltip/Tooltip.styl +7 -2
- package/src/declarations.d.ts +4 -0
- package/src/docs/pages/BannerPage.tsx +83 -0
- package/src/docs/pages/FeedbackOverviewPage.tsx +7 -1
- package/src/docs/pages/SelectPage.tsx +30 -1
- package/src/docs/pages/SupplyChainMapPage.tsx +21 -0
- package/src/docs/registry.ts +12 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
CLUSTER_RADIUS_PX,
|
|
5
|
+
clusterMapNodes,
|
|
6
|
+
forceOpenExpired,
|
|
7
|
+
remapArcsToClusters,
|
|
8
|
+
} from './supply-chain-map-clusters';
|
|
9
|
+
|
|
10
|
+
function projectScale(
|
|
11
|
+
scale: number,
|
|
12
|
+
): (lat: number, lng: number) => [number, number] {
|
|
13
|
+
return (lat, lng) => [lng * scale, lat * scale];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function testIsolatedNodesStaySingletons() {
|
|
17
|
+
const nodes = [
|
|
18
|
+
{ id: 'a', lat: 0, lng: 0 },
|
|
19
|
+
{ id: 'b', lat: 10, lng: 10 },
|
|
20
|
+
];
|
|
21
|
+
const clusters = clusterMapNodes(nodes, projectScale(20), 40);
|
|
22
|
+
assert.equal(clusters.length, 2);
|
|
23
|
+
assert.deepEqual(clusters.map(c => c.id).sort(), ['a', 'b']);
|
|
24
|
+
assert.ok(clusters.every(c => c.count === 1 && c.nodeId === c.id));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function testNearbyPixelsMerge() {
|
|
28
|
+
const nodes = [
|
|
29
|
+
{ id: 'b', lat: 0, lng: 1 },
|
|
30
|
+
{ id: 'a', lat: 0, lng: 0 },
|
|
31
|
+
];
|
|
32
|
+
const clusters = clusterMapNodes(nodes, projectScale(10), 40);
|
|
33
|
+
assert.equal(clusters.length, 1);
|
|
34
|
+
assert.equal(clusters[0].count, 2);
|
|
35
|
+
assert.deepEqual(clusters[0].memberIds, ['a', 'b']);
|
|
36
|
+
assert.equal(clusters[0].id, 'cluster:a');
|
|
37
|
+
assert.equal(clusters[0].nodeId, null);
|
|
38
|
+
assert.equal(clusters[0].lat, 0);
|
|
39
|
+
assert.equal(clusters[0].lng, 0.5);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function testFarPixelsStaySeparate() {
|
|
43
|
+
const nodes = [
|
|
44
|
+
{ id: 'a', lat: 0, lng: 0 },
|
|
45
|
+
{ id: 'b', lat: 0, lng: 10 },
|
|
46
|
+
];
|
|
47
|
+
const clusters = clusterMapNodes(nodes, projectScale(10), 40);
|
|
48
|
+
assert.equal(clusters.length, 2);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function testConnectedComponentMergesAChain() {
|
|
52
|
+
const nodes = [
|
|
53
|
+
{ id: 'a', lat: 0, lng: 0 },
|
|
54
|
+
{ id: 'b', lat: 0, lng: 3 },
|
|
55
|
+
{ id: 'c', lat: 0, lng: 6 },
|
|
56
|
+
];
|
|
57
|
+
// 30px between neighbours, 60px from a to c — still one cluster.
|
|
58
|
+
const clusters = clusterMapNodes(nodes, projectScale(10), 40);
|
|
59
|
+
assert.equal(clusters.length, 1);
|
|
60
|
+
assert.deepEqual(clusters[0].memberIds, ['a', 'b', 'c']);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function testEveryProjectedNodeStaysInACluster() {
|
|
64
|
+
const nodes = Array.from({ length: 40 }, (_, i) => ({
|
|
65
|
+
id: `n-${i}`,
|
|
66
|
+
lat: (i % 8) * 4,
|
|
67
|
+
lng: Math.floor(i / 8) * 4,
|
|
68
|
+
}));
|
|
69
|
+
const clusters = clusterMapNodes(nodes, projectScale(2), 40);
|
|
70
|
+
const members = clusters.flatMap(c => c.memberIds).sort();
|
|
71
|
+
assert.deepEqual(members, nodes.map(n => n.id).sort());
|
|
72
|
+
assert.equal(
|
|
73
|
+
clusters.reduce((sum, c) => sum + c.count, 0),
|
|
74
|
+
nodes.length,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function testUnprojectableNodesAreDropped() {
|
|
79
|
+
const clusters = clusterMapNodes(
|
|
80
|
+
[
|
|
81
|
+
{ id: 'a', lat: 0, lng: 0 },
|
|
82
|
+
{ id: 'ghost', lat: 99, lng: 99 },
|
|
83
|
+
],
|
|
84
|
+
(lat, lng) => (lat === 99 ? null : [lng, lat]),
|
|
85
|
+
40,
|
|
86
|
+
);
|
|
87
|
+
assert.deepEqual(
|
|
88
|
+
clusters.map(c => c.id),
|
|
89
|
+
['a'],
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function testForceOpenExpiresOnlyOnZoomOutPastExpandK() {
|
|
94
|
+
assert.equal(forceOpenExpired(3, 3), false);
|
|
95
|
+
assert.equal(forceOpenExpired(3, 4), false);
|
|
96
|
+
assert.equal(forceOpenExpired(3, 2.9), true);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function testExpandIdsStaySingletons() {
|
|
100
|
+
const nodes = [
|
|
101
|
+
{ id: 'a', lat: 0, lng: 0 },
|
|
102
|
+
{ id: 'b', lat: 0, lng: 1 },
|
|
103
|
+
];
|
|
104
|
+
const merged = clusterMapNodes(nodes, projectScale(10), 40);
|
|
105
|
+
const expanded = clusterMapNodes(
|
|
106
|
+
nodes,
|
|
107
|
+
projectScale(10),
|
|
108
|
+
40,
|
|
109
|
+
new Set(['a', 'b']),
|
|
110
|
+
);
|
|
111
|
+
assert.equal(merged.length, 1);
|
|
112
|
+
assert.equal(expanded.length, 2);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function testZoomSplitsACluster() {
|
|
116
|
+
const nodes = [
|
|
117
|
+
{ id: 'a', lat: 0, lng: 0 },
|
|
118
|
+
{ id: 'b', lat: 0, lng: 3 },
|
|
119
|
+
];
|
|
120
|
+
const zoomedOut = clusterMapNodes(nodes, projectScale(10), CLUSTER_RADIUS_PX);
|
|
121
|
+
const zoomedIn = clusterMapNodes(nodes, projectScale(20), CLUSTER_RADIUS_PX);
|
|
122
|
+
assert.equal(zoomedOut.length, 1);
|
|
123
|
+
assert.equal(zoomedIn.length, 2);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function testRemapDropsIntraClusterArcs() {
|
|
127
|
+
const clusters = clusterMapNodes(
|
|
128
|
+
[
|
|
129
|
+
{ id: 'a', lat: 0, lng: 0 },
|
|
130
|
+
{ id: 'b', lat: 0, lng: 1 },
|
|
131
|
+
{ id: 'c', lat: 20, lng: 20 },
|
|
132
|
+
],
|
|
133
|
+
projectScale(10),
|
|
134
|
+
40,
|
|
135
|
+
);
|
|
136
|
+
const remapped = remapArcsToClusters(
|
|
137
|
+
[
|
|
138
|
+
{
|
|
139
|
+
id: 'ab',
|
|
140
|
+
fromEntity: 'a',
|
|
141
|
+
toEntity: 'b',
|
|
142
|
+
from: [0, 0],
|
|
143
|
+
to: [1, 0],
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: 'ac',
|
|
147
|
+
fromEntity: 'a',
|
|
148
|
+
toEntity: 'c',
|
|
149
|
+
from: [0, 0],
|
|
150
|
+
to: [20, 20],
|
|
151
|
+
},
|
|
152
|
+
],
|
|
153
|
+
clusters,
|
|
154
|
+
);
|
|
155
|
+
assert.deepEqual(
|
|
156
|
+
remapped.map(a => a.id),
|
|
157
|
+
['ac'],
|
|
158
|
+
);
|
|
159
|
+
const abCluster = clusters.find(c => c.memberIds.includes('a'));
|
|
160
|
+
assert.ok(abCluster);
|
|
161
|
+
assert.deepEqual(remapped[0].from, [abCluster.lng, abCluster.lat]);
|
|
162
|
+
assert.deepEqual(remapped[0].to, [20, 20]);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
describe('supply-chain-map-clusters', () => {
|
|
166
|
+
it('keeps isolated nodes as singletons', testIsolatedNodesStaySingletons);
|
|
167
|
+
it('merges nearby pixels', testNearbyPixelsMerge);
|
|
168
|
+
it('keeps far pixels separate', testFarPixelsStaySeparate);
|
|
169
|
+
it('merges a connected chain', testConnectedComponentMergesAChain);
|
|
170
|
+
it(
|
|
171
|
+
'keeps every projected node in a cluster',
|
|
172
|
+
testEveryProjectedNodeStaysInACluster,
|
|
173
|
+
);
|
|
174
|
+
it('drops unprojectable nodes', testUnprojectableNodesAreDropped);
|
|
175
|
+
it(
|
|
176
|
+
'expires force-open only on zoom out',
|
|
177
|
+
testForceOpenExpiresOnlyOnZoomOutPastExpandK,
|
|
178
|
+
);
|
|
179
|
+
it('keeps expand ids as singletons', testExpandIdsStaySingletons);
|
|
180
|
+
it('splits a cluster on zoom', testZoomSplitsACluster);
|
|
181
|
+
it('drops intra-cluster arcs', testRemapDropsIntraClusterArcs);
|
|
182
|
+
});
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Screen-space clustering for the flat supply-chain map.
|
|
2
|
+
//
|
|
3
|
+
// Supercluster / Mapbox is the usual default for millions of lon/lat points, but
|
|
4
|
+
// it speaks Web Mercator integer zoom. This map uses Natural Earth plus a
|
|
5
|
+
// continuous camera.k, with tens-to-hundreds of confirmed sites — not a tile
|
|
6
|
+
// grid. Clustering in projected pixels tracks zoom for free: the same two plants
|
|
7
|
+
// merge when they overlap on screen and split as the reader flies in.
|
|
8
|
+
//
|
|
9
|
+
// Connected components (not a first-come greedy grab) so A~B and B~C become one
|
|
10
|
+
// cluster even when A is just outside C's radius.
|
|
11
|
+
|
|
12
|
+
export const CLUSTER_RADIUS_PX = 40;
|
|
13
|
+
|
|
14
|
+
/** Force-open expires once the camera zooms out past the k recorded at expand. */
|
|
15
|
+
export function forceOpenExpired(openedAtK: number, currentK: number): boolean {
|
|
16
|
+
return currentK < openedAtK - 1e-6;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type ClusterableNode = {
|
|
20
|
+
id: string;
|
|
21
|
+
lat: number;
|
|
22
|
+
lng: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type MapClusterMarker = {
|
|
26
|
+
id: string;
|
|
27
|
+
lat: number;
|
|
28
|
+
lng: number;
|
|
29
|
+
count: number;
|
|
30
|
+
memberIds: string[];
|
|
31
|
+
nodeId: string | null;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type ClusterableArc = {
|
|
35
|
+
id: string;
|
|
36
|
+
fromEntity: string;
|
|
37
|
+
toEntity: string;
|
|
38
|
+
from: [number, number];
|
|
39
|
+
to: [number, number];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function find(parent: number[], i: number): number {
|
|
43
|
+
while (parent[i] !== i) {
|
|
44
|
+
parent[i] = parent[parent[i]];
|
|
45
|
+
i = parent[i];
|
|
46
|
+
}
|
|
47
|
+
return i;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function clusterMapNodes<T extends ClusterableNode>(
|
|
51
|
+
nodes: readonly T[],
|
|
52
|
+
project: (lat: number, lng: number) => [number, number] | null,
|
|
53
|
+
radiusPx = CLUSTER_RADIUS_PX,
|
|
54
|
+
expandIds?: ReadonlySet<string>,
|
|
55
|
+
): MapClusterMarker[] {
|
|
56
|
+
const projected: { node: T; x: number; y: number }[] = [];
|
|
57
|
+
for (const node of nodes) {
|
|
58
|
+
const p = project(node.lat, node.lng);
|
|
59
|
+
if (!p) continue;
|
|
60
|
+
projected.push({ node, x: p[0], y: p[1] });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const n = projected.length;
|
|
64
|
+
const parent = Array.from({ length: n }, (_, i) => i);
|
|
65
|
+
const r2 = radiusPx * radiusPx;
|
|
66
|
+
for (let i = 0; i < n; i++) {
|
|
67
|
+
if (expandIds?.has(projected[i].node.id)) continue;
|
|
68
|
+
for (let j = i + 1; j < n; j++) {
|
|
69
|
+
if (expandIds?.has(projected[j].node.id)) continue;
|
|
70
|
+
const dx = projected[i].x - projected[j].x;
|
|
71
|
+
const dy = projected[i].y - projected[j].y;
|
|
72
|
+
if (dx * dx + dy * dy <= r2) {
|
|
73
|
+
const a = find(parent, i);
|
|
74
|
+
const b = find(parent, j);
|
|
75
|
+
if (a !== b) parent[b] = a;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const groups = new Map<number, typeof projected>();
|
|
81
|
+
for (let i = 0; i < n; i++) {
|
|
82
|
+
const root = find(parent, i);
|
|
83
|
+
const group = groups.get(root);
|
|
84
|
+
if (group) group.push(projected[i]);
|
|
85
|
+
else groups.set(root, [projected[i]]);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return [...groups.values()].map(members => {
|
|
89
|
+
members.sort((a, b) => (a.node.id < b.node.id ? -1 : 1));
|
|
90
|
+
const memberIds = members.map(m => m.node.id);
|
|
91
|
+
const lat =
|
|
92
|
+
members.reduce((sum, m) => sum + m.node.lat, 0) / members.length;
|
|
93
|
+
const lng =
|
|
94
|
+
members.reduce((sum, m) => sum + m.node.lng, 0) / members.length;
|
|
95
|
+
const singleton = members.length === 1;
|
|
96
|
+
return {
|
|
97
|
+
id: singleton ? members[0].node.id : `cluster:${memberIds[0]}`,
|
|
98
|
+
lat,
|
|
99
|
+
lng,
|
|
100
|
+
count: members.length,
|
|
101
|
+
memberIds,
|
|
102
|
+
nodeId: singleton ? members[0].node.id : null,
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function remapArcsToClusters<T extends ClusterableArc>(
|
|
108
|
+
arcs: readonly T[],
|
|
109
|
+
clusters: readonly MapClusterMarker[],
|
|
110
|
+
): T[] {
|
|
111
|
+
const clusterOf = new Map<string, MapClusterMarker>();
|
|
112
|
+
for (const cluster of clusters) {
|
|
113
|
+
for (const id of cluster.memberIds) clusterOf.set(id, cluster);
|
|
114
|
+
}
|
|
115
|
+
const remapped: T[] = [];
|
|
116
|
+
for (const arc of arcs) {
|
|
117
|
+
const from = clusterOf.get(arc.fromEntity);
|
|
118
|
+
const to = clusterOf.get(arc.toEntity);
|
|
119
|
+
if (!from || !to || from.id === to.id) continue;
|
|
120
|
+
remapped.push({
|
|
121
|
+
...arc,
|
|
122
|
+
from: [from.lng, from.lat],
|
|
123
|
+
to: [to.lng, to.lat],
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return remapped;
|
|
127
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { type GeoPermissibleObjects, geoGraticule10, geoPath } from 'd3-geo';
|
|
2
|
+
import { feature } from 'topojson-client';
|
|
3
|
+
import type { GeometryCollection, Topology } from 'topojson-specification';
|
|
4
|
+
import worldData from 'world-atlas/countries-110m.json';
|
|
5
|
+
|
|
6
|
+
import type { MapBasemapPalette } from './map-colors';
|
|
7
|
+
import { type MapCamera, mapProjection, projectPoint } from './map-projection';
|
|
8
|
+
|
|
9
|
+
type CountryFeature = {
|
|
10
|
+
geometry: { type: string; coordinates: unknown };
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const topology = worldData as unknown as Topology<{
|
|
14
|
+
countries: GeometryCollection;
|
|
15
|
+
}>;
|
|
16
|
+
const COUNTRY_FEATURES = feature(topology, topology.objects.countries)
|
|
17
|
+
.features as unknown as CountryFeature[];
|
|
18
|
+
|
|
19
|
+
export type MapHitLane = { id: string; points: [number, number][] };
|
|
20
|
+
|
|
21
|
+
export type MapDrawArc = {
|
|
22
|
+
id: string;
|
|
23
|
+
from: [number, number];
|
|
24
|
+
to: [number, number];
|
|
25
|
+
color: string;
|
|
26
|
+
alpha: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type MapDrawCommand = {
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
dpr: number;
|
|
33
|
+
camera: MapCamera;
|
|
34
|
+
arcs: MapDrawArc[];
|
|
35
|
+
palette: MapBasemapPalette;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
type MapCanvas = {
|
|
39
|
+
width: number;
|
|
40
|
+
height: number;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type MapCanvasCtx =
|
|
44
|
+
| CanvasRenderingContext2D
|
|
45
|
+
| OffscreenCanvasRenderingContext2D;
|
|
46
|
+
|
|
47
|
+
export function resizeMapCanvas(
|
|
48
|
+
canvas: MapCanvas,
|
|
49
|
+
width: number,
|
|
50
|
+
height: number,
|
|
51
|
+
dpr: number,
|
|
52
|
+
): void {
|
|
53
|
+
canvas.width = Math.round(width * dpr);
|
|
54
|
+
canvas.height = Math.round(height * dpr);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Land, ocean, graticule and arcs. Safe in a Worker — no DOM, no React. */
|
|
58
|
+
export function paintSupplyChainMap(
|
|
59
|
+
ctx: MapCanvasCtx,
|
|
60
|
+
command: MapDrawCommand,
|
|
61
|
+
): MapHitLane[] {
|
|
62
|
+
const { width, height, dpr, camera, arcs, palette } = command;
|
|
63
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
64
|
+
ctx.clearRect(0, 0, width, height);
|
|
65
|
+
|
|
66
|
+
const proj = mapProjection(width, height, camera);
|
|
67
|
+
const path = geoPath(proj, ctx);
|
|
68
|
+
const draw = (obj: GeoPermissibleObjects) => {
|
|
69
|
+
ctx.beginPath();
|
|
70
|
+
path(obj);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
draw({ type: 'Sphere' });
|
|
74
|
+
ctx.fillStyle = palette.ocean;
|
|
75
|
+
ctx.fill();
|
|
76
|
+
|
|
77
|
+
draw(geoGraticule10());
|
|
78
|
+
ctx.strokeStyle = palette.graticule;
|
|
79
|
+
ctx.lineWidth = 1;
|
|
80
|
+
ctx.stroke();
|
|
81
|
+
|
|
82
|
+
for (const f of COUNTRY_FEATURES) {
|
|
83
|
+
draw(f.geometry as GeoPermissibleObjects);
|
|
84
|
+
ctx.fillStyle = palette.land;
|
|
85
|
+
ctx.fill();
|
|
86
|
+
}
|
|
87
|
+
ctx.beginPath();
|
|
88
|
+
for (const f of COUNTRY_FEATURES) path(f.geometry as GeoPermissibleObjects);
|
|
89
|
+
ctx.strokeStyle = palette.landStroke;
|
|
90
|
+
ctx.lineWidth = 0.7;
|
|
91
|
+
ctx.stroke();
|
|
92
|
+
|
|
93
|
+
const hitLanes: MapHitLane[] = [];
|
|
94
|
+
for (const a of arcs) {
|
|
95
|
+
draw({
|
|
96
|
+
type: 'LineString',
|
|
97
|
+
coordinates: [a.from, a.to],
|
|
98
|
+
});
|
|
99
|
+
ctx.strokeStyle = a.color;
|
|
100
|
+
ctx.globalAlpha = a.alpha;
|
|
101
|
+
ctx.lineWidth = 2;
|
|
102
|
+
ctx.lineCap = 'round';
|
|
103
|
+
ctx.stroke();
|
|
104
|
+
ctx.globalAlpha = 1;
|
|
105
|
+
|
|
106
|
+
const flat: [number, number][] = [];
|
|
107
|
+
for (const [lng, lat] of [a.from, a.to] as [number, number][]) {
|
|
108
|
+
const q = projectPoint(proj, lat, lng);
|
|
109
|
+
if (q) flat.push(q);
|
|
110
|
+
}
|
|
111
|
+
if (flat.length > 1) hitLanes.push({ id: a.id, points: flat });
|
|
112
|
+
}
|
|
113
|
+
return hitLanes;
|
|
114
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
buildConfirmedMapScene,
|
|
5
|
+
projectNaturalEarthPoint,
|
|
6
|
+
resolveMapSelection,
|
|
7
|
+
} from './supply-chain-map-scene';
|
|
8
|
+
import type {
|
|
9
|
+
EntityGeo,
|
|
10
|
+
GraphStatus,
|
|
11
|
+
MapEdge,
|
|
12
|
+
MapEntity,
|
|
13
|
+
} from './supply-chain-map-types';
|
|
14
|
+
|
|
15
|
+
function entity(
|
|
16
|
+
id: string,
|
|
17
|
+
status: GraphStatus,
|
|
18
|
+
geo?: EntityGeo,
|
|
19
|
+
type = 'supplier',
|
|
20
|
+
): MapEntity {
|
|
21
|
+
return {
|
|
22
|
+
id,
|
|
23
|
+
type,
|
|
24
|
+
name: `Entity ${id}`,
|
|
25
|
+
status,
|
|
26
|
+
geo,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function edge(
|
|
31
|
+
id: string,
|
|
32
|
+
from: string,
|
|
33
|
+
to: string,
|
|
34
|
+
status: GraphStatus,
|
|
35
|
+
): MapEdge {
|
|
36
|
+
return {
|
|
37
|
+
id,
|
|
38
|
+
from_entity: from,
|
|
39
|
+
to_entity: to,
|
|
40
|
+
relation: 'supplies',
|
|
41
|
+
status,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function testConfirmedPlacedNodesOnly() {
|
|
46
|
+
const entities = [
|
|
47
|
+
entity('valid', 'confirmed', {
|
|
48
|
+
lat: 51.5072,
|
|
49
|
+
lng: -0.1276,
|
|
50
|
+
precision: 'city',
|
|
51
|
+
place: 'London',
|
|
52
|
+
country: 'GB',
|
|
53
|
+
}),
|
|
54
|
+
entity('draft', 'draft', { lat: 48.8566, lng: 2.3522, precision: 'city' }),
|
|
55
|
+
entity('rejected', 'rejected', {
|
|
56
|
+
lat: 52.52,
|
|
57
|
+
lng: 13.405,
|
|
58
|
+
precision: 'city',
|
|
59
|
+
}),
|
|
60
|
+
entity('unplaced', 'confirmed'),
|
|
61
|
+
entity('nan', 'confirmed', {
|
|
62
|
+
lat: Number.NaN,
|
|
63
|
+
lng: 10,
|
|
64
|
+
precision: 'region',
|
|
65
|
+
}),
|
|
66
|
+
entity('infinite', 'confirmed', {
|
|
67
|
+
lat: 10,
|
|
68
|
+
lng: Number.POSITIVE_INFINITY,
|
|
69
|
+
precision: 'region',
|
|
70
|
+
}),
|
|
71
|
+
entity('latitude', 'confirmed', {
|
|
72
|
+
lat: 90.1,
|
|
73
|
+
lng: 10,
|
|
74
|
+
precision: 'country',
|
|
75
|
+
}),
|
|
76
|
+
entity('longitude', 'confirmed', {
|
|
77
|
+
lat: 10,
|
|
78
|
+
lng: -180.1,
|
|
79
|
+
precision: 'country',
|
|
80
|
+
}),
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
const scene = buildConfirmedMapScene(entities, []);
|
|
84
|
+
|
|
85
|
+
assert.deepEqual(scene.nodes, [
|
|
86
|
+
{
|
|
87
|
+
id: 'valid',
|
|
88
|
+
name: 'Entity valid',
|
|
89
|
+
type: 'supplier',
|
|
90
|
+
lat: 51.5072,
|
|
91
|
+
lng: -0.1276,
|
|
92
|
+
precision: 'city',
|
|
93
|
+
place: 'London',
|
|
94
|
+
country: 'GB',
|
|
95
|
+
},
|
|
96
|
+
]);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function testConfirmedEdgesNeedTwoPlacedConfirmedEndpoints() {
|
|
100
|
+
const entities = [
|
|
101
|
+
entity('a', 'confirmed', { lat: 51.5072, lng: -0.1276, precision: 'city' }),
|
|
102
|
+
entity('b', 'confirmed', {
|
|
103
|
+
lat: 35.6762,
|
|
104
|
+
lng: 139.6503,
|
|
105
|
+
precision: 'city',
|
|
106
|
+
}),
|
|
107
|
+
entity('unplaced', 'confirmed'),
|
|
108
|
+
entity('draft', 'draft', { lat: 1.3521, lng: 103.8198, precision: 'city' }),
|
|
109
|
+
];
|
|
110
|
+
const edges = [
|
|
111
|
+
edge('kept', 'a', 'b', 'confirmed'),
|
|
112
|
+
edge('draft-edge', 'a', 'b', 'draft'),
|
|
113
|
+
edge('missing-endpoint', 'a', 'missing', 'confirmed'),
|
|
114
|
+
edge('unplaced-endpoint', 'a', 'unplaced', 'confirmed'),
|
|
115
|
+
edge('draft-endpoint', 'a', 'draft', 'confirmed'),
|
|
116
|
+
];
|
|
117
|
+
|
|
118
|
+
assert.deepEqual(buildConfirmedMapScene(entities, edges).arcs, [
|
|
119
|
+
{
|
|
120
|
+
id: 'kept',
|
|
121
|
+
fromEntity: 'a',
|
|
122
|
+
toEntity: 'b',
|
|
123
|
+
relation: 'supplies',
|
|
124
|
+
from: [-0.1276, 51.5072],
|
|
125
|
+
to: [139.6503, 35.6762],
|
|
126
|
+
},
|
|
127
|
+
]);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function testInputsAreNotMutated() {
|
|
131
|
+
const entities = [
|
|
132
|
+
entity('a', 'confirmed', {
|
|
133
|
+
lat: 51.5072,
|
|
134
|
+
lng: -0.1276,
|
|
135
|
+
precision: 'city',
|
|
136
|
+
place: 'London',
|
|
137
|
+
}),
|
|
138
|
+
entity('b', 'confirmed', {
|
|
139
|
+
lat: 35.6762,
|
|
140
|
+
lng: 139.6503,
|
|
141
|
+
precision: 'city',
|
|
142
|
+
}),
|
|
143
|
+
];
|
|
144
|
+
const edges = [edge('e', 'a', 'b', 'confirmed')];
|
|
145
|
+
const entitiesBefore = JSON.parse(JSON.stringify(entities));
|
|
146
|
+
const edgesBefore = JSON.parse(JSON.stringify(edges));
|
|
147
|
+
|
|
148
|
+
buildConfirmedMapScene(entities, edges);
|
|
149
|
+
|
|
150
|
+
assert.deepEqual(entities, entitiesBefore);
|
|
151
|
+
assert.deepEqual(edges, edgesBefore);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function testStaleSelectionClearsWhenNodeLeavesScene() {
|
|
155
|
+
const london = entity('valid', 'confirmed', {
|
|
156
|
+
lat: 51.5072,
|
|
157
|
+
lng: -0.1276,
|
|
158
|
+
precision: 'city',
|
|
159
|
+
place: 'London',
|
|
160
|
+
country: 'GB',
|
|
161
|
+
});
|
|
162
|
+
const selected = buildConfirmedMapScene([london], []).nodes[0];
|
|
163
|
+
assert.equal(resolveMapSelection(selected, [selected])?.id, 'valid');
|
|
164
|
+
assert.equal(resolveMapSelection(selected, []), null);
|
|
165
|
+
assert.equal(resolveMapSelection(null, [selected]), null);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function testKnownPlaceProjectsInsideViewport() {
|
|
169
|
+
const width = 960;
|
|
170
|
+
const height = 520;
|
|
171
|
+
const point = projectNaturalEarthPoint([-0.1276, 51.5072], width, height);
|
|
172
|
+
|
|
173
|
+
assert.ok(point, 'London should project');
|
|
174
|
+
assert.ok(
|
|
175
|
+
point[0] >= 0 && point[0] <= width,
|
|
176
|
+
`x ${point[0]} should be inside the viewport`,
|
|
177
|
+
);
|
|
178
|
+
assert.ok(
|
|
179
|
+
point[1] >= 0 && point[1] <= height,
|
|
180
|
+
`y ${point[1]} should be inside the viewport`,
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
describe('supply-chain-map-scene', () => {
|
|
185
|
+
it('keeps only confirmed placed nodes', testConfirmedPlacedNodesOnly);
|
|
186
|
+
it(
|
|
187
|
+
'needs two placed confirmed endpoints',
|
|
188
|
+
testConfirmedEdgesNeedTwoPlacedConfirmedEndpoints,
|
|
189
|
+
);
|
|
190
|
+
it('does not mutate inputs', testInputsAreNotMutated);
|
|
191
|
+
it('clears a stale selection', testStaleSelectionClearsWhenNodeLeavesScene);
|
|
192
|
+
it(
|
|
193
|
+
'projects a known place inside the viewport',
|
|
194
|
+
testKnownPlaceProjectsInsideViewport,
|
|
195
|
+
);
|
|
196
|
+
});
|