@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.
Files changed (101) hide show
  1. package/dist/agent-glossary/workspace.generated.ts +1 -0
  2. package/dist/agent-glossary/workspace.index.md +1 -0
  3. package/dist/agent-glossary/workspace.md +1 -0
  4. package/dist/esm/src/components/ui/Banner/Banner.styl.js +1 -1
  5. package/dist/esm/src/components/ui/Chart/Chart.js +1 -2
  6. package/dist/esm/src/components/ui/Dialog/Dialog.styl.js +1 -1
  7. package/dist/esm/src/components/ui/Drawer/Drawer.styl.js +1 -1
  8. package/dist/esm/src/components/ui/DropdownMenu/DropdownMenu.styl.js +1 -1
  9. package/dist/esm/src/components/ui/MobileAdaptiveSelector/MobileAdaptiveSelector.styl.js +1 -1
  10. package/dist/esm/src/components/ui/Page/PageHeader/PageHeader.styl.js +1 -1
  11. package/dist/esm/src/components/ui/Select/Select.js +313 -5
  12. package/dist/esm/src/components/ui/Select/Select.styl.js +2 -2
  13. package/dist/esm/src/components/ui/Sheet/Sheet.styl.js +1 -1
  14. package/dist/esm/src/components/ui/SupplyChainMap/SupplyChainMap.js +384 -0
  15. package/dist/esm/src/components/ui/SupplyChainMap/SupplyChainMap.styl.js +7 -0
  16. package/dist/esm/src/components/ui/SupplyChainMap/map-colors.js +44 -0
  17. package/dist/esm/src/components/ui/SupplyChainMap/map-disclosure.js +142 -0
  18. package/dist/esm/src/components/ui/SupplyChainMap/map-labels.js +55 -0
  19. package/dist/esm/src/components/ui/SupplyChainMap/map-projection.js +186 -0
  20. package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-clusters.js +96 -0
  21. package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-draw.js +66 -0
  22. package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-scene.js +59 -0
  23. package/dist/esm/src/components/ui/SupplyChainMap/supply-chain-map-types.js +10 -0
  24. package/dist/esm/src/components/ui/SupplyChainMap/use-map-base-renderer.js +122 -0
  25. package/dist/esm/src/components/ui/Table/Table.styl.js +1 -1
  26. package/dist/esm/src/components/ui/Tabs/Tabs.styl.js +1 -1
  27. package/dist/esm/src/components/ui/Tooltip/Tooltip.styl.js +1 -1
  28. package/dist/esm/src/components/widgets/DriverCard/DriverPerformanceChart.js +1 -2
  29. package/dist/esm/src/index.js +2 -0
  30. package/dist/esm/types/src/components/ui/Select/Select.d.ts +3 -3
  31. package/dist/esm/types/src/components/ui/Select/Select.types.d.ts +3 -1
  32. package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.contract.test.d.ts +1 -0
  33. package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.d.ts +2 -0
  34. package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.example.d.ts +1 -0
  35. package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.render.worker.d.ts +15 -0
  36. package/dist/esm/types/src/components/ui/SupplyChainMap/SupplyChainMap.types.d.ts +10 -0
  37. package/dist/esm/types/src/components/ui/SupplyChainMap/index.d.ts +3 -0
  38. package/dist/esm/types/src/components/ui/SupplyChainMap/map-colors.d.ts +43 -0
  39. package/dist/esm/types/src/components/ui/SupplyChainMap/map-disclosure.d.ts +35 -0
  40. package/dist/esm/types/src/components/ui/SupplyChainMap/map-disclosure.test.d.ts +1 -0
  41. package/dist/esm/types/src/components/ui/SupplyChainMap/map-labels.d.ts +14 -0
  42. package/dist/esm/types/src/components/ui/SupplyChainMap/map-labels.test.d.ts +1 -0
  43. package/dist/esm/types/src/components/ui/SupplyChainMap/map-projection.d.ts +83 -0
  44. package/dist/esm/types/src/components/ui/SupplyChainMap/map-projection.test.d.ts +1 -0
  45. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-clusters.d.ts +25 -0
  46. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-clusters.test.d.ts +1 -0
  47. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-draw.d.ts +30 -0
  48. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-scene.d.ts +28 -0
  49. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-scene.test.d.ts +1 -0
  50. package/dist/esm/types/src/components/ui/SupplyChainMap/supply-chain-map-types.d.ts +31 -0
  51. package/dist/esm/types/src/components/ui/SupplyChainMap/use-map-base-renderer.d.ts +14 -0
  52. package/dist/esm/types/src/docs/pages/BannerPage.d.ts +1 -0
  53. package/dist/esm/types/src/docs/pages/SupplyChainMapPage.d.ts +1 -0
  54. package/dist/esm/types/src/index.d.ts +1 -0
  55. package/dist/esm/types/src/lib/logger.d.ts +1 -0
  56. package/package.json +7 -1
  57. package/src/components/ui/Banner/Banner.styl +1 -1
  58. package/src/components/ui/Dialog/Dialog.styl +2 -2
  59. package/src/components/ui/Drawer/Drawer.styl +2 -2
  60. package/src/components/ui/DropdownMenu/DropdownMenu.styl +2 -2
  61. package/src/components/ui/MobileAdaptiveSelector/MobileAdaptiveSelector.styl +1 -1
  62. package/src/components/ui/Page/PageHeader/PageHeader.styl +1 -1
  63. package/src/components/ui/Select/AGENT.md +3 -1
  64. package/src/components/ui/Select/Select.styl +31 -0
  65. package/src/components/ui/Select/Select.styl.d.ts +28 -25
  66. package/src/components/ui/Select/Select.tsx +506 -4
  67. package/src/components/ui/Select/Select.types.ts +3 -1
  68. package/src/components/ui/Sheet/Sheet.styl +2 -2
  69. package/src/components/ui/SupplyChainMap/AGENT.md +45 -0
  70. package/src/components/ui/SupplyChainMap/SupplyChainMap.contract.test.ts +45 -0
  71. package/src/components/ui/SupplyChainMap/SupplyChainMap.example.tsx +176 -0
  72. package/src/components/ui/SupplyChainMap/SupplyChainMap.render.worker.ts +63 -0
  73. package/src/components/ui/SupplyChainMap/SupplyChainMap.styl +221 -0
  74. package/src/components/ui/SupplyChainMap/SupplyChainMap.styl.d.ts +25 -0
  75. package/src/components/ui/SupplyChainMap/SupplyChainMap.tsx +720 -0
  76. package/src/components/ui/SupplyChainMap/SupplyChainMap.types.ts +15 -0
  77. package/src/components/ui/SupplyChainMap/index.ts +11 -0
  78. package/src/components/ui/SupplyChainMap/map-colors.ts +66 -0
  79. package/src/components/ui/SupplyChainMap/map-disclosure.test.ts +237 -0
  80. package/src/components/ui/SupplyChainMap/map-disclosure.ts +226 -0
  81. package/src/components/ui/SupplyChainMap/map-labels.test.ts +102 -0
  82. package/src/components/ui/SupplyChainMap/map-labels.ts +85 -0
  83. package/src/components/ui/SupplyChainMap/map-projection.test.ts +172 -0
  84. package/src/components/ui/SupplyChainMap/map-projection.ts +281 -0
  85. package/src/components/ui/SupplyChainMap/supply-chain-map-clusters.test.ts +182 -0
  86. package/src/components/ui/SupplyChainMap/supply-chain-map-clusters.ts +127 -0
  87. package/src/components/ui/SupplyChainMap/supply-chain-map-draw.ts +114 -0
  88. package/src/components/ui/SupplyChainMap/supply-chain-map-scene.test.ts +196 -0
  89. package/src/components/ui/SupplyChainMap/supply-chain-map-scene.ts +118 -0
  90. package/src/components/ui/SupplyChainMap/supply-chain-map-types.ts +45 -0
  91. package/src/components/ui/SupplyChainMap/use-map-base-renderer.ts +145 -0
  92. package/src/components/ui/Table/Table.styl +3 -3
  93. package/src/components/ui/Tabs/Tabs.styl +1 -0
  94. package/src/components/ui/Tooltip/Tooltip.styl +7 -2
  95. package/src/declarations.d.ts +4 -0
  96. package/src/docs/pages/BannerPage.tsx +83 -0
  97. package/src/docs/pages/FeedbackOverviewPage.tsx +7 -1
  98. package/src/docs/pages/SelectPage.tsx +30 -1
  99. package/src/docs/pages/SupplyChainMapPage.tsx +21 -0
  100. package/src/docs/registry.ts +12 -0
  101. package/src/index.ts +1 -0
@@ -0,0 +1,15 @@
1
+ import type {
2
+ MapCascadeOverlay,
3
+ MapEdge,
4
+ MapEntity,
5
+ } from './supply-chain-map-types';
6
+
7
+ export type SupplyChainMapProps = {
8
+ entities: MapEntity[];
9
+ edges: MapEdge[];
10
+ onEdgeSelect?: (id: string) => void;
11
+ onNodeClick?: (id: string) => void;
12
+ cascade?: MapCascadeOverlay;
13
+ fill?: boolean;
14
+ className?: string;
15
+ };
@@ -0,0 +1,11 @@
1
+ export { SupplyChainMap } from './SupplyChainMap';
2
+ export type { SupplyChainMapProps } from './SupplyChainMap.types';
3
+ export {
4
+ activeMapCascade,
5
+ type EntityGeo,
6
+ type GeoPrecision,
7
+ type GraphStatus,
8
+ type MapCascadeOverlay,
9
+ type MapEdge,
10
+ type MapEntity,
11
+ } from './supply-chain-map-types';
@@ -0,0 +1,66 @@
1
+ /** Basemap and mark colours from Team-Merged globe-colors — inlined so this
2
+ * map does not import the globe stack. Dark tones follow the same globe
3
+ * night land/ocean family so canvas paint matches `html.dark`. */
4
+
5
+ export const OCEAN_TONE = '#4b88b1';
6
+ export const OCEAN_TONE_DARK = '#3a5f78';
7
+
8
+ export type MapBasemapPalette = {
9
+ ocean: string;
10
+ land: string;
11
+ landStroke: string;
12
+ graticule: string;
13
+ };
14
+
15
+ export type MapMarkColors = {
16
+ org: string;
17
+ chain: string;
18
+ rim: string;
19
+ rimSelected: string;
20
+ arcDefault: string;
21
+ spine: string;
22
+ };
23
+
24
+ export const MAP_COLORS = {
25
+ land: '#fbfcfc',
26
+ landStroke: 'rgba(103,119,126,0.38)',
27
+ graticule: 'rgba(226,240,250,0.22)',
28
+ org: '#0f9ec0',
29
+ chain: '#54646a',
30
+ rim: '#ffffff',
31
+ rimSelected: '#242829',
32
+ arcDefault: 'rgba(222,236,246,0.88)',
33
+ spine: '#d93a2f',
34
+ } as const;
35
+
36
+ export const MAP_COLORS_DARK = {
37
+ land: '#c3cdd5',
38
+ landStroke: 'rgba(36,40,41,0.42)',
39
+ graticule: 'rgba(226,240,250,0.10)',
40
+ org: '#2ec4e0',
41
+ chain: '#8a9aa0',
42
+ rim: '#1a2226',
43
+ rimSelected: '#e8eef0',
44
+ arcDefault: 'rgba(46,80,96,0.82)',
45
+ spine: '#ff6b5a',
46
+ } as const;
47
+
48
+ export function mapBasemapPalette(dark: boolean): MapBasemapPalette {
49
+ return dark
50
+ ? {
51
+ ocean: OCEAN_TONE_DARK,
52
+ land: MAP_COLORS_DARK.land,
53
+ landStroke: MAP_COLORS_DARK.landStroke,
54
+ graticule: MAP_COLORS_DARK.graticule,
55
+ }
56
+ : {
57
+ ocean: OCEAN_TONE,
58
+ land: MAP_COLORS.land,
59
+ landStroke: MAP_COLORS.landStroke,
60
+ graticule: MAP_COLORS.graticule,
61
+ };
62
+ }
63
+
64
+ export function mapMarkColors(dark: boolean): MapMarkColors {
65
+ return dark ? MAP_COLORS_DARK : MAP_COLORS;
66
+ }
@@ -0,0 +1,237 @@
1
+ import assert from 'node:assert/strict';
2
+
3
+ import {
4
+ type MapDisclosureTier,
5
+ discloseMapScene,
6
+ mapRoutingArcs,
7
+ resolveMapDisclosureAnchors,
8
+ } from './map-disclosure';
9
+
10
+ type TestNode = { id: string; label: string; impacted?: boolean };
11
+ type TestArc = {
12
+ id: string;
13
+ fromId: string;
14
+ toId: string;
15
+ onSpine?: boolean;
16
+ impacted?: boolean;
17
+ reach?: string | null;
18
+ };
19
+
20
+ function largeScene() {
21
+ const nodes: TestNode[] = Array.from({ length: 71 }, (_, index) => ({
22
+ id: index === 0 ? 'org' : `node-${String(index).padStart(2, '0')}`,
23
+ label: `Node ${index}`,
24
+ }));
25
+ const arcs: TestArc[] = Array.from({ length: 70 }, (_, index) => ({
26
+ id: `arc-${String(index).padStart(2, '0')}`,
27
+ fromId: nodes[index].id,
28
+ toId: nodes[index + 1].id,
29
+ }));
30
+ return { nodes, arcs };
31
+ }
32
+
33
+ function testTierBudgetsAndCompleteReveal() {
34
+ const { nodes, arcs } = largeScene();
35
+ const cases: Array<{
36
+ options: { tier: MapDisclosureTier; orgId: string; expanded?: boolean };
37
+ arcCount: number;
38
+ routeBudget: number | null;
39
+ }> = [
40
+ { options: { tier: 'far', orgId: 'org' }, arcCount: 18, routeBudget: 18 },
41
+ { options: { tier: 'mid', orgId: 'org' }, arcCount: 48, routeBudget: 48 },
42
+ {
43
+ options: { tier: 'near', orgId: 'org' },
44
+ arcCount: arcs.length,
45
+ routeBudget: null,
46
+ },
47
+ {
48
+ options: { tier: 'far', orgId: 'org', expanded: true },
49
+ arcCount: arcs.length,
50
+ routeBudget: null,
51
+ },
52
+ ];
53
+
54
+ for (const { options, arcCount, routeBudget } of cases) {
55
+ const result = discloseMapScene(nodes, arcs, options);
56
+ assert.equal(result.arcs.length, arcCount, JSON.stringify(options));
57
+ assert.equal(result.routeBudget, routeBudget, JSON.stringify(options));
58
+ assert.equal(result.nodes.length, nodes.length);
59
+ if (routeBudget === null) {
60
+ assert.equal(result.canRevealByZoom, false);
61
+ }
62
+ }
63
+ }
64
+
65
+ function testMandatoryCascadeRoutesMayExceedTheBudget() {
66
+ const nodes = Array.from({ length: 36 }, (_, index) => ({
67
+ id: `n-${index}`,
68
+ label: `${index}`,
69
+ }));
70
+ const arcs: TestArc[] = Array.from({ length: 35 }, (_, index) => ({
71
+ id: `route-${String(index).padStart(2, '0')}`,
72
+ fromId: nodes[index].id,
73
+ toId: nodes[index + 1].id,
74
+ ...(index < 9
75
+ ? { onSpine: true }
76
+ : index < 18
77
+ ? { impacted: true }
78
+ : index < 25
79
+ ? { reach: 'exposure' }
80
+ : {}),
81
+ }));
82
+
83
+ const result = discloseMapScene(nodes, arcs, { tier: 'far' });
84
+ assert.equal(result.arcs.length, 25);
85
+ }
86
+
87
+ function testEverySelectedIncidentRouteSurvives() {
88
+ const selected = { id: 'selected', label: 'Selected' };
89
+ const leaves = Array.from({ length: 24 }, (_, index) => ({
90
+ id: `leaf-${String(index).padStart(2, '0')}`,
91
+ label: `Leaf ${index}`,
92
+ }));
93
+ const selectedArcs: TestArc[] = leaves.map((leaf, index) => ({
94
+ id: `selected-${String(index).padStart(2, '0')}`,
95
+ fromId: index % 2 === 0 ? selected.id : leaf.id,
96
+ toId: index % 2 === 0 ? leaf.id : selected.id,
97
+ }));
98
+
99
+ const result = discloseMapScene([selected, ...leaves], selectedArcs, {
100
+ tier: 'far',
101
+ selectedId: selected.id,
102
+ });
103
+ assert.equal(result.arcs.length, selectedArcs.length);
104
+ }
105
+
106
+ function testUnconnectedSelectionRemainsVisible() {
107
+ const { nodes, arcs } = largeScene();
108
+ const isolated = { id: 'isolated', label: 'Isolated' };
109
+ const result = discloseMapScene([...nodes, isolated], arcs, {
110
+ tier: 'far',
111
+ orgId: 'org',
112
+ selectedId: isolated.id,
113
+ });
114
+ assert.ok(result.nodes.includes(isolated));
115
+ }
116
+
117
+ function testFarTierKeepsIsolatedUnselectedPlaces() {
118
+ const { nodes, arcs } = largeScene();
119
+ const isolated = { id: 'isolated', label: 'Isolated' };
120
+ const result = discloseMapScene([...nodes, isolated], arcs, {
121
+ tier: 'far',
122
+ orgId: 'org',
123
+ });
124
+ assert.ok(result.nodes.includes(isolated));
125
+ assert.equal(result.nodes.length, nodes.length + 1);
126
+ assert.equal(result.hiddenNodeCount, 0);
127
+ }
128
+
129
+ function testIsolatedImpactedNodeRemainsVisible() {
130
+ const { nodes, arcs } = largeScene();
131
+ const impacted = {
132
+ id: 'isolated-impact',
133
+ label: 'Exposure site',
134
+ impacted: true,
135
+ };
136
+ const result = discloseMapScene([...nodes, impacted], arcs, {
137
+ tier: 'far',
138
+ orgId: 'org',
139
+ });
140
+ assert.ok(result.nodes.includes(impacted));
141
+ assert.equal(result.arcs.length, 18);
142
+ }
143
+
144
+ function testMissingOrganizationFallsBackToDegreeRanking() {
145
+ const hub = { id: 'hub', label: 'Hub' };
146
+ const leaves = Array.from({ length: 25 }, (_, index) => ({
147
+ id: `leaf-${String(index).padStart(2, '0')}`,
148
+ label: `Leaf ${index}`,
149
+ }));
150
+ const hubArcs: TestArc[] = leaves.map((leaf, index) => ({
151
+ id: `z-hub-${String(index).padStart(2, '0')}`,
152
+ fromId: hub.id,
153
+ toId: leaf.id,
154
+ }));
155
+ const lowDegreeArcs: TestArc[] = Array.from({ length: 25 }, (_, index) => ({
156
+ id: `a-low-${String(index).padStart(2, '0')}`,
157
+ fromId: `low-${index * 2}`,
158
+ toId: `low-${index * 2 + 1}`,
159
+ }));
160
+ const lowNodes = Array.from({ length: 50 }, (_, index) => ({
161
+ id: `low-${index}`,
162
+ label: `Low ${index}`,
163
+ }));
164
+
165
+ const result = discloseMapScene(
166
+ [hub, ...leaves, ...lowNodes],
167
+ [...lowDegreeArcs, ...hubArcs],
168
+ { tier: 'far', orgId: 'not-in-scene' },
169
+ );
170
+ assert.deepEqual(
171
+ result.arcs.map(arc => arc.id),
172
+ hubArcs.slice(0, 18).map(arc => arc.id),
173
+ );
174
+ }
175
+
176
+ function testCountsOrderAndObjectIdentityAreStable() {
177
+ const { nodes, arcs } = largeScene();
178
+ const options = { tier: 'far' as const, orgId: 'org', focusId: 'node-65' };
179
+ const first = discloseMapScene(nodes, arcs, options);
180
+ const second = discloseMapScene(nodes, arcs, options);
181
+ assert.deepEqual(
182
+ first.arcs.map(arc => arc.id),
183
+ second.arcs.map(arc => arc.id),
184
+ );
185
+ assert.equal(first.canRevealByZoom, true);
186
+ for (const arc of first.arcs) assert.equal(arcs.includes(arc), true);
187
+ }
188
+
189
+ function testAnchorLookupAndRoutingHelper() {
190
+ const lookup = new Map([
191
+ ['a', { id: 'rep' }],
192
+ ['rep', { id: 'rep' }],
193
+ ]);
194
+ assert.deepEqual(resolveMapDisclosureAnchors(lookup, { selectedId: 'a' }), {
195
+ selectedId: 'rep',
196
+ focusId: undefined,
197
+ signalIds: undefined,
198
+ });
199
+ assert.deepEqual(mapRoutingArcs(['full'], ['shown'], false), ['shown']);
200
+ assert.deepEqual(mapRoutingArcs(['full'], ['shown'], true), ['full']);
201
+ }
202
+
203
+ describe('map-disclosure', () => {
204
+ it(
205
+ 'applies tier budgets and complete reveal',
206
+ testTierBudgetsAndCompleteReveal,
207
+ );
208
+ it(
209
+ 'lets mandatory cascade routes exceed the budget',
210
+ testMandatoryCascadeRoutesMayExceedTheBudget,
211
+ );
212
+ it(
213
+ 'keeps every selected incident route',
214
+ testEverySelectedIncidentRouteSurvives,
215
+ );
216
+ it(
217
+ 'keeps an unconnected selection visible',
218
+ testUnconnectedSelectionRemainsVisible,
219
+ );
220
+ it(
221
+ 'keeps isolated unselected places at far tier',
222
+ testFarTierKeepsIsolatedUnselectedPlaces,
223
+ );
224
+ it(
225
+ 'keeps an isolated impacted node visible',
226
+ testIsolatedImpactedNodeRemainsVisible,
227
+ );
228
+ it(
229
+ 'falls back to degree ranking without org',
230
+ testMissingOrganizationFallsBackToDegreeRanking,
231
+ );
232
+ it(
233
+ 'keeps counts and identity stable',
234
+ testCountsOrderAndObjectIdentityAreStable,
235
+ );
236
+ it('resolves anchors and routing helper', testAnchorLookupAndRoutingHelper);
237
+ });
@@ -0,0 +1,226 @@
1
+ export type MapDisclosureTier = 'far' | 'mid' | 'near';
2
+
3
+ export type MapDisclosureOptions = {
4
+ tier: MapDisclosureTier;
5
+ orgId?: string | null;
6
+ selectedId?: string | null;
7
+ focusId?: string | null;
8
+ signalIds?: ReadonlySet<string>;
9
+ expanded?: boolean;
10
+ };
11
+
12
+ export type MapDisclosureResult<TNode, TArc> = {
13
+ nodes: TNode[];
14
+ arcs: TArc[];
15
+ hiddenNodeCount: number;
16
+ hiddenArcCount: number;
17
+ routeBudget: number | null;
18
+ canRevealByZoom: boolean;
19
+ };
20
+
21
+ type DisclosureAnchors = Pick<
22
+ MapDisclosureOptions,
23
+ 'selectedId' | 'focusId' | 'signalIds'
24
+ >;
25
+
26
+ export function resolveMapDisclosureAnchors<TNode extends { id: string }>(
27
+ lookup: ReadonlyMap<string, TNode>,
28
+ anchors: DisclosureAnchors,
29
+ ): DisclosureAnchors {
30
+ const resolve = <T extends string | null | undefined>(id: T): T =>
31
+ (id == null ? id : (lookup.get(id)?.id ?? id)) as T;
32
+ return {
33
+ selectedId: resolve(anchors.selectedId),
34
+ focusId: resolve(anchors.focusId),
35
+ signalIds:
36
+ anchors.signalIds == null
37
+ ? undefined
38
+ : new Set([...anchors.signalIds].map(id => resolve(id))),
39
+ };
40
+ }
41
+
42
+ export function mapRoutingArcs<TArc>(
43
+ fullSceneArcs: readonly TArc[],
44
+ disclosedArcs: readonly TArc[],
45
+ worldAnalysisActive: boolean,
46
+ ): readonly TArc[] {
47
+ return worldAnalysisActive ? fullSceneArcs : disclosedArcs;
48
+ }
49
+
50
+ type DisclosureArc = {
51
+ id: string;
52
+ fromId: string;
53
+ toId: string;
54
+ onSpine?: boolean;
55
+ impacted?: boolean;
56
+ reach?: string | null;
57
+ };
58
+
59
+ function tierBudget(options: MapDisclosureOptions): number | null {
60
+ if (options.expanded || options.tier === 'near') return null;
61
+ return options.tier === 'far' ? 18 : 48;
62
+ }
63
+
64
+ function isCascadeRoute(arc: DisclosureArc): boolean {
65
+ return Boolean(arc.onSpine || arc.impacted || arc.reach != null);
66
+ }
67
+
68
+ function compareIds(left: string, right: string): number {
69
+ return left < right ? -1 : left > right ? 1 : 0;
70
+ }
71
+
72
+ type AdjacencyEntry<TArc> = {
73
+ arc: TArc;
74
+ nextId: string;
75
+ };
76
+
77
+ function buildAdjacency<TArc extends DisclosureArc>(
78
+ arcs: readonly TArc[],
79
+ ): Map<string, AdjacencyEntry<TArc>[]> {
80
+ const adjacency = new Map<string, AdjacencyEntry<TArc>[]>();
81
+ const add = (nodeId: string, entry: AdjacencyEntry<TArc>) => {
82
+ const entries = adjacency.get(nodeId);
83
+ if (entries) entries.push(entry);
84
+ else adjacency.set(nodeId, [entry]);
85
+ };
86
+
87
+ for (const arc of arcs) {
88
+ add(arc.fromId, { arc, nextId: arc.toId });
89
+ add(arc.toId, { arc, nextId: arc.fromId });
90
+ }
91
+ for (const entries of adjacency.values()) {
92
+ entries.sort(
93
+ (left, right) =>
94
+ compareIds(left.arc.id, right.arc.id) ||
95
+ compareIds(left.nextId, right.nextId),
96
+ );
97
+ }
98
+ return adjacency;
99
+ }
100
+
101
+ function addShortestPath<TArc extends DisclosureArc>(
102
+ startId: string,
103
+ targetId: string,
104
+ adjacency: ReadonlyMap<string, readonly AdjacencyEntry<TArc>[]>,
105
+ mandatory: Set<TArc>,
106
+ ): void {
107
+ if (startId === targetId) return;
108
+
109
+ const queue = [startId];
110
+ const visited = new Set(queue);
111
+ const parent = new Map<string, { previousId: string; arc: TArc }>();
112
+ let cursor = 0;
113
+
114
+ while (cursor < queue.length && !visited.has(targetId)) {
115
+ const nodeId = queue[cursor++];
116
+ for (const { arc, nextId } of adjacency.get(nodeId) ?? []) {
117
+ if (visited.has(nextId)) continue;
118
+ visited.add(nextId);
119
+ parent.set(nextId, { previousId: nodeId, arc });
120
+ queue.push(nextId);
121
+ if (nextId === targetId) break;
122
+ }
123
+ }
124
+
125
+ if (!visited.has(targetId)) return;
126
+ for (let nodeId = targetId; nodeId !== startId; ) {
127
+ const step = parent.get(nodeId);
128
+ if (!step) return;
129
+ mandatory.add(step.arc);
130
+ nodeId = step.previousId;
131
+ }
132
+ }
133
+
134
+ export function discloseMapScene<
135
+ TNode extends { id: string; impacted?: boolean },
136
+ TArc extends DisclosureArc,
137
+ >(
138
+ nodes: readonly TNode[],
139
+ arcs: readonly TArc[],
140
+ options: MapDisclosureOptions,
141
+ ): MapDisclosureResult<TNode, TArc> {
142
+ const routeBudget = tierBudget(options);
143
+ if (routeBudget === null) {
144
+ return {
145
+ nodes: [...nodes],
146
+ arcs: [...arcs],
147
+ hiddenNodeCount: 0,
148
+ hiddenArcCount: 0,
149
+ routeBudget,
150
+ canRevealByZoom: false,
151
+ };
152
+ }
153
+
154
+ const degrees = new Map<string, number>();
155
+ for (const arc of arcs) {
156
+ degrees.set(arc.fromId, (degrees.get(arc.fromId) ?? 0) + 1);
157
+ degrees.set(arc.toId, (degrees.get(arc.toId) ?? 0) + 1);
158
+ }
159
+
160
+ const nodeIds = new Set(nodes.map(node => node.id));
161
+ const orgExists = options.orgId != null && nodeIds.has(options.orgId);
162
+ const adjacency = buildAdjacency(arcs);
163
+ const mandatory = new Set<TArc>();
164
+ for (const arc of arcs) {
165
+ if (
166
+ isCascadeRoute(arc) ||
167
+ (options.selectedId != null &&
168
+ (arc.fromId === options.selectedId || arc.toId === options.selectedId))
169
+ ) {
170
+ mandatory.add(arc);
171
+ }
172
+ }
173
+ if (orgExists) {
174
+ const anchorIds = [
175
+ options.selectedId,
176
+ options.focusId,
177
+ ...(options.signalIds ?? []),
178
+ ].filter((id): id is string => id != null && nodeIds.has(id));
179
+ for (const anchorId of anchorIds) {
180
+ addShortestPath(anchorId, options.orgId!, adjacency, mandatory);
181
+ }
182
+ }
183
+
184
+ const compareArcs = (left: TArc, right: TArc) => {
185
+ const leftOrgAdjacent =
186
+ orgExists &&
187
+ (left.fromId === options.orgId || left.toId === options.orgId);
188
+ const rightOrgAdjacent =
189
+ orgExists &&
190
+ (right.fromId === options.orgId || right.toId === options.orgId);
191
+ const orgRank = Number(rightOrgAdjacent) - Number(leftOrgAdjacent);
192
+ if (orgRank !== 0) return orgRank;
193
+
194
+ const leftDegree = Math.max(
195
+ degrees.get(left.fromId) ?? 0,
196
+ degrees.get(left.toId) ?? 0,
197
+ );
198
+ const rightDegree = Math.max(
199
+ degrees.get(right.fromId) ?? 0,
200
+ degrees.get(right.toId) ?? 0,
201
+ );
202
+ return rightDegree - leftDegree || compareIds(left.id, right.id);
203
+ };
204
+ const mandatoryArcs = [...mandatory].sort(compareArcs);
205
+ const ordinaryArcs = arcs
206
+ .filter(arc => !mandatory.has(arc))
207
+ .sort(compareArcs);
208
+ const ordinarySlots = Math.max(0, routeBudget - mandatoryArcs.length);
209
+ const disclosedArcs = [
210
+ ...mandatoryArcs,
211
+ ...ordinaryArcs.slice(0, ordinarySlots),
212
+ ];
213
+
214
+ // Places always stay on the map (as markers or clusters). The budget only
215
+ // hides routes — otherwise world view drops isolated / low-degree sites.
216
+ const hiddenArcCount = Math.max(0, arcs.length - disclosedArcs.length);
217
+
218
+ return {
219
+ nodes: [...nodes],
220
+ arcs: disclosedArcs,
221
+ hiddenNodeCount: 0,
222
+ hiddenArcCount,
223
+ routeBudget,
224
+ canRevealByZoom: hiddenArcCount > 0,
225
+ };
226
+ }
@@ -0,0 +1,102 @@
1
+ import assert from 'node:assert/strict';
2
+
3
+ import { type MapLabel, placeMapLabels } from './map-labels';
4
+
5
+ const W = 1000;
6
+ const H = 600;
7
+
8
+ function label(
9
+ id: string,
10
+ lat: number,
11
+ lng: number,
12
+ text = id,
13
+ primary = false,
14
+ ): MapLabel {
15
+ return { id, lat, lng, text, primary };
16
+ }
17
+
18
+ function project(lat: number, lng: number): [number, number] | null {
19
+ return [W / 2 + lng * 2, H / 2 - lat * 2];
20
+ }
21
+
22
+ function testSeparatedLabelsAllDraw() {
23
+ const placed = placeMapLabels(
24
+ [label('a', 0, -100), label('b', 0, 0), label('c', 0, 100)],
25
+ project,
26
+ W,
27
+ H,
28
+ );
29
+ assert.deepEqual(placed.map(l => l.id).sort(), ['a', 'b', 'c']);
30
+ }
31
+
32
+ function testCollidingLabelsKeepTheHigherRanked() {
33
+ const placed = placeMapLabels(
34
+ [label('winner', 0, 0), label('loser', 0.5, 0.5)],
35
+ project,
36
+ W,
37
+ H,
38
+ );
39
+ assert.deepEqual(
40
+ placed.map(l => l.id),
41
+ ['winner'],
42
+ );
43
+ }
44
+
45
+ function testOffscreenLabelsNeitherDrawNorVeto() {
46
+ const placed = placeMapLabels(
47
+ [label('far', 0, 4000), label('here', 0, 0)],
48
+ project,
49
+ W,
50
+ H,
51
+ );
52
+ assert.deepEqual(
53
+ placed.map(l => l.id),
54
+ ['here'],
55
+ );
56
+ }
57
+
58
+ function testAnUnprojectablePointIsSkipped() {
59
+ const placed = placeMapLabels([label('a', 0, 0)], () => null, W, H);
60
+ assert.equal(placed.length, 0);
61
+ }
62
+
63
+ function testPlacementCarriesTheAnchorPixel() {
64
+ const placed = placeMapLabels([label('a', 10, 20)], project, W, H);
65
+ assert.equal(placed[0].x, W / 2 + 40);
66
+ assert.equal(placed[0].y, H / 2 - 20);
67
+ }
68
+
69
+ function testLongerNamesClaimMoreRoom() {
70
+ const apart = placeMapLabels(
71
+ [label('a', 0, 0, 'Ab'), label('b', 0, 20, 'Cd')],
72
+ project,
73
+ W,
74
+ H,
75
+ );
76
+ assert.equal(apart.length, 2, 'short names side by side should both draw');
77
+ const crowded = placeMapLabels(
78
+ [
79
+ label('a', 0, 0, 'A very long supplier name GmbH'),
80
+ label('b', 0, 20, 'Cd'),
81
+ ],
82
+ project,
83
+ W,
84
+ H,
85
+ );
86
+ assert.deepEqual(
87
+ crowded.map(l => l.id),
88
+ ['a'],
89
+ );
90
+ }
91
+
92
+ describe('map-labels', () => {
93
+ it('draws separated labels', testSeparatedLabelsAllDraw);
94
+ it(
95
+ 'keeps the higher-ranked colliding label',
96
+ testCollidingLabelsKeepTheHigherRanked,
97
+ );
98
+ it('skips offscreen labels', testOffscreenLabelsNeitherDrawNorVeto);
99
+ it('skips an unprojectable point', testAnUnprojectablePointIsSkipped);
100
+ it('carries the anchor pixel', testPlacementCarriesTheAnchorPixel);
101
+ it('gives longer names more room', testLongerNamesClaimMoreRoom);
102
+ });